【问题标题】:Xamarin Forms watch ViewModel property from .xaml.cs classXamarin Forms 观看 .xaml.cs 类中的 ViewModel 属性
【发布时间】:2017-11-25 20:54:42
【问题描述】:

正如标题所示,在 Xamarin Forms 上,当 ViewModel 上的属性发生变化时,我试图从视图中观察。

这是我的 ViewModel 类

public class RegisterViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    public bool AutomaticVerificationDone { get; set; }

    public ICommand AutomaticVerification
    {
        get
        {
            return new Command(async () =>
            {
                AutomaticVerificationDone = true;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("AutomaticVerificationDone"));
            });
        }
    }
}

这是我的 Register.xaml.cs 类

public partial class Register : ContentPage
{
    public static readonly BindableProperty AutomaticVerificationDoneProperty = BindableProperty.Create(nameof(AutomaticVerificationDone), typeof(bool), typeof(Register), false);
    public bool AutomaticVerificationDone
    {
        get { return (bool)GetValue(AutomaticVerificationDoneProperty); }
        set
        {
            SetValue(AutomaticVerificationDoneProperty, value);
            if (value)
                accessButton.Opacity = 1;
            else
                accessButton.Opacity = 0.8f;

        }
    }

    public Register()
    {
        InitializeComponent();
        NavigationPage.SetHasNavigationBar(this, false);
        this.BindingContext = new RegisterViewModel();
    }
}

以这种方式做任何事情都不会发生。 我错过了什么?

【问题讨论】:

    标签: c# xamarin mvvm binding xamarin.forms


    【解决方案1】:

    可绑定属性不使用您的设置器;它们直接通过可绑定属性系统。

    相反,您需要将pass a propertyChanged callback 转至BindableProperty.Create

    但实际上,您应该在 XAML 中绑定 Opacity(使用转换器)。

    【讨论】:

      猜你喜欢
      • 2017-12-21
      • 1970-01-01
      • 2017-01-13
      • 2019-01-19
      • 2017-02-11
      • 2019-01-03
      • 1970-01-01
      • 2019-12-14
      • 2022-01-19
      相关资源
      最近更新 更多