最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網 會員登陸 & 注冊

WPF MVVM模式簡介

2023-04-07 10:26 作者:百寶門  | 我要投稿


WPF是Windows Presentation Foundation的縮寫,它是一種用于創(chuàng)建桌面應用程序的用戶界面框架。WPF支持多種開發(fā)模式,其中一種叫做MVVM(Model-View-ViewModel)。

什么是MVVM?

MVVM是一種軟件架構模式,它將應用程序分為三個層次:Model(模型),View(視圖)和ViewModel(視圖模型)。Model表示應用程序的數(shù)據(jù)和業(yè)務邏輯,View表示應用程序的用戶界面,ViewModel表示View和Model之間的橋梁,它負責處理View的數(shù)據(jù)綁定和用戶交互。

為什么要使用MVVM?

使用MVVM有以下幾個好處:

  • 降低了View和Model之間的耦合度,使得它們可以獨立地開發(fā)和測試。

  • 提高了代碼的可重用性和可維護性,因為ViewModel可以在不同的View之間共享。

  • 簡化了單元測試,因為ViewModel不依賴于具體的UI控件。

  • 支持雙向數(shù)據(jù)綁定,使得View可以自動更新Model的變化,反之亦然。

  • 利用了WPF提供的強大特性,如命令、依賴屬性、數(shù)據(jù)注解等。

下圖我們可以直觀的理解MVVM誰急模式:



View: 使用XAML呈現(xiàn)給用戶的界面,負責與用戶交互,接收用戶輸入,把數(shù)據(jù)展現(xiàn)給用戶。

Model: 事物的抽象,開發(fā)過程中涉及到的事物都可以抽象為Model,例如姓名、年齡、性別、地址等屬性.不包含方法,也不需要實現(xiàn)INotifyPropertyChanged接口.

ViewModel: 負責收集需要綁定的數(shù)據(jù)和命令,聚合Model對象,通過View類的DataContext屬性綁定到View。同時也可以處理一些UI邏輯。

如何實現(xiàn)MVVM?

實現(xiàn)MVVM需要遵循以下幾個步驟:

  1. 創(chuàng)建一個Model類,定義應用程序所需的數(shù)據(jù)和業(yè)務邏輯。

  2. 創(chuàng)建一個ViewModel類,繼承自INotifyPropertyChanged接口,并實現(xiàn)屬性變更通知。在ViewModel中定義與Model相關聯(lián)的屬性,并提供相應的命令來執(zhí)行用戶操作。

  3. 創(chuàng)建一個View類(通常是一個XAML文件),定義應用程序的用戶界面。在View中使用數(shù)據(jù)綁定來連接ViewModel中的屬性和命令,并設置相關的樣式和行為。

  4. 在App.xaml或其他合適的地方創(chuàng)建一個ViewModel實例,并將其作為View中DataContext屬性值。

  5. 示例代碼:


  1. // Model class

  2. public class User

  3. {

  4. ? ?public string Name { get; set; }

  5. ? ?public int Age { get; set; }

  6. }

  7. // ViewModel class

  8. using System;

  9. using System.Collections.Generic;

  10. using System.ComponentModel;

  11. using System.Linq;

  12. using System.Text;

  13. using System.Threading.Tasks;

  14. using System.Windows.Input;

  15. using System.Windows;

  16. namespace WpfApp1

  17. {

  18. ? ?public class UserInfoViewModel : INotifyPropertyChanged

  19. ? ?{

  20. ? ? ? ?private User user;

  21. ? ? ? ?public UserInfoViewModel()

  22. ? ? ? ?{

  23. ? ? ? ? ? ?user = new User();

  24. ? ? ? ? ? ?SaveCommand = new RelayCommand(Save);

  25. ? ? ? ? ? ?CancelCommand = new RelayCommand(Cancel);

  26. ? ? ? ?}

  27. ? ? ? ?public string UserName

  28. ? ? ? ?{

  29. ? ? ? ? ? ?get { return user.Name; }

  30. ? ? ? ? ? ?set

  31. ? ? ? ? ? ?{

  32. ? ? ? ? ? ? ? ?user.Name = value;

  33. ? ? ? ? ? ? ? ?OnPropertyChanged("UserName");

  34. ? ? ? ? ? ?}

  35. ? ? ? ?}

  36. ? ? ? ?public int UserAge

  37. ? ? ? ?{

  38. ? ? ? ? ? ?get { return user.Age; }

  39. ? ? ? ? ? ?set

  40. ? ? ? ? ? ?{

  41. ? ? ? ? ? ? ? ?user.Age = value;

  42. ? ? ? ? ? ? ? ?OnPropertyChanged("UserAge");

  43. ? ? ? ? ? ?}

  44. ? ? ? ?}

  45. ? ? ? ?public string UserInfo

  46. ? ? ? ?{

  47. ? ? ? ? ? ?get { return $"Name:{UserName} Age:{UserAge}"; }

  48. ? ? ? ?}

  49. ? ? ? ?public ICommand SaveCommand { get; private set; }

  50. ? ? ? ?public ICommand CancelCommand { get; private set; }

  51. ? ? ? ?private void Save(object parameter)

  52. ? ? ? ?{

  53. ? ? ? ? ? ?// Save user data to database or service

  54. ? ? ? ? ? ?MessageBox.Show("User data saved!");

  55. ? ? ? ? ? ?OnPropertyChanged("UserInfo");

  56. ? ? ? ?}

  57. ? ? ? ?private void Cancel(object parameter)

  58. ? ? ? ?{

  59. ? ? ? ? ? ?// Close dialog window without saving data

  60. ? ? ? ? ? ?var window = parameter as Window;

  61. ? ? ? ? ? ?if (window != null)

  62. ? ? ? ? ? ? ? ?window.Close();

  63. ? ? ? ?}

  64. ? ? ? ?public event PropertyChangedEventHandler PropertyChanged;

  65. ? ? ? ?protected void OnPropertyChanged(string propertyName)

  66. ? ? ? ?{

  67. ? ? ? ? ? ?if (PropertyChanged != null)

  68. ? ? ? ? ? ? ? ?PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

  69. ? ? ? ?}

  70. ? ?}

  71. }

  72. //Command class

  73. public class RelayCommand : ICommand

  74. {

  75. ? ?public RelayCommand(Action<object> action)

  76. ? ?{

  77. ? ? ? ?DoExecute = action;

  78. ? ?}

  79. ? ?public event EventHandler? CanExecuteChanged;

  80. ? ?public Func<object, bool>? CanExecution { set; get; }

  81. ? ?public Action<object>? DoExecute { set; get; }

  82. ? ?public bool CanExecute(object? parameter)

  83. ? ?{

  84. ? ? ? ?if (CanExecution != null)

  85. ? ? ? ?{

  86. ? ? ? ? ? ?CanExecute(parameter);

  87. ? ? ? ?}

  88. ? ? ? ?return true;

  89. ? ?}

  90. ? ?public void Execute(object? parameter)

  91. ? ?{

  92. ? ? ? ?DoExecute!.Invoke(parameter!);

  93. ? ?}

  94. }

  1. // View class (XAML file)

  2. <Window x:Class="WpfApp1.MainWindow"

  3. ? ? ? ?xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

  4. ? ? ? ?xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

  5. ? ? ? ?Title="MainWindow" Height="220" Width="300">

  6. ? ?<Grid>

  7. ? ? ? ?<Grid.RowDefinitions>

  8. ? ? ? ? ? ?<RowDefinition Height="Auto"/>

  9. ? ? ? ? ? ?<RowDefinition Height="Auto"/>

  10. ? ? ? ? ? ?<RowDefinition Height="Auto"/>

  11. ? ? ? ? ? ?<RowDefinition Height="*"/>

  12. ? ? ? ?</Grid.RowDefinitions>

  13. ? ? ? ?<Grid.ColumnDefinitions>

  14. ? ? ? ? ? ?<ColumnDefinition Width="Auto"/>

  15. ? ? ? ? ? ?<ColumnDefinition Width="*"/>

  16. ? ? ? ?</Grid.ColumnDefinitions>

  17. ? ? ? ?<!-- Labels and textboxes for user name and age -->

  18. ? ? ? ?<Label Content="Name:" Grid.Row="0" Grid.Column="0" Margin="10"/>

  19. ? ? ? ?<TextBox Text="{Binding UserName}" Grid.Row="0" Grid.Column="1" Margin="10"/>

  20. ? ? ? ?<Label Content="Age:" Grid.Row="1" Grid.Column="0" Margin="10"/>

  21. ? ? ? ?<TextBox Text="{Binding UserAge}" Grid.Row="1" Grid.Column="1" Margin="10"/>

  22. ? ? ? ?<Label Content="{Binding UserInfo}" Grid.Row="2" Grid.Column="1" Margin="10"/>

  23. ? ? ? ?<!-- Buttons for save and cancel commands -->

  24. ? ? ? ?<StackPanel Orientation= "Horizontal" HorizontalAlignment= "Right"

  25. ? ? ? ? ? ? ? ? ? ?Grid.Row= "3" Grid.ColumnSpan= "2">

  26. ? ? ? ? ? ?<Button Content= "Save" Command="{Binding SaveCommand}"

  27. ? ? ? ? ? ? ? ? ? ?CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor,

  28. ? ? ? ? ? ? ? ? ?AncestorType={x:Type Window}}}" Margin= "10"/>

  29. ? ? ? ? ? ?<Button Content= "Cancel" Command="{Binding CancelCommand}"

  30. ? ? ? ? ? ? ? ? ? ?CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor,

  31. ? ? ? ? ? ? ? ? ?AncestorType={x:Type Window}}}" Margin= "10"/>

  32. ? ? ? ?</StackPanel>

  33. ? ?</Grid>

  34. </Window>

  1. // View code-behind file

  2. using System.Windows;

  3. namespace WpfApp1

  4. {

  5. ? /// Interaction logic for UserInfoView.xaml

  6. ? public partial class MainWindow : Window

  7. ? {

  8. ? ? ?public MainWindow()

  9. ? ? ?{

  10. ? ? ? ? InitializeComponent();

  11. ? ? ? ? // Set the ViewModel as the DataContext of the View

  12. ? ? ? ? this.DataContext = new UserInfoViewModel();

  13. ? ? ?}

  14. ? }

  15. }


運行結果如下:



代碼位置:https://github.com/DXG88/WpfApp1.git

有哪些MVVM框架?

雖然可以手動實現(xiàn)MVVM模式,但是也有許多第三方庫提供了更方便和高效地使用MVVM模式。以下是一些常見且流行的MVVM框架:

  • Prism: 一個由微軟支持的MVVM框架,提供了一系列服務和特性,如導航、模塊化、事件聚合、命令、依賴注入等。

  • MVVM Light: 一個輕量級的MVVM框架,提供了一些基礎類和組件,如ViewModelBase, RelayCommand, Messenger等。

  • Caliburn.Micro: 一個基于約定而非配置的MVVM框架,提供了一些高級特性,如屏幕激活/關閉生命周期管理、自動綁定、窗口管理器等。

原文地址:WPF MVVM模式簡介 - 百寶門的博客 (baibaomen.com)


WPF MVVM模式簡介的評論 (共 條)

分享到微博請遵守國家法律
弋阳县| 武宁县| 任丘市| 财经| 中方县| 黔江区| 兴义市| 泌阳县| 黔江区| 大庆市| 娱乐| 资溪县| 青神县| 政和县| 丁青县| 桐乡市| 甘谷县| 元朗区| 长白| 大名县| 邵武市| 尚义县| 宁波市| 蓝田县| 莲花县| 开阳县| 金溪县| 寿宁县| 留坝县| 汤原县| 长武县| 那曲县| 玉山县| 卓尼县| 玉环县| 利川市| 兖州市| 穆棱市| 明光市| 东明县| 龙海市|