Skip to content

Instantly share code, notes, and snippets.

@sjehutch
Last active July 27, 2020 08:39
Show Gist options
  • Save sjehutch/7626cb8a29a309e802a25548b7fcd00c to your computer and use it in GitHub Desktop.
Save sjehutch/7626cb8a29a309e802a25548b7fcd00c to your computer and use it in GitHub Desktop.
Binding to viewmodel
<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:mainn" xmlns:accordion="clr-namespace:Syncfusion.XForms.Accordion;assembly=Syncfusion.Expander.XForms"
x:Class="mainn.MainPage"
xmlns:vm="clr-namespace:mainn.ViewModels">
<ContentPage.Resources>
<ResourceDictionary>
<vm:MainViewModel x:Key="vm"/>
</ResourceDictionary>
</ContentPage.Resources>
<StackLayout>
<!-- Place new controls here -->
<Label Text="Welcome to Xamarin.Forms!" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" />
<Button Command="{Binding Source={StaticResource vm},Path=Move}" Text="GO"></Button>
<Label Text="{Binding Source={StaticResource vm},Path=FullName}" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" ></Label>
</StackLayout>
</ContentPage>
using System;
using System.ComponentModel;
using System.Windows.Input;
using Xamarin.Forms;
namespace mainn.ViewModels
{
public class MainViewModel : INotifyPropertyChanged
{
public ICommand Move { get; set; }
private string _fullname;
public string FullName
{
protected set
{
if (_fullname != value)
{
_fullname = value;
OnPropertyChanged("FullName");
}
}
get { return _fullname; }
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public MainViewModel()
{
Move = new Command(get => Moveit());
FullName = "tom";
}
public void Moveit ()
{
Application.Current.MainPage.DisplayAlert("hello", "hello", "hello");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment