如何在代码背后调用Prism事件

时间:2015-03-02 10:03:53

标签: c# wpf xaml prism

我是WPF的新手。目前,我想允许我的添加按钮使用单击或双击添加项目。但是,当我尝试双击时,它会结束两次单击事件。 XAML中的代码如下:

<Button.InputBindings>
    <MouseBinding Command="{Binding Path=AddCommand}" CommandParameter="{Binding}" MouseAction="LeftClick" />
    <MouseBinding Command="{Binding Path=AddCommand}" CommandParameter="{Binding}" MouseAction="LeftDoubleClick" />

我在网上找到了使用 DispatcherTimer 来解决问题的解决方案。我在代码中插入了这些:

private static DispatcherTimer myClickWaitTimer =
    new DispatcherTimer(
        new TimeSpan(0, 0, 0, 1),
        DispatcherPriority.Background,
        mouseWaitTimer_Tick,
        Dispatcher.CurrentDispatcher);

private void btnAdd_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
        // Stop the timer from ticking.
        myClickWaitTimer.Stop();

        // Handle Double Click Actions
}

private void btnAdd_Click(object sender, RoutedEventArgs e)
{
        myClickWaitTimer.Start();
}

private static void mouseWaitTimer_Tick(object sender, EventArgs e)
{
        myClickWaitTimer.Stop();

        // Handle Single Click Actions
}

所以这是我的问题。我删除了XAML中的 MouseBinding ,并希望在代码中调用AddCommand,但由于 PrismEventAggregator ,我遇到了问题。 .cs中的AddCommand如下:

private void AddCommandExecute(Object commandArg)
{
     // Broadcast Prism event for adding item
     this.PrismEventAggregator.GetEvent<AddItemEvent>().Publish(
     new AddItemPayload()
       {
          BlockType = this.BlockType
       }
     );
}

因此想知道如何在Code中调用 AddCommand (这是.cs中的Prism事件)?

注意:该按钮位于资源字典中,因此我无法使用按钮名称来调用该命令。

2 个答案:

答案 0 :(得分:0)

您需要创建一个订阅您要发布的事件的类,然后执行您想要的逻辑。

例如:

public class AddItemViewModel : INotifyPropertyChanged
{
    private IEventAggregator _eventAggregator;

    public AddItemViewModel(IEventAggregator eventAggregator)
    {
        _eventAggregator = eventAggregator;
        _eventAggregator.GetEvent<AddItemEvent>().Subscribe(AddItem);
    }

    private void AddItem(AddItemPayload payload)
    {
        // Your logic here
    }
}

然后,当您发布事件时,它将触发订阅者并执行。

答案 1 :(得分:0)

使用 Expression Blend SDK ,您可以创建一个封装所有自定义逻辑的Behavior。此行为将为您的命令及其参数提供两个依赖项属性,因此您可以轻松地为它们创建Binding,就像对InputBinding执行此操作一样。

将事件处理程序和DispatcherTimer逻辑移到此行为中:

using System.Windows.Interactivity;

class ClickBehavior : Behavior<Button>
{
    // a dependency property for the command
    public static readonly DependencyProperty CommandProperty =
        DependencyProperty.Register("Command", typeof(ICommand), 
            typeof(ClickBehavior), new PropertyMetadata(null));

    // a dependency property for the command's parameter        
    public static readonly DependencyProperty CommandParameterProperty =
        DependencyProperty.Register("CommandParameter", typeof(object), 
            typeof(ClickBehavior), new PropertyMetadata(null));

    public ICommand Command
    {
        get { return (ICommand)this.GetValue(CommandProperty); }
        set { this.SetValue(CommandProperty, value); }
    }

    public object CommandParameter
    {
        get { return this.GetValue(CommandParameterProperty); }
        set { this.SetValue(CommandParameterProperty, value); }
    }

    // on attaching to a button, subscribe to its Click and MouseDoubleClick events
    protected override void OnAttached()
    {
        this.AssociatedObject.Click += this.AssociatedObject_Click;
        this.AssociatedObject.MouseDoubleClick += this.AssociatedObject_MouseDoubleClick;
    }

    // on detaching, unsubscribe to prevent memory leaks
    protected override void OnDetaching()
    {
        this.AssociatedObject.Click -= this.AssociatedObject_Click;
        this.AssociatedObject.MouseDoubleClick -= this.AssociatedObject_MouseDoubleClick;
    }        

    // move your event handlers here        
    private void AssociatedObject_Click(object sender, RoutedEventArgs e)
    { //... }        

    private void AssociatedObject_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    { //... }

    // call this method in your event handlers to execute the command
    private void ExecuteCommand()
    {
        if (this.Command != null && this.Command.CanExecute(this.CommandParameter))
        {
            this.Command.Execute(this.CommandParameter);
        }
    }

用法非常简单。您需要声明其他名称空间:

<Window
    xmlns:local="Your.Behavior.Namespace"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    ...

最后,将行为附加到按钮:

<Button>
    <i:Interaction.Behaviors>
        <local:ClickBehavior Command="{Binding AddCommand}" CommandParameter="{Binding}"/>
    </i:Interaction.Behaviors>
</Button>