关于我们

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻公共列表

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

发布时间:2020-03-21 00:00:00

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

 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     15         <local:ReverseBool x:Key="ReverseBool" />16 17         <DataTemplate x:Key="Template1">18             <TextBlock Text="{Binding IsTemplate1,StringFormat=我是模板1:{0}}"/>19         20 21         <DataTemplate x:Key="Template2">22             <TextBlock Text="{Binding IsTemplate1,StringFormat=我是模板2:{0}}"/>23         24         25         <local:MainWindowViewModel x:Key="vm"/>26     27     28         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             34             <ContentControl Content="{Binding}">35                 36                     <Style TargetType="ContentControl">37                         <Setter Property="ContentTemplate" Value="{StaticResource Template1}" />38                         39                             <DataTrigger Binding="{Binding IsTemplate1}" Value="false">40                                 <Setter Property="ContentTemplate" Value="{StaticResource Template2}" />41                             42                         43                     44                 45             46         47         48             <i:EventTrigger EventName="Loaded">49                 <i:InvokeCommandAction Command="{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_MVVM17 {18     /// 19     /// MainWindow.xaml 的交互逻辑20     /// 21     public partial class MainWindow : Window22     {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_MVVM12 {13     class MainWindowViewModel : INotifyPropertyChanged14     {15         private bool isTemplate1 = true;16 17         public event PropertyChangedEventHandler PropertyChanged;18 19         public ICommand InitCommand { get; private set; }20 21         public bool IsTemplate122         {23             get => isTemplate1;24             set25             {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

 

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

 


/template/Home/Zkeys/PC/Static