【问题标题】:Data Binding using Mvvm Helpers使用 Mvvm Helpers 进行数据绑定
【发布时间】:2017-08-05 02:34:42
【问题描述】:

我正在尝试通过创建一个对象并绑定该对象来使用 MvvmHelpers 绑定一些数据。

https://github.com/jamesmontemagno/mvvm-helpers

在我的模型中,我有几个数据要绑定,所以我只是制作了一个我正在处理的快速模板。

如果我将 NameModel 中的内容移动到 NameViewModel,它确实可以工作,但是我正在尝试分离我的数据。

型号:

public class NameModel : BaseViewModel
    {
        string name;

        public string Name
        {
            get { return name; }
            set { SetProperty(ref name, value); }
        }
    }

查看模型:

public class NameViewModel : BaseViewModel
    {
        NameModel nameModel;

        public NameViewModel()
        {

            nameModel = new NameModel { name="Jon Doe" };
        }
    }

Page.xaml.cs

public partial class NamePage : ContentPage
    {
        public NamePage()
        {
            InitializeComponent();
            BindingContext = new NameViewModel();
        }
    }

Page.xaml:

<ContentPage 
    xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    x:Class="NameProj.NamePage">
    <StackLayout
        Orientation="Vertical" >
        <Label
            HorizontalOptions="FillAndExpand"
            VerticalOptions="FillAndExpand"
            BackgroundColor="Transparent"
            Text={Binding Name}/>
    </StackLayout>
</ContentPage>

【问题讨论】:

    标签: c# xamarin mvvm data-binding xamarin.forms


    【解决方案1】:

    您是否尝试将其更改为nameModel.Name

    <Label
            HorizontalOptions="FillAndExpand"
            VerticalOptions="FillAndExpand"
            BackgroundColor="Transparent"
            Text="{Binding nameModel.Name}"/>
    

    希望对你有帮助!

    【讨论】:

    • 这个答案加上 Alessandro Caliaro 的答案是正确的。
    【解决方案2】:
    NameModel nameModel;
    

    我认为应该公开

    public NameModel nameModel {get; set;}
    

    是的,我认为@mindOfAi 是正确的

    【讨论】:

    • 哈哈当然!在我阅读您的回答之前,我就知道了。我必须公开它才能访问它!
    • 是的,也是这样!哈哈哈。
    猜你喜欢
    • 2012-06-04
    • 2012-01-11
    • 2021-04-27
    • 2011-01-21
    • 1970-01-01
    • 1970-01-01
    • 2011-06-13
    • 1970-01-01
    • 2020-01-28
    相关资源
    最近更新 更多