界面控件DevExpress WPF入门级教程:MVVM 框架 – ViewModelBase

本文主要介绍DevExpress MVVM架构下View Model的ViewModelBase,欢迎下载相关控件体验!

ViewModelBase类是BindableBase的后代,它继承了 BindableBase 类的功能(例如 GetProperty、SetProperty 和 RaisePropertyChanged/RaisePropertiesChanged 方法)并提供以下附加功能。

分别为运行时和设计时模式初始化属性

视图模型可能包含需要访问数据库的属性,在 Visual Studio 设计器中工作时,不允许 View Model 连接到数据库,这会导致设计者出现错误。

在这种情况下,ViewModelBase 类提供了 OnInitializeInDesignMode 和 OnInitializeInRuntime 受保护的虚拟方法,您可以覆盖它们以分别初始化运行时和设计时模式的属性。

C#

public class ViewModel : ViewModelBase {public IEnumerable<Employee> Employees {get { return GetProperty(() => Employees); }private set { SetProperty(() => Employees, value); }}protected override void OnInitializeInDesignMode() {base.OnInitializeInDesignMode();Employees = new List<Employee>() {new Employee() { Name = "Employee 1" },};}protected override void OnInitializeInRuntime() {base.OnInitializeInRuntime();Employees = DatabaseController.GetEmployees();}}

VB.NET

Public Class ViewModelInherits ViewModelBasePublic Property Employees As IEnumerable(Of Employee)GetReturn GetProperty(Function() Employees)End GetSet(value As IEnumerable(Of Employee))SetProperty(Function() Employees, value)End SetEnd PropertyProtected Overrides Sub OnInitializeInDesignMode()MyBase.OnInitializeInDesignMode()Employees = New List(Of Employee) From {New Employee() With {.Name = "Employee 1"}}End SubProtected Overrides Sub OnInitializeInRuntime()MyBase.OnInitializeInRuntime()Employees = DatabaseController.GetEmployees()End SubEnd Class
访问在视图中注册的服务

ViewModelBase 类实现了维护服务机制的 ISupportServices 接口,使用 ISupportServices 接口的 ViewModelBase.GetService 方法允许您访问在视图中注册的服务。

XAML

<UserControl x_Class="ViewModelBaseSample.View"xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"xmlns:ViewModels="clr-namespace:ViewModelBaseSample.ViewModels" ...><UserControl.DataContext><ViewModels:ViewModel/></UserControl.DataContext><dxmvvm:Interaction.Behaviors><dxmvvm:MessageBoxService/></dxmvvm:Interaction.Behaviors>...</UserControl>

C#

public class ViewModel : ViewModelBase {
public IMessageBoxService MessageBoxService { get { return GetService<IMessageBoxService>(); } }
}

VB.NET

Public Class ViewModelInherits ViewModelBasePublic ReadOnly Property MessageBoxService As IMessageBoxServiceGetReturn GetService(Of IMessageBoxService)()End GetEnd PropertyEnd Class
使用 View Model 父子关系

从 ViewModelBase 继承的视图模型可以通过父子关系相互关联,这是通过在 ViewModelBase 类中实现的 ISupportParentViewModel 接口实现的。在这种情况下,子视图模型可以访问在主视图模型中注册的服务。

在视图模型之间传递数据

ViewModelBase 类实现了 ISupportParameter 接口,该接口可用于将初始数据传递给视图模型。

创建没有命令属性的命令

ViewModelBase 类实现 ICustomTypeDescriptor 接口以提供在运行时基于方法(具有 Command 属性)自动创建命令属性的能力。

创建命令的传统方法是如下声明命令属性。

C#

public class ViewModel : ViewModelBase {public ICommand SaveCommand { get; private set; }public ViewModel() {SaveCommand = new DelegateCommand(Save, CanSave);}public void Save() {//...}public bool CanSave() {//...}}

VB.NET

Public Class ViewModelInherits ViewModelBaseDim _SaveCommand As ICommandPublic ReadOnly Property SaveCommand As ICommandGetReturn _SaveCommandEnd GetEnd PropertyPublic Sub New()_SaveCommand = New DelegateCommand(AddressOf Save, AddressOf CanSave)End SubPublic Sub Save()'...End SubPublic Function CanSave() As Boolean'...End FunctionEnd Class

从 ViewModelBase 派生允许您使用更短的定义,为您希望用作命令的方法设置 Command 属性。

C#

public class ViewModel : ViewModelBase {[Command]public void Save() {//...}public bool CanSave() {//...}}

VB.NET

Public Class ViewModelInherits ViewModelBase<Command>Public Sub Save()'...End SubPublic Function CanSave() As Boolean'...End FunctionEnd Class

当 Command 属性应用于方法时,相应的命令将具有名称:[MethodName]Command”,其中 [MethodName] 是目标方法的名称。 对于上面的示例,命令名称将是“SaveCommand”。

XAML

<Button Command=”{Binding SaveCommand}”/>

Command 属性可以应用于无参数方法或具有一个参数的方法,您还可以通过设置命令属性来自定义命令。

DevExpress WPF | 下载试用

DevExpress WPF拥有120+个控件和库,将帮助您交付满足甚至超出企业需求的高性能业务应用程序。通过DevExpress WPF能创建有着强大互动功能的XAML基础应用程序,这些应用程序专注于当代客户的需求和构建未来新一代支持触摸的解决方案。 无论是Office办公软件的衍伸产品,还是以数据为中心的商业智能产品,都能通过DevExpress WPF控件来实现。


DevExpress技术交流群6:600715373      欢迎一起进群讨论

更多DevExpress线上公开课、中文教程资讯请上中文网获取

DevExpress企业定制服务
标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2022年3月11日
下一篇 2022年3月11日

相关推荐

发表回复

登录后才能评论