MVVM_UI和逻辑分离(事件利用命令替换),命令代替事件,命令传递事件参数,附完整demo

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

先大致看下效果:

主要是利用 Prism 库,可直接利用 nuget 添加

下面是详细代码:

<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"
        xmlns:prism="http://www.codeplex.com/prism"
        mc:Ignorable="d"
        Title="MainWindow"
        WindowStartupLocation="CenterScreen"
        Height="350" 
        Width="400"
        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>

            <TextBox Text="这里面实现了鼠标滚动获取滚动向量" Height="100" >
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="PreviewMouseWheel">
                        <prism:InvokeCommandAction Command="{Binding MouseWheelCommand}"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </TextBox>
        </StackPanel>
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Loaded">
                <prism:InvokeCommandAction Command="{Binding InitCommand}"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </Grid>
</Window>

 

using 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 DelegateCommand<object> MouseWheelCommand { get; set; }

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


        public MainWindowViewModel()
        {
            InitCommand = new DelegateCommand(Init);
            MouseWheelCommand = new DelegateCommand<object>(MouseWheel);
        }

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

        void MouseWheel(object obj)
        {
            if (obj is MouseWheelEventArgs eventArgs)
            {
                MessageBox.Show("滚动了:" + eventArgs.Delta);
            }
        }
    }
}

 有需要完整demo的,包含所需要的库,请移步下载

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值