c# - 如何在 Xamarin.Forms 中实现 INotifyPropertyChanged

标签 c# xamarin xamarin.forms

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

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

我有一个继承 INotifyProperty 的基本 View 模型包含一个事件。

 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]}"/>

关于c# - 如何在 Xamarin.Forms 中实现 INotifyPropertyChanged,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32667408/

相关文章:

xamarin - 问 : AutomationId not getting set on Android with Forms

xamarin.forms - 出现: Could not find 1 Android X assemblies,错误时,请确保安装以下NuGet软件包: - Xamarin. AndroidX.AppCompat.Resources

ios - Xamarin.Forms SkiaSharp Gradient 适用于 Android 但不适用于 iOS

c# - 从 C# 发送 sql server 时间戳

ios - MvvmCross和UICollectionView如何将SelectedItem从VM绑定(bind)到View

c# - 如何在 Asp.net 表中从左到右更改数据绑定(bind)?

c# - Xamarin不包括构建中的资源文件夹

Xamarin.forms如何像浏览器一样自动保存用户名

c# - 如何获得目标 ListView 项目?

c# - 如何扩展具有 50000 个同时任务的应用程序