【问题标题】:Create a ModelView xamarin创建一个 ModelView xamarin
【发布时间】:2023-04-08 17:11:02
【问题描述】:

这是一个示例应用程序,有一个条目和标签,写一个条目号和标签写相同的数字。

我想应用 MVVM 模型,但我不明白我需要做什么。

我不知道要创建 ModelView。

在我的代码中:

Model Persona,是具有 int age 属性的 Person;`命名空间 HolaMVVM.Models { 类角色 { 私有 int _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();
    }
}

但我不知道绑定人归属于视图

    class MainViewModel : BindableObject
{
    private Persona persona;

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

谢谢你!!!

【问题讨论】:

    标签: xamarin mvvm model-view


    【解决方案1】:

    我建议看看Introduction to MVVM

    并使用PropertyChanged.Fody

    您的角色应该实现 INotifyPropertyChanged(使用 Fody...)

    在您的 ViewModel 中,您应该有一个“public Persona persona {get;set;}

    然后在你的 XAML 中你可以绑定类似{Binding persona.Edad}

    这些是“基础”,然后观看@jamesmontemagno 视频

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多