MVVM_UI和逻辑分离(事件利用命令替换),并实现模板切换等...

近期公司重构了些界面,因为换肤和界面定制的缘故,需要把样式和逻辑分开;所以记录下关键的操作;主要是利用命令代替事件...

 1 "Demo_MVVM.MainWindow"
 2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 5         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 6         xmlns:local="clr-namespace:Demo_MVVM"
 7         xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
 8         mc:Ignorable="d"
 9         Title="MainWindow"
10         Height="450" 
11         Width="800"
12         DataContext="{DynamicResource vm}"
13         >
14     
15         "ReverseBool" />
16 
17         "Template1">
18             "{Binding IsTemplate1,StringFormat=我是模板1:{0}}"/>
19         
20 
21         "Template2">
22             "{Binding IsTemplate1,StringFormat=我是模板2:{0}}"/>
23         
24         
25         "vm"/>
26     
27     
28         
29             "采用mvvm,UI和逻辑分离" />
30             "Horizontal">
31                 "模板1" IsChecked="{Binding IsTemplate1}" GroupName="mb" />
32                 "模板2" IsChecked="{Binding IsTemplate1,Converter={StaticResource ReverseBool}}" GroupName="mb" />
33             
34             "{Binding}">
35                 
36                     
44                 
45             
46         
47         
48             "Loaded">
49                 "{Binding InitCommand}"  />
50             
51         
52     
53 
UI前段代码
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Windows;
 7 using System.Windows.Controls;
 8 using System.Windows.Data;
 9 using System.Windows.Documents;
10 using System.Windows.Input;
11 using System.Windows.Media;
12 using System.Windows.Media.Imaging;
13 using System.Windows.Navigation;
14 using System.Windows.Shapes;
15 
16 namespace Demo_MVVM
17 {
18     /// 
19     /// MainWindow.xaml 的交互逻辑
20     /// 
21     public partial class MainWindow : Window
22     {
23         public MainWindow()
24         {
25             InitializeComponent();
26         }
27     }
28 }
UI前段代码
 1 using Microsoft.Practices.Prism.Commands;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.ComponentModel;
 5 using System.Linq;
 6 using System.Text;
 7 using System.Threading.Tasks;
 8 using System.Windows;
 9 using System.Windows.Input;
10 
11 namespace Demo_MVVM
12 {
13     class MainWindowViewModel : INotifyPropertyChanged
14     {
15         private bool isTemplate1 = true;
16 
17         public event PropertyChangedEventHandler PropertyChanged;
18 
19         public ICommand InitCommand { get; private set; }
20 
21         public bool IsTemplate1
22         {
23             get => isTemplate1;
24             set
25             {
26                 isTemplate1 = value;
27                 PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsTemplate1)));
28             }
29         }
30 
31 
32         public MainWindowViewModel()
33         {
34             InitCommand = new DelegateCommand(Init);
35         }
36 
37         void Init()
38         {
39             MessageBox.Show("初始化完成!");
40         }
41     }
42 }
上下文实体

 

项目中依赖dll:

Microsoft.Practices.Prism.dll

Microsoft.Practices.Prism.MefExtensions.dll

System.Windows.Interactivity.dll

 

有需要的朋友可以前往下载:点击下载

 

你可能感兴趣的:(MVVM_UI和逻辑分离(事件利用命令替换),并实现模板切换等...)