c# - 简单的 Mvvm 数据绑定(bind) - xamarin 表单

标签 c# xamarin mvvm binding xamarin.forms

看起来我无法理解 xamarin.forms 应用程序中的精确 mvvm 模式是如何工作的。我想制作简单的 hello world 应用程序及其操作方式。

查看型号

namespace HelloWorldApp.ViewModels
{
    public class MainViewModel : INotifyPropertyChanged
    {
        private string _hello;

        private string hello
        {
            get { return _hello; }
            set
            {
                _hello = value;
                OnPropertyChanged();
            }
        }

        public MainViewModel() {
            hello = "Hello world";
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

查看
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="SkanerDetali.Views.LoginPage"
             xmlns:ViewModels="clr-namespace:HelloWorldApp.ViewModels;assembly=HelloWorldApp">

    <ContentPage.BindingContext>
        <ViewModels:MainViewModel />
    </ContentPage.BindingContext>

    <StackLayout>
        <Label Text="{Binding hello}" />
    </StackLayout>
</ContentPage>

我无法弄清楚我错过了什么。任何帮助将不胜感激

最佳答案

您的 hello属性(property)需要公开。

public class MainViewModel : INotifyPropertyChanged
{
    private string _hello;

    public string hello
    {
        get { return _hello; }
        set
        {
            _hello = value;
            OnPropertyChanged();
        }
    }
    ...
}

关于c# - 简单的 Mvvm 数据绑定(bind) - xamarin 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46094335/

相关文章:

c# - 使用 POST 方法调用 webservice(asmx)

testing - 在 Xamarin Test Cloud 中,如何为使用登录用户的应用程序在多个设备上运行并发测试?

swift - 使用 RxSwift 从 ViewController 向 ViewModel 传递值

ios - 使用 ReactiveCocoa 禁用 UIBarButtonItem

c# - WPF如何更新CanExecute

c# - .NET 文件访问冲突

c# - 收到错误 'The process cannot access the file because it is being used by another process.'

C# 速记 LINQ .Where 语句

xamarin - 如何从 TFS 连接 Visual Studio 应用程序中心(本地)

c# - 使用 xamarin.forms 跨平台 C# 在设备日历上设置提醒