クラス分けする
View ViewModelModel
XAML ViewModel
Class
Codebehind
*.cs
INotifyPropertyChanged
ICommand
Azure/
Web API
etc
7.
Bindingの関係(Label)
<ContentPage>
<LabelText="{Binding Name}"/>
XAML ViewModel+Model
Class
Codebehind
*.cs
public class MyItem : BindableBase
{
private string _Name;
public string Name
{
get { return _Name; }
set { this.SetProperty(ref this._Name, value); }
}
var vm = new MyItem();
this.BindingContext = vm;
8.
Bindingの関係(Label)
<ContentPage>
<LabelText="{Binding Name}"/>
XAML ViewModel+Model
Class
Codebehind
*.cs
public class MyItem : BindableBase
{
private string _Name;
public string Name
{
get { return _Name; }
set { this.SetProperty(ref this._Name, value); }
}
var vm = new MyItem();
this.BindingContext = vm;
9.
Bindingの関係(ListView)
<ListView>
<ListView.ItemTemplate>
<DataTemplate>
XAML
ViewModel+Model
Class
Codebehind
*.cs
<ViewCell>
<StackLayout>
<Label Text="{Binding Name}" />
public class MyItems :
ObservableCollection<MyItem>
{
var items = new MyItems();
lv.ItemsSource = items;
public class MyItem : BindableBase
{
private string _Name;
public string Name { get; set; }
{
get { return _Name; }
set { this.SetProperty(ref this._}
※ただし、一度だけの表示であれば、
INotifyPropertyChanged を実装する必要はない。
10.
Bindingの関係(ListView複合)
<ContentPage>
<ListViewItemsSource="{Binding Items}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
XAML
ViewModel+Model
Class
Codebehind
*.cs
<Label Text="{Binding Name}" />
public class MyViewModel
{
public MyItems Items { get; set; }
public class MyItems :
ObservableCollection<MyItem>
{
var vm = new MyViewModel();
this.BindingContext = vm;
public class MyItem : BindableBase
{
private string _Name;
public string Name { get; set; }
{
get { return _Name; }
set { this.SetProperty(ref this._}
ICommandの関係
<ContentPage>
<ButtonCommand="{Binding UpdateCommand}"/>
XAML
ViewModel+Model
Class
Codebehind
*.cs
var vm = new MyItem();
this.BindingContext = vm;
this.UpdateCommand = new Command(() => { });
Xamarin.Forms.CommandでIcommandを実装できる
15.
ICommandの関係(パラメータ付き)
<ContentPage>
<ButtonCommand="{Binding UpdateCommand}“
CommandParameter="..."/>
XAML
ViewModel+Model
Class
Codebehind
*.cs
var vm = new MyItem();
this.BindingContext = vm;
this.UpdateCommand =
new Command<string>((param) => { });
その他のXamarin.Forms のバインド機能
Part4. Data Binding Basics
http://developer.xamarin.com/guides/cr
oss-platform/xamarin-forms/xaml-for-xamarin-
forms/data_binding_basics/
Part 5. From Data Bindings to MVVM
http://developer.xamarin.com/guides/cr
oss-platform/xamarin-forms/xaml-for-xamarin-
forms/data_bindings_to_mvvm/