Carlos Alamo Garcia
Carlos Alamo Garcia

Reputation: 5

Create a ModelView xamarin

It's a example app, have a Entry and Label, write a Entry number and the label write the same number.

I want aply MVVM model, but I don't understand what I need to do.

I don't know to create the ModelView.

In my code I have:

Model Persona, is Person whith atribute int age;`namespace HolaMVVM.Models { class Persona { private int _edad;

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

View MainView.xaml

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

and MainView.xaml.cs

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

But I don't know Binding person atributes to View

    class MainViewModel : BindableObject
{
    private Persona persona;

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

TANK YOU!!!

Upvotes: 0

Views: 24

Answers (1)

Alessandro Caliaro
Alessandro Caliaro

Reputation: 5768

I suggests to take a look to Introduction to MVVM

and use PropertyChanged.Fody

Your Persona should implement INotifyPropertyChanged (with Fody...)

In your ViewModel you should have a "public Persona persona {get;set;}"

then in your XAML you can bind something like {Binding persona.Edad}

these are the "basics", then watch @jamesmontemagno video

Upvotes: 1

Related Questions