2017-06-01 77 views
0

這是一個示例應用程序,有一個條目和標籤,寫入條目號碼和標籤寫入相同的數字。創建模型查看xamarin

我想apvy MVVM模型,但我不明白我需要做什麼。

我不知道創建ModelView。

在我的代碼有:

型號女神,是人蒙山屬性附加傷害INT年齡;`命名空間HolaMVVM.Models { 類假面 { 私人詮釋_edad;

public Persona() 
    { 
     _edad = 0; 
    } 
    public int Edad 
    { 
     get { return _edad; } 
     set 
     { 
      _edad = value; 
     } 
    } 
} 

查看MainView.xaml

<StackLayout> 
    <Entry 
     Text="{Binding Edad, Mode=TwoWay}" 
     VerticalOptions="Center" 
     HorizontalOptions="Center"/> 
    <Label Text="{Binding Edad}" 
     VerticalOptions="Center" 
     HorizontalOptions="Center" /> 
</StackLayout> 

和MainView.xaml.cs

public partial class MainView : ContentPage 
{ 
    public MainView() 
    { 
     InitializeComponent(); 
     BindingContext = new MainViewModel(); 
    } 
} 

但我不知道綁定人atributes查看

class MainViewModel : BindableObject 
{ 
    private Persona persona; 

    public MainViewModel() 
    { 
     persona = new Persona(); 
    } 
} 

TANK您!!!

回答

1

我建議採取一看Introduction to MVVM

,並使用PropertyChanged.Fody

你的人物應該執行INotifyPropertyChanged(與Fody ...)

在您的視圖模型,你應該有一個 「public Persona persona {get;set;}

然後在您的XAML中,您可以綁定類似{Binding persona.Edad}

這些是「基礎知識」,然後看@jamesmontemagno視頻