翻譯|使用教程|編輯:龔雪|2022-04-21 10:06:30.253|閱讀 218 次
概述:本文主要介紹DevExpress MVVM架構(gòu)下生成的POCO視圖模型,歡迎下載官方正式版體驗!
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
POCO(Plain Old CLR Objects)視圖模型簡化并加快了開發(fā)過程。
POCO 視圖模型允許您:
這允許您創(chuàng)建干凈、簡單、可維護和可測試的 MVVM 代碼,POCO 視圖模型與任何 WPF 控件完全兼容。
您可以使用在編譯時生成的視圖模型在編譯時為您的視圖模型生成樣板代碼。
POCO 類不實現(xiàn)接口,也不需要繼承自基類,例如 ViewModelBase 或 BindableBase。要將 POCO 類轉(zhuǎn)換為功能齊全的 ViewModel,請使用 DevExpress.Mvvm.POCO.ViewModelSource.Create 方法創(chuàng)建一個類實例,請參見下面的示例。
C#
public class LoginViewModel { //This property will be converted to a bindable one public virtual string UserName { get; set; } //SaveAccountSettingsCommand will be created for the SaveAccountSettings and CanSaveAccountSettings methods: //SaveAccountSettingsCommand = new DelegateCommand<string>(SaveAccountSettings, CanSaveAccountSettings); public void SaveAccountSettings(string fileName) { //... } public bool CanSaveAccountSettings(string fileName) { return !string.IsNullOrEmpty(fileName); } //We recommend that you not use public constructors to prevent creating the View Model without the ViewModelSource protected LoginViewModel() { } //This is a helper method that uses the ViewModelSource class for creating a LoginViewModel instance public static LoginViewModel Create() { return ViewModelSource.Create(() => new LoginViewModel()); } }
您可以使用 ViewModelSource 類在 XAML 中創(chuàng)建視圖模型實例。
XAML
<UserControl x:Class="DXPOCO.Views.LoginView" xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm" xmlns:ViewModels="clr-namespace:DXPOCO.ViewModels" DataContext="{dxmvvm:ViewModelSource Type=ViewModels:LoginViewModel}" ...> <Grid> <!--...--> </Grid> </UserControl>
ViewModelSource.Create 方法使用創(chuàng)建指定 ViewModel 類的后代,并在運行時返回后代類實例。 下面的代碼類似于 ViewModelSource 基于 LoginViewModel 類生成的代碼。
C#
public class LoginViewModel_EXTENSION : LoginViewModel, INotifyPropertyChanged { public override string UserName { get { return base.UserName; } set { if(base.UserName == value) return; base.UserName = value; RaisePropertyChanged("UserName"); } } DelegateCommand<string> saveAccountSettingsCommand; public DelegateCommand<string> SaveAccountSettingsCommand { get { return saveAccountSettingsCommand ?? (saveAccountSettingsCommand = new DelegateCommand<string>(SaveAccountSettings, CanSaveAccountSettings)); } } //INotifyPropertyChanged Implementation }
您可以使用以下任何方法將參數(shù)傳遞給 ViewModel 的構(gòu)造函數(shù)。
C#
ViewModelSource.Create(() => new LoginViewModel(caption: "Login") { UserName = "John Smith" });
C#
var factory = ViewModelSource.Factory((string caption) => new LoginViewModel(caption)); factory("Login");
這個例子演示了如何使用 POCO 機制來創(chuàng)建視圖模型。
LoginView.xaml
<UserControl x:Class="Example.View.LoginView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ViewModel="clr-namespace:Example.ViewModel" xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm" xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignHeight="500" d:DesignWidth="600" DataContext="{dxmvvm:ViewModelSource Type=ViewModel:LoginViewModel}"> <dxmvvm:Interaction.Behaviors> <dx:DXMessageBoxService/> </dxmvvm:Interaction.Behaviors> <Grid x:Name="LayoutRoot" Background="White"> <StackPanel Orientation="Vertical"> <StackPanel Orientation="Horizontal" Margin="10"> <TextBlock Text="UserName: " Margin="3" VerticalAlignment="Center"/> <TextBox Text="{Binding UserName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="3" Width="80"/> </StackPanel> <Button Content="Login" Command="{Binding LoginCommand}" Margin="5" HorizontalAlignment="Left"/> </StackPanel> </Grid> </UserControl>
LoginViewModel.cs
using DevExpress.Mvvm; using DevExpress.Mvvm.DataAnnotations; using DevExpress.Mvvm.POCO; namespace Example.ViewModel { [POCOViewModel] public class LoginViewModel { public static LoginViewModel Create() { return ViewModelSource.Create(() => new LoginViewModel()); } protected LoginViewModel() { } public virtual string UserName { get; set; } public void Login() { this.GetService<IMessageBoxService>().Show("Login succeeded", "Login", MessageButton.OK, MessageIcon.Information, MessageResult.OK); } public bool CanLogin() { return !string.IsNullOrEmpty(UserName); } } }
LoginViewModel.vb
Imports DevExpress.Mvvm Imports DevExpress.Mvvm.DataAnnotations Imports DevExpress.Mvvm.POCO Namespace Example.ViewModel <POCOViewModel> _ Public Class LoginViewModel Public Shared Function Create() As LoginViewModel Return ViewModelSource.Create(Function() New LoginViewModel()) End Function Protected Sub New() End Sub Public Overridable Property UserName() As String Public Sub Login() Me.GetService(Of IMessageBoxService)().Show("Login succeeded", "Login", MessageButton.OK, MessageIcon.Information, MessageResult.OK) End Sub Public Function CanLogin() As Boolean Return Not String.IsNullOrEmpty(UserName) End Function End Class End Namespace
DevExpress WPF擁有120+個控件和庫,將幫助您交付滿足甚至超出企業(yè)需求的高性能業(yè)務應用程序。通過DevExpress WPF能創(chuàng)建有著強大互動功能的XAML基礎應用程序,這些應用程序?qū)W⒂诋敶蛻舻男枨蠛蜆?gòu)建未來新一代支持觸摸的解決方案。 無論是Office辦公軟件的衍伸產(chǎn)品,還是以數(shù)據(jù)為中心的商業(yè)智能產(chǎn)品,都能通過DevExpress WPF控件來實現(xiàn)。
DevExpress技術(shù)交流群6:600715373 歡迎一起進群討論
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務必注明出處、不得修改原文相關鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都網(wǎng)