翻譯|使用教程|編輯:龔雪|2022-04-14 10:05:30.793|閱讀 270 次
概述:本文主要介紹DevExpress MVVM架構下View Model的ViewModelBase,歡迎下載相關控件體驗!
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
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 ViewModel Inherits ViewModelBase Public Property Employees As IEnumerable(Of Employee) Get Return GetProperty(Function() Employees) End Get Set(value As IEnumerable(Of Employee)) SetProperty(Function() Employees, value) End Set End Property Protected Overrides Sub OnInitializeInDesignMode() MyBase.OnInitializeInDesignMode() Employees = New List(Of Employee) From { New Employee() With {.Name = "Employee 1"} } End Sub Protected Overrides Sub OnInitializeInRuntime() MyBase.OnInitializeInRuntime() Employees = DatabaseController.GetEmployees() End Sub End 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 ViewModel Inherits ViewModelBase Public ReadOnly Property MessageBoxService As IMessageBoxService Get Return GetService(Of IMessageBoxService)() End Get End Property End Class
從 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 ViewModel Inherits ViewModelBase Dim _SaveCommand As ICommand Public ReadOnly Property SaveCommand As ICommand Get Return _SaveCommand End Get End Property Public Sub New() _SaveCommand = New DelegateCommand(AddressOf Save, AddressOf CanSave) End Sub Public Sub Save() '... End Sub Public Function CanSave() As Boolean '... End Function End Class
從 ViewModelBase 派生允許您使用更短的定義,為您希望用作命令的方法設置 Command 屬性。
C#
public class ViewModel : ViewModelBase { [Command] public void Save() { //... } public bool CanSave() { //... } }
VB.NET
Public Class ViewModel Inherits ViewModelBase <Command> Public Sub Save() '... End Sub Public Function CanSave() As Boolean '... End Function End Class
當 Command 屬性應用于方法時,相應的命令將具有名稱:[MethodName]Command”,其中 [MethodName] 是目標方法的名稱。 對于上面的示例,命令名稱將是“SaveCommand”。
XAML
<Button Command="{Binding SaveCommand}"/>
Command 屬性可以應用于無參數方法或具有一個參數的方法,您還可以通過設置命令屬性來自定義命令。
DevExpress WPF擁有120+個控件和庫,將幫助您交付滿足甚至超出企業需求的高性能業務應用程序。通過DevExpress WPF能創建有著強大互動功能的XAML基礎應用程序,這些應用程序專注于當代客戶的需求和構建未來新一代支持觸摸的解決方案。 無論是Office辦公軟件的衍伸產品,還是以數據為中心的商業智能產品,都能通過DevExpress WPF控件來實現。
DevExpress技術交流群6:600715373 歡迎一起進群討論
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都網