在不使用Blend 3 SDK的情况下向ICommand发送事件 [英] Event to ICommand without using the Blend 3 SDK

查看:73
本文介绍了在不使用Blend 3 SDK的情况下向ICommand发送事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,所有



我有一个问题。如何在不使用System.Window.Interactivity(Blend 3 SDK)的情况下在基于ICommand的方法中实现Event的转换。当需要这个库时,互联网上的几种方法已经找到了解决方案。



例如我有一个基于ICommand接口的方法,它绑定到menuitem Exit(从应用程序退出并保存结果等)。我想要相同的方法(基于ICommand)绑定到关闭/关闭窗口事件但我需要将命令转换为事件。



感谢解决方案

Hello, all

I have a question. How can I implement converting of the Event in the method based ICommand without using System.Window.Interactivity (Blend 3 SDK). Several ways in the Internet have led to the solution when need this library.

E.g. I have a method based on ICommand interface which bind to menuitem Exit (Exit from application with saving results etc.). And I want same method (based on ICommand) bind to close/closing window events but I need cast command to event.

Thanks for solutions

推荐答案

MVVM Light工具包有一个EventToCommand类可以解决这个问题。它甚至支持将事件的参数传递给命令方法。我已经多次使用它并且效果很好。如果您手动包含工具包(而不是使用NuGet),您可以引入该功能,而不是像ViewModelLocator那样的其他东西。



但是请注意,如果您公开发布应用程序,MVVM Light工具包要求您包含您正在使用它的事实。有关更多详细信息,请参阅网站上的许可证信息。
The MVVM Light toolkit has an EventToCommand class that will do the trick. It even supports passing the parameters for the event to the command method. I''ve used it many times and it works great. If you manually include the toolkit (rather than using NuGet) you can just bring in that functionality and not the other stuff like the ViewModelLocator.

Note, however, that the MVVM Light toolkit requires you to include the fact you are using it if you release your application publicly. Refer to the license info on the web site for more details.


我找到了解决方案,但它适用于基于类Delegate的常规事件。它的方法不适用于MulticastDelegate基于的方法(例如关闭,关闭窗口事件)。剩余使用路由事件或lib System.Windows.Interactivity或MVVM light工具包(感谢它解决方案 Jason Gleim [ ^ ])。



如果有趣的话,我在下面的文章中获取了有关命令和事件的信息:

1) MVVM中的命令 [ ^ ]

2)将任何WPF事件连接到ViewModel上的命令 [ ^ ]
I found solution but it works with general events based on class Delegate. It method doesn''t work with MulticastDelegate''s based methods (e.g. closed, closing window events). Remaining to use routed events or lib System.Windows.Interactivity or MVVM light toolkit (thanks for it solution Jason Gleim[^]).

If interestingly below articles where I took info about commands and events:
1) Commands in MVVM[^]
2) Wire any WPF Event to Command on ViewModel[^]


好的,我执行我的关闭/关闭行为


结束活动的实施

OK, I implement my closed/closing behaviour

Implementation of the closing event
public partial class Commands
    {
        #region Closing (Window event)
        // Event to command
        private static void Window_Closing(object sender, CancelEventArgs e)
        {
            FrameworkElement element = sender as FrameworkElement;
            if (element == null) return;
            ICommand command = GetClosingCommand(element);
            if(command != null && command.CanExecute(e))
                command.Execute(e);
        }

        // Property
        public static readonly DependencyProperty ClosingCommandProperty =
            DependencyProperty.RegisterAttached("ClosingCommand", typeof(ICommand),
            typeof(Commands), new PropertyMetadata(default(ICommand), OnClosingCommandChanged));

        public static ICommand GetClosingCommand(DependencyObject d) { return (ICommand)d.GetValue(ClosingCommandProperty); }
        public static void SetClosingCommand(DependencyObject d, ICommand value) { d.SetValue(ClosingCommandProperty, value); }

        // Event (add or remove)
        private static void OnClosingCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var element = d as Window;
            if (element != null)
            {
                if (e.OldValue == null && e.NewValue != null)
                    element.Closing += Window_Closing;
                else if (e.OldValue != null && e.NewValue == null)
                    element.Closing -= Window_Closing;
            }
        }
        #endregion
    }





这里是'我们正在创建一个依赖属性及其get和set方法。然后我们实现一个ChangeProperty句柄,我们在框架元素中添加或删除我们的EventHandle(在我们的例子中是它的窗口类)。事件处理程序只需使用CancelEventArgs参数执行命令。



例如命令



Here''s we are reating a dependency property and its get and set methods. Then we emplement a ChangeProperty handle where we add or remove our EventHandle to a framework element (in our case it''s window class). Event handler just execute a command with CancelEventArgs param.

E.g. of the command

public ICommand ClosingWindowCommand
        {
            get
            {
                return new RelayCommand<canceleventargs>((e)=>
                {
                    MessageBoxResult result = MessageBox.Show(
                        "Are you sure that want to exit from application?",
                        "Exit from application",
                        MessageBoxButton.YesNo, MessageBoxImage.Question);

                    if (result == MessageBoxResult.No) e.Cancel = true;
                });
            }
        }
</canceleventargs>



只显示带有两个按钮的消息框(是,否)。如果用户按下按钮否则分配给属性取消CancelEventArgs参数值为true。



XAML(更改我们的属性和绑定命令)


Just shows message box with two buttons (Yes, No). If user pressed button No then assign to property Cancel of the CancelEventArgs param value true.

XAML (changing our property and binding command)

<window x:class="MyApplication.View.MainWindow" xmlns:x="#unknown">
        x:Name="MainWindowInstance"
...
        xmlns:cmd="clr-namespace:MyApplication.Command"
        DataContext="{Binding CloseTab,ElementName=MainWindowInstance}"
        Title="MainWindow" Height="350" Width="525"
        cmd:Commands.ClosingCommand="{Binding ClosingWindowCommand}">
<!-- TODO MainWindow -->
</window>


这篇关于在不使用Blend 3 SDK的情况下向ICommand发送事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆