欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-07-09 21:59:03
近期公司重构了些界面,因为换肤和界面定制的缘故,需要把样式和逻辑分开;所以记录下关键的操作;主要是利用命令代替事件... 1

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

 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>
 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 }
 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

 

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