1. 程式人生 > 其它 >介面控制元件DevExpress WPF入門指南 - ViewModelBase

介面控制元件DevExpress WPF入門指南 - ViewModelBase

DevExpress WPF v21.2正式版下載

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
使用 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 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 | 下載試用

DevExpress WPF擁有120+個控制元件和庫,將幫助您交付滿足甚至超出企業需求的高效能業務應用程式。通過DevExpress WPF能建立有著強大互動功能的XAML基礎應用程式,這些應用程式專注於當代客戶的需求和構建未來新一代支援觸控的解決方案。 無論是Office辦公軟體的衍伸產品,還是以資料為中心的商業智慧產品,都能通過DevExpress WPF控制元件來實現。


DevExpress技術交流群6:600715373      歡迎一起進群討論

更多DevExpress線上公開課、中文教程資訊請上中文網獲取