5. シンプルなMVVM
View ViewModel Model
XAML Glue Data
XAML ViewModel(Model)
ViewModelに
データを含ませて
しまうことが多い
シンプルに
実装したいから
6. クラス分けする
View ViewModel Model
XAML ViewModel
Class
Codebehind
*.cs
INotifyPropertyChanged
ICommand
Azure/
Web API
etc
7. Bindingの関係(Label)
<ContentPage>
<Label Text="{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>
<Label Text="{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>
<ListView ItemsSource="{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._}
19. Viewをプラットフォーム依存にする
View ViewModel Model
XAMLによる実装
WPF
Windows Store
Windows Phone
Xamarin.Forms
プラットフォーム非依存
のライブラリにできる
ライブラリ
PCL
Shared Project
※ただし、活用範囲が狭ければ、
ライブラリにする必要はない