【MVVM】目的是為了分離視圖(View)和模型(Model)的耦合——解耦
1、View負責前端展示,與ViewModel進行數據和命令的交互。(
雙向的數據屬性傳遞,單向的命令屬性傳遞View→ViewModel)
2、ViewModel,負責前端視圖業務級別的邏輯結構組織,並將其反饋給前端。
3、Model,主要負責數據實體的結構處理,與ViewModel進行交互。
其實我個人認為,數據和業務交互這一層還是應該另外獨立,Model中完全就是實體模型,這樣更清晰。
【安裝Prism模板框架】建議用手機熱點下載。也可以在VS的NuGet中搜索,但是每建一個項目就需要搜索引用一次,麻煩。
VS2017及以上版本中,拓展—管理拓展(或工具—拓展和更新),聯機,搜索Prism Template Pack,安裝即可。(推薦此安裝方式)
也可以網上下載https://marketplace.visualstudio.com/items?itemName=BrianLagunas.PrismTemplatePack
新建項目界面出現下圖,安裝成功。
【實戰】像學高中物理受力分析一樣,先分析界面有多少數據屬性、命令屬性
新建Prsm Blank App(WPF) 項目:Demo MVVM
Views中MainWindow.xaml代碼:
<Window x:Class="DemoMVVM.Views.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:prism="http://prismlibrary.com/" prism:ViewModelLocator.AutoWireViewModel="True" Title="{Binding Title}" Height="200" Width="300"> <StackPanel> <TextBox Text="{Binding Input1}" Margin="5,20,5,5" Width="100"/> <TextBox Text="{Binding Input2}" Margin="5" Width="100"/> <Button Command="{Binding AddCommand}" Content="求和" Width="75" Margin="5"/> <TextBox Text="{Binding Result}" Margin="5" Width="100"/> </StackPanel> </Window>
ViewModels中MainWindowViewModel.cs代碼:其他文件代碼不動
using Prism.Mvvm; using Prism.Commands; //引入命令 using System; //ViewModel為View提供數據屬性、命令屬性 namespace DemoMVVM.ViewModels { public class MainWindowViewModel : BindableBase { #region 私有變量,以及對應的屬性(習慣大寫) private string _title = "Prism Application"; private double _input1; //默認0 private double _input2; private double _result; public double Result //數據屬性 { get { return _result; } set { _result = value; RaisePropertyChanged("Result"); } //變化結果展示在View界面上 } public double Input2 //數據屬性 { get { return _input2; } set { _input2 = value; } } public double Input1 //數據屬性 { get { return _input1; } set { _input1 = value; } } public string Title //數據屬性 { get { return _title; } set { _title = value; } } #endregion #region 命令屬性 public DelegateCommand AddCommand { get; set; } #endregion public void Add() //方法 { this.Result = this.Input1 + this.Input2; } public MainWindowViewModel() //將方法Add與命令屬性AddCommand聯系起來 { this.AddCommand = new DelegateCommand(new Action(Add)); } } }
注意:View與ModelView傳遞的是屬性、屬性、屬性(習慣大寫)
【實戰2】
新建Prsm Blank App(WPF) 項目:BlankApp5
Views中MainWindow.xaml代碼:
<Window x:Class="BlankApp5.Views.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:prism="http://prismlibrary.com/" prism:ViewModelLocator.AutoWireViewModel="True" Title="{Binding Title}" Height="200" Width="300"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="*"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <StackPanel Grid.Row="0" Orientation="Horizontal" VerticalAlignment="Center"> <TextBlock Text="學號" Margin="20,0" VerticalAlignment="Center"/> <TextBox Text="{Binding Id}" IsReadOnly="True" Width="200" Padding="2" /> </StackPanel> <StackPanel Grid.Row="1" Orientation="Horizontal" VerticalAlignment="Center"> <TextBlock Text="姓名" Margin="20,0" VerticalAlignment="Center"/> <TextBox Text="{Binding Name}" IsReadOnly="True" Width="200" Padding="2"/> </StackPanel> <Button Command="{Binding ShowCommand}" Content="顯示" Grid.Row="2" VerticalAlignment="Center" Width="50"/> </Grid> </Window>
ViewModels中MainWindowViewModel.cs代碼:其他文件代碼不動
using Prism.Mvvm; using Prism.Commands; using System; namespace BlankApp5.ViewModels { public class MainWindowViewModel : BindableBase { #region 私有變量,以及對應的屬性(習慣大寫) private string _title = "Prism Application"; private string _name; private string _id; public string Id { get { return _id; } set { _id = value; RaisePropertyChanged("Id"); } } public string Name { get { return _name; } set { _name = value; RaisePropertyChanged("Name"); } } public string Title { get { return _title; } set { _title=value; } } #endregion #region 命令屬性 public DelegateCommand ShowCommand { get; set; } #endregion public void Show() //方法 { this.Name = "夕西行"; this.Id = "20190809"; } public MainWindowViewModel() //命令屬性與方法聯系起來 { this.ShowCommand = new DelegateCommand(new Action(Show)); } } }
MainWindow.xaml.cs代碼:貼出來只是有疑問,大神可解答下
using System.Windows; using BlankApp5.ViewModels; namespace BlankApp5.Views { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); //this.DataContext = new MainWindowViewModel(); //這句有無貌似沒什么影響,懂得大神可以留言講下 } } }