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

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

<Window x:Class="Demo_MVVM.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Demo_MVVM"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        mc:Ignorable="d"
        Title="MainWindow"
        Height="450" 
        Width="800"
        DataContext="{DynamicResource vm}"
        >
    <Window.Resources>
        <local:ReverseBool x:Key="ReverseBool" />

        <DataTemplate x:Key="Template1">
            <TextBlock Text="{Binding IsTemplate1,StringFormat=我是模板1:{0}}"/>
        </DataTemplate>

        <DataTemplate x:Key="Template2">
            <TextBlock Text="{Binding IsTemplate1,StringFormat=我是模板2:{0}}"/>
        </DataTemplate>
        
        <local:MainWindowViewModel x:Key="vm"/>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <TextBlock Text="采用mvvm,UI和逻辑分离" />
            <StackPanel Orientation="Horizontal">
                <RadioButton Content="模板1" IsChecked="{Binding IsTemplate1}" GroupName="mb" />
                <RadioButton Content="模板2" IsChecked="{Binding IsTemplate1,Converter={StaticResource ReverseBool}}" GroupName="mb" />
            </StackPanel>
            <ContentControl Content="{Binding}">
                <ContentControl.Style>
                    <Style TargetType="ContentControl">
                        <Setter Property="ContentTemplate" Value="{StaticResource Template1}" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding IsTemplate1}" Value="false">
                                <Setter Property="ContentTemplate" Value="{StaticResource Template2}" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </ContentControl.Style>
            </ContentControl>
        </StackPanel>
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Loaded">
                <i:InvokeCommandAction Command="{Binding InitCommand}"  />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Demo_MVVM
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}
using Microsoft.Practices.Prism.Commands;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace Demo_MVVM
{
    class MainWindowViewModel : INotifyPropertyChanged
    {
        private bool isTemplate1 = true;

        public event PropertyChangedEventHandler PropertyChanged;

        public ICommand InitCommand { get; private set; }

        public bool IsTemplate1
        {
            get => isTemplate1;
            set
            {
                isTemplate1 = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsTemplate1)));
            }
        }


        public MainWindowViewModel()
        {
            InitCommand = new DelegateCommand(Init);
        }

        void Init()
        {
            MessageBox.Show("初始化完成!");
        }
    }
}

项目中依赖dll:

Microsoft.Practices.Prism.dll

Microsoft.Practices.Prism.MefExtensions.dll

System.Windows.Interactivity.dll

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

发布了7 篇原创文章 · 获赞 1 · 访问量 3876

猜你喜欢

转载自blog.csdn.net/u010438205/article/details/104962309