MVVM_UI和邏輯分離(事件利用命令替換),並實現模板切換等...

来源:https://www.cnblogs.com/xuling-297769461/archive/2020/03/19/12522580.html
-Advertisement-
Play Games

近期公司重構了些界面,因為換膚和界面定製的緣故,需要把樣式和邏輯分開;所以記錄下關鍵的操作;主要是利用命令代替事件... 1 <Window x:Class="Demo_MVVM.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006 ...


近期公司重構了些界面,因為換膚和界面定製的緣故,需要把樣式和邏輯分開;所以記錄下關鍵的操作;主要是利用命令代替事件...

 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:

Microsoft.Practices.Prism.dll

Microsoft.Practices.Prism.MefExtensions.dll

System.Windows.Interactivity.dll

 

有需要的朋友可以前往下載:點擊下載

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 前幾天微軟收購npm的新聞對於軟粉來收很是振奮。微軟收購npm很可能是為了加強Github Packages。目前Github,Typescript,VSCode,npm這些開源社區的重磅工具全部都在微軟旗下,顯示出了微軟對開源的態度,微軟已經不是以前那個封閉的微軟。Github推出Github P ...
  • 基於 Roslyn 實現一個簡單的條件解析引擎 Intro 最近在做一個勛章的服務,我們想定義一些勛章的獲取條件,滿足條件之後就給用戶頒發一個勛章,定義條件的時候會定義需要哪些參數,參數的類型,獲取勛章的時候會提供鎖需要的參數,有一些內置的參數,內置的參數解析器(ParamResolver)。 最後 ...
  • 作業:輸入某年某月某日,判斷這一天是這一年的第幾天?。要求:需寫一個函數,給定年月 日,求的該天處於該年的第幾天。然後在Main函數中測試。 思路: ①需要有兩個函數。一個主函數,一個Date函數用來計算天數。 ②在主函數裡面利用控制台輸入年月日,然後在調用Date函數. 由於調用函數了就傳值了,調 ...
  • asp.net core應用常常要通過nginx來反向代理, 普通的web api配置asp.net core反向代理比較常見, 如果在應用中集成了signalr, 如何使用nginx來反代呢? ...
  • 使用UUID或者GUID產生的ID沒有規則 Snowflake演算法是Twitter的工程師為實現遞增而不重覆的ID實現的 概述 分散式系統中,有一些需要使用全局唯一ID的場景,這種時候為了防止ID衝突可以使用36位的UUID,但是UUID有一些缺點,首先他相對比較長,另外UUID一般是無序的。有些時 ...
  • 帶著問題去思考!大家好。 修飾符 修飾符有什麼作用呢?它是什麼東西呢? 首先修飾符有四種 private[ˈpraɪvət] protected [prə'tektɪd] internal [ɪnˈtɜːnl] public [ˈpʌblɪk] 他們的特效依次是: private 修飾符用於設置類或 ...
  • Humanizer 能夠滿足您所有.Net關於操作和展示以下類型的需求,包括字元串、枚舉、日期、時間、時間跨度、數字和數量。它採用 MIT 進行授權分發。 ...
  • using System.Security.Cryptography; static void Main(string[] args) { string rawString = "Make every second count."; string encryptedString = Encrypt3 ...
一周排行
    -Advertisement-
    Play Games
  • C#TMS系統代碼-基礎頁面BaseCity學習 本人純新手,剛進公司跟領導報道,我說我是java全棧,他問我會不會C#,我說大學學過,他說這個TMS系統就給你來管了。外包已經把代碼給我了,這幾天先把增刪改查的代碼背一下,說不定後面就要趕鴨子上架了 Service頁面 //using => impo ...
  • 委托與事件 委托 委托的定義 委托是C#中的一種類型,用於存儲對方法的引用。它允許將方法作為參數傳遞給其他方法,實現回調、事件處理和動態調用等功能。通俗來講,就是委托包含方法的記憶體地址,方法匹配與委托相同的簽名,因此通過使用正確的參數類型來調用方法。 委托的特性 引用方法:委托允許存儲對方法的引用, ...
  • 前言 這幾天閑來沒事看看ABP vNext的文檔和源碼,關於關於依賴註入(屬性註入)這塊兒產生了興趣。 我們都知道。Volo.ABP 依賴註入容器使用了第三方組件Autofac實現的。有三種註入方式,構造函數註入和方法註入和屬性註入。 ABP的屬性註入原則參考如下: 這時候我就開始疑惑了,因為我知道 ...
  • C#TMS系統代碼-業務頁面ShippingNotice學習 學一個業務頁面,ok,領導開完會就被裁掉了,很突然啊,他收拾東西的時候我還以為他要旅游提前請假了,還在尋思為什麼回家連自己買的幾箱飲料都要叫跑腿帶走,怕被偷嗎?還好我在他開會之前拿了兩瓶芬達 感覺感覺前面的BaseCity差不太多,這邊的 ...
  • 概述:在C#中,通過`Expression`類、`AndAlso`和`OrElse`方法可組合兩個`Expression<Func<T, bool>>`,實現多條件動態查詢。通過創建表達式樹,可輕鬆構建複雜的查詢條件。 在C#中,可以使用AndAlso和OrElse方法組合兩個Expression< ...
  • 閑來無聊在我的Biwen.QuickApi中實現一下極簡的事件匯流排,其實代碼還是蠻簡單的,對於初學者可能有些幫助 就貼出來,有什麼不足的地方也歡迎板磚交流~ 首先定義一個事件約定的空介面 public interface IEvent{} 然後定義事件訂閱者介面 public interface I ...
  • 1. 案例 成某三甲醫預約系統, 該項目在2024年初進行上線測試,在正常運行了兩天後,業務系統報錯:The connection pool has been exhausted, either raise MaxPoolSize (currently 800) or Timeout (curren ...
  • 背景 我們有些工具在 Web 版中已經有了很好的實踐,而在 WPF 中重新開發也是一種費時費力的操作,那麼直接集成則是最省事省力的方法了。 思路解釋 為什麼要使用 WPF?莫問為什麼,老 C# 開發的堅持,另外因為 Windows 上已經裝了 Webview2/edge 整體打包比 electron ...
  • EDP是一套集組織架構,許可權框架【功能許可權,操作許可權,數據訪問許可權,WebApi許可權】,自動化日誌,動態Interface,WebApi管理等基礎功能於一體的,基於.net的企業應用開發框架。通過友好的編碼方式實現數據行、列許可權的管控。 ...
  • .Net8.0 Blazor Hybird 桌面端 (WPF/Winform) 實測可以完整運行在 win7sp1/win10/win11. 如果用其他工具打包,還可以運行在mac/linux下, 傳送門BlazorHybrid 發佈為無依賴包方式 安裝 WebView2Runtime 1.57 M ...