wpf prism 命令(事件转换成命令)

1 新建wpf工程,添加prism.Unity 和System.Windows.Interactivity的引用

2 新建Views和ViewModels文件夹

3 将MainWindow拖到Views

4 修改MainWindow

<Window x:Class="InteractionDemo.Views.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:InteractionDemo.Views"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:prism="http://prismlibrary.com/"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">

    <StackPanel >
        <TextBox Margin="10" Text="{Binding CurrentTime}" FontSize="32"  />
        <TextBox Margin="10" FontSize="32" Text="{Binding Foo,UpdateSourceTrigger=PropertyChanged}">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="TextChanged">
                    <i:InvokeCommandAction Command="{Binding TextChangedCommand}" CommandParameter="{Binding ElementName=mybtn}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </TextBox>
        <Button  x:Name="mybtn"  FontSize="30"  Content="Click Me" Margin="10" Height="60" Command="{Binding GetCurrentTimeCommand}"/>
        <Viewbox Height="80" >
            <CheckBox IsChecked="{Binding IsCanExcute}"  Content="CanExcute" Margin="10"  HorizontalAlignment="Center" VerticalAlignment="Center" />
        </Viewbox>
    </StackPanel>
</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 InteractionDemo.Views
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

5 在ViewModels文件夹新建MainWindowViewModel类

using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace InteractionDemo.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        private bool _isCanExcute;
        public bool IsCanExcute
        {
            get { return _isCanExcute; }
            set
            {
                SetProperty(ref _isCanExcute, value);
                GetCurrentTimeCommand.RaiseCanExecuteChanged();
            }
        }

        private string _currentTime;
        public string CurrentTime
        {
            get { return _currentTime; }
            set { SetProperty(ref _currentTime, value); }
        }

        private DelegateCommand _getCurrentTimeCommand;
        public DelegateCommand GetCurrentTimeCommand =>
            _getCurrentTimeCommand ?? (_getCurrentTimeCommand = new DelegateCommand(ExecuteGetCurrentTimeCommand, CanExecuteGetCurrentTimeCommand));

        void ExecuteGetCurrentTimeCommand()
        {
            this.CurrentTime = DateTime.Now.ToString();
        }

        bool CanExecuteGetCurrentTimeCommand()
        {
            return IsCanExcute;
        }

        private string _foo;
        public string Foo
        {
            get { return _foo; }
            set { SetProperty(ref _foo, value); }
        }

        private DelegateCommand<object> _textChangedCommand;
        public DelegateCommand<object> TextChangedCommand =>
          _textChangedCommand ?? (_textChangedCommand = new DelegateCommand<object>(ExecuteTextChangedCommand));

        void ExecuteTextChangedCommand(object parameter)
        {
            this.CurrentTime = Foo + ((Button)parameter)?.Name;
        }
    }
}

6 修改App

<prism:PrismApplication x:Class="InteractionDemo.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:InteractionDemo"
             xmlns:prism="http://prismlibrary.com/"
           >
    <Application.Resources>
         
    </Application.Resources>
</prism:PrismApplication>
using InteractionDemo.Views;
using Prism.Ioc;
using Prism.Unity;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace InteractionDemo
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : PrismApplication
    {
        protected override Window CreateShell()
        {
            return Container.Resolve<MainWindow>();
        }
        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            
        }
    }
}

7 Run

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF Prism MVVM中,ViewModels通常用来承载与视图相关的逻辑代码和数据。而命令则是一种将用户界面操作与ViewModel中的方法绑定起来的技术。 在ViewModel中声明命令的方式如下: ```csharp public ICommand MyCommand { get; private set; } ``` 其中,ICommand是一个接口,它定义了执行命令所需的方法。通常情况下,我们使用Prism库提供的DelegateCommand或者CompositeCommand来实现ICommand接口。 DelegateCommand表示可以通过传递Action或者Func来实现ICommand接口的类,并且它是一个泛型类型。例如: ```csharp public DelegateCommand<string> MyCommand { get; private set; } ``` CompositeCommand则是一种可以组合多个ICommand对象的类。例如: ```csharp public CompositeCommand MyCompositeCommand { get; private set; } ``` 在ViewModel的构造函数中,我们需要为命令对象赋值。例如: ```csharp public MyViewModel() { MyCommand = new DelegateCommand<string>(ExecuteMyCommand); MyCompositeCommand = new CompositeCommand(); MyCompositeCommand.RegisterCommand(MyCommand); } ``` 其中,ExecuteMyCommand是我们自定义的方法,用于执行命令的操作。 在View中,我们可以通过以下方式将命令与用户界面操作绑定起来: ```xaml <Button Content="Click Me" Command="{Binding MyCommand}" CommandParameter="Hello World"/> ``` 其中,Binding语法用于绑定ViewModel中的属性。在Command属性中,我们绑定了ViewModel中的MyCommand属性,这意味着当用户点击按钮时,MyCommand所绑定的ExecuteMyCommand方法将被调用。 而CommandParameter属性则是用来传递参数的。在这个例子中,我们将字符串"Hello World"作为参数传递给了ExecuteMyCommand方法。 总之,在WPF Prism MVVM中,ViewModels命令代码是用来将用户界面操作与ViewModel中的逻辑代码和数据绑定起来的一种技术,它可以有效地提高代码的可重用性和可维护性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值