MVVM 基础视图模型类 [英] MVVM base view model class

查看:46
本文介绍了MVVM 基础视图模型类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个基本视图模型类.ViewModelBase 是一个抽象类,我想定义我希望所有其他派生视图模型实现的属性.

I am working on creating a base view model class. ViewModelBase is an abstract class and I want to define the properties that I want all my other derived view models to implement.

其中一个属性是 ObservableCollection:

One of the properties is an ObservableCollection:

public abstract ObservableCollection<???> Items { get; set; }

从该基类派生的类将定义不同类型的项目(ObservableCollectionObservableCollection).

The classes that derive from this base class will have different types of Items defined (ObservableCollection<Person>, ObservableCollection<Car>).

如果我在 ViewModelBase 中将 ObservableCollection 类型设置为 object,则需要我在派生类中进行大量不同的转换才能使其工作.

If I set the ObservableCollection type to object in ViewModelBase, it would require me to do a lot of different casting in the derived classes to get it to work.

这是正确的方法吗?

推荐答案

我不太确定你为什么要让它如此通用,但如果你这样做了,我建议你让抽象基类通用还有:

I'm not quite sure why you would want to make it so generic, but if you did, I'd recommend that you make the abstract base class generic as well:

public abstract class ViewModelBase<T>
{
    public abstract ObservableCollection<T> Items { get; set; }
}

我希望您也确保您的 ViewModelBase 实现了 INotifyPropertyChanged.

I hope you also make sure that your ViewModelBase implements INotifyPropertyChanged.

这篇关于MVVM 基础视图模型类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆