【问题标题】:MVVM pattern, ViewModel Collection [closed]MVVM 模式,ViewModel 集合
【发布时间】:2023-03-17 18:34:01
【问题描述】:

我最近开始尝试掌握 MVVM 的窍门,但它似乎还不起作用。

我有我的模型、视图和视图模型。我有一个使用INotifyPropertyChanged 接口的基本视图模型。

我想收集我的ViewModels,以便我可以在所有视图中使用我的数据。但我似乎无法继续这样做。

无论如何,在阅读了大量不同的东西之后,我什至不再确定应该是什么。

我希望有人能为我解答的最大问题是我在哪里转储ViewModels 的收藏?我需要在View 中更改一个ViewModel 并在另一个中再次显示它。

public class BaseViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public  BaseViewModel()
    {

    }

    protected virtual void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

这是一个ViewModel,它正在一个视图中填充,然后需要在另一个视图中显示。但我不知道如何使用 ObservableCollection<T> 类完成此操作。

public class WorkViewModel : BaseViewModel
{
    private string reference;
    private string address;
    private string scope;
    private string cost;
    private double amount;
    private double gst;
    private double total;

    public string Reference
    {
        get { return reference; }
        set
        {
            if (reference != value)
            { reference = value; RaisePropertyChanged("Reference"); }
        }
    }

    public string Address
    {
        get { return address; }
        set
        {
            if (address != value)
            { address = value; RaisePropertyChanged("Address"); }
        }
    }

    public string Scope
    {
        get { return scope; }
        set
        {
            if (scope != value)
            { scope = value; RaisePropertyChanged("Scope"); }
        }
    }

    public string Cost
    {
        get { return cost; }
        set
        {
            if (cost != value)
            { cost = value; RaisePropertyChanged("Cost"); }
        }
    }

    public double Amount
    {
        get { return amount; }
        set
        {
            if (amount != value)
            {
                amount = value;
                GST = Math.Round(amount * 0.10,2);
                RaisePropertyChanged("Amount");
            }
        }
    }

    public double GST
    {
        get { return gst; }
        set
        {
            if (gst != value)
            {
                gst = value;
                Total = Math.Round(Amount + GST,2);
                RaisePropertyChanged("GST");
            }
        }
    }

    public double Total
    {
        get { return total; }
        set
        {
            if (total != value)
            {
                total = value;
                RaisePropertyChanged("Total");
            }
        }
    }

}

我试过这个:

"Create a BaseViewModel and a Collection ObservableCollection<BaseViewModel> _viewModels;

Create a Property ObservableCollection<BaseViewModel> ViewModels around _viewModels

Define your View Models like this and add to Collection

MainViewModel : BaseViewModel

Tab1ViewModel : BaseViewModel

Tab2ViewModel : BaseViewModel

现在你可以使用这个了:

Tab1ViewModel vm = (ViewModels.Where(vm => vm is Tab1ViewModel).Count() == 0) ? new  Tab1ViewModel(): (ViewModels.Where(vm => vm is Tab1ViewModel).FirstOrDefault() as Tab1ViewModel;"

好像我需要做一个单例?

【问题讨论】:

  • 请添加有问题的代码示例...

标签: c# design-patterns mvvm observablecollection


【解决方案1】:

您不需要在设计中的任何位置存储视图模型的集合。在我看来,您的视图模型应该使用来自支持源(很可能是您的模型)的数据。在该设计中,您应该能够实例化 2 个相同类型的视图模型,并且它们都将看到相同的数据,因为它们使用相同的底层数据。每个视图我总是有 1 个视图模型实例,并且它不与任何其他人共享。

【讨论】:

  • 这就是我所追求的,但显然我做的不对
  • +1 本文描述了在由两个不同的视图模型表示的模型中拥有一个可观察的集合:codeproject.com/Articles/28802/…
猜你喜欢
  • 1970-01-01
  • 2010-10-23
  • 1970-01-01
  • 1970-01-01
  • 2015-08-24
  • 2018-03-11
  • 1970-01-01
  • 1970-01-01
  • 2012-03-19
相关资源
最近更新 更多