繁体   English   中英

如何在 Xamarin.Forms 中实现 INotifyPropertyChanged

[英]How to implement INotifyPropertyChanged in Xamarin.Forms

我正在 Xamarin.Forms 中实现购物车。 在我的购物车页面中有一个带有数据的ListView 每个单元格都包含一个按钮,用于选择itemamount的计数。 在购物车视图中有一个总计标签。

我的问题是当数字选择器发生变化时,总计没有更新。 计算方法在 item 添加视图单元时调用。 我知道我需要为此实现INotifyProperty ,但我不确定如何去做。

我有一个基本视图模型,它继承了包含事件的INotifyProperty

 public class BaseViewModel : INotifyPropertyChanged
{
    private double  _price;
    public double Price
    {
        get 
        { 
            return _price; 
        }
        set 
        { 
            _price = value;
            OnPropertyChanged("Price");}
        } 

 protected virtual void OnPropertyChanged(string propertyName)
 {
     if (PropertyChanged != null)
     {
         PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
     }
 }

查看模型

    public BaseViewModel()
    {
        App.Instance.ViewModel = this;
        TempList = TempList ?? new ObservableCollection<cm_items>();
        this.Title = AppResources.AppResource.Cart_menu_title;
        this.Price = CartCell.price;
    }

作为一种设计方法,最好将 MVVM 实现为子类并将其实现到您的ViewModel

示例实现:

public class ObservableProperty : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

我还强烈建议将 ICommand 实现为字典结构,例如:

public abstract class ViewModelBase : ObservableProperty
{
    public Dictionary<string,ICommand> Commands { get; protected set; }

    public ViewModelBase()
    {
        Commands = new Dictionary<string,ICommand>();
    }
}

因此,您的 ViewModel 中的所有待办事项都只是继承 ViewModelBase 类并使用它

class LoginViewModel : ViewModelBase
{
    #region fields
    string userName;
    string password;
    #endregion

    #region properties
    public string UserName 
    {
         get {return userName;}
        set 
        {
            userName = value;
            OnPropertyChanged("UserName");
        }
     }

    public string Password 
    {
        get{return password;}
        set
        {
            password = value;
            OnPropertyChanged("Password");
        }
    }
    #endregion

    #region ctor
    public LoginViewModel()
    {
        //Add Commands
        Commands.Add("Login", new Command(CmdLogin));
    }
    #endregion


    #region UI methods

    private void CmdLogin()
    {
        // do your login jobs here
    }
    #endregion
}

最后:Xaml 用法:

<Entry Placeholder="Username"  Text="{Binding UserName}"/>
<Entry Placeholder="Password" Text="{Binding Password}" IsPassword="True"/>
<Button Text="Login" Command="{Binding Commands[Login]}"/>

例如试试这个视图模型:

public abstract class BaseViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected bool SetPropertyValue<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
    {
        if (value == null ? field != null : !value.Equals(field))
        {
            field = value;

            var handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
            return true;
        }
        return false;
    }
}

并在继承的类中像这样使用它:

    private int myProperty;
    public int MyProperty
    {
        get { return this.myProperty; }
        set { this.SetPropertyValue(ref this.myProperty, value); }
    }

当我开始 Xamarin 编码时,MVVM 有点混乱,直到我发现 ViewModel 上的 PropertyChangedEvent 向视图 (ContentPage) 发出信号,并更新了标签/文本框/等。

对于那些寻找“最新最好的”的人......这是一些修改后的代码:

private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
    var handler = PropertyChanged;
    if (handler != null)
    {
        handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

并在您的财产 Setter 上:

public string SomeProperty
{
    get { return _somProperty; }
    set
    {
       _someProperty= value;
            OnPropertyChanged();
        }
    }
}

好的? 不? 节省每次都必须传递属性名称!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM