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

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

 1 <Window x:Class="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     <Window.Resources>
15         <local:ReverseBool x:Key="ReverseBool" />
16 
17         <DataTemplate x:Key="Template1">
18             <TextBlock Text="{Binding IsTemplate1,StringFormat=我是模板1:{0}}"/>
19         </DataTemplate>
20 
21         <DataTemplate x:Key="Template2">
22             <TextBlock Text="{Binding IsTemplate1,StringFormat=我是模板2:{0}}"/>
23         </DataTemplate>
24         
25         <local:MainWindowViewModel x:Key="vm"/>
26     </Window.Resources>
27     <Grid>
28         <StackPanel>
29             <TextBlock Text="采用mvvm,UI和逻辑分离" />
30             <StackPanel Orientation="Horizontal">
31                 <RadioButton Content="模板1" IsChecked="{Binding IsTemplate1}" GroupName="mb" />
32                 <RadioButton Content="模板2" IsChecked="{Binding IsTemplate1,Converter={StaticResource ReverseBool}}" GroupName="mb" />
33             </StackPanel>
34             <ContentControl Content="{Binding}">
35                 <ContentControl.Style>
36                     <Style TargetType="ContentControl">
37                         <Setter Property="ContentTemplate" Value="{StaticResource Template1}" />
38                         <Style.Triggers>
39                             <DataTrigger Binding="{Binding IsTemplate1}" Value="false">
40                                 <Setter Property="ContentTemplate" Value="{StaticResource Template2}" />
41                             </DataTrigger>
42                         </Style.Triggers>
43                     </Style>
44                 </ContentControl.Style>
45             </ContentControl>
46         </StackPanel>
47         <i:Interaction.Triggers>
48             <i:EventTrigger EventName="Loaded">
49                 <i:InvokeCommandAction Command="{Binding InitCommand}"  />
50             </i:EventTrigger>
51         </i:Interaction.Triggers>
52     </Grid>
53 </Window>
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     /// <summary>
19     /// MainWindow.xaml 的交互逻辑
20     /// </summary>
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:mvvm

Microsoft.Practices.Prism.dllide

Microsoft.Practices.Prism.MefExtensions.dllthis

System.Windows.Interactivity.dllspa

 

有须要的朋友能够前往下载:点击下载.net

相关文章
相关标签/搜索