2011-03-10 61 views
0

我正在使用MVVM Light框架构建SL4应用程序。我的简单应用主要由一个主视图(shellView)组成,该视图分为多个UserControl。它们只是UI的一个方便的分离,因此它们没有自己的ViewModel。Galasoft RelayCommand没有触发

ShellView包含一个包含多个KeypadButtons(自定义用户控件)的键盘(自定义用户控件)。

我很确定(因为我已经检查)DataContext设置正确,并且它被层次结构中的所有用户控件使用。 (ShellView的Datacontext是ShellViewModel,Keypad的DataContext是ShellViewModel等)。

在ShellViewModel中我有一个名为“ProcessKey”的ICommand(RelayCommand)。

在键盘控制,我有这样的:

<controls:KeypadButton x:Name="testBtn" Text="Hello"> 
     <i:Interaction.Triggers> 
      <i:EventTrigger EventName="Click"> 
       <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding PressStandardKeyCommand}" /> 
      </i:EventTrigger> 
     </i:Interaction.Triggers> 
</controls:KeypadButton> 

的KeypadButton基本上是包含一个按钮的网格。 MouseLeftButtonUp事件被捕获并且自定义的“Click”事件被触发。让我告诉你一些代码来轻松地解释我在做什么:

public partial class KeypadButton : UserControl 
{ 
    public delegate void KeypadButtonClickHandler(object sender, RoutedEventArgs e); 
    public event KeypadButtonClickHandler Click; 

public KeypadButton() 
{ 
     // Required to initialize variables 
    InitializeComponent(); 
} 

    private void innerButton_Click(object sender, MouseButtonEventArgs e) 
    { 
     if (Click != null) 
      Click(sender, new KeypadButtonEventArgs()); 
    } 
} 

public class KeypadButtonEventArgs : RoutedEventArgs 
{ 
    public string test { get; set; } 
} 

现在,如果我设置一个断点innerButton_Click的身体,我可以看到点击正确捕获,它包含指向RelayCommand。然而,没有任何反应:“点击(发件人,新的KeypadButtonEventArgs());”被执行,但仅此而已。

为什么这表现如此呢?不应该执行RelayCommand中定义的目标函数吗?也许是范围相关的问题?

在此先感谢, 干杯, Gianluca。

回答

0

路由事件应该是这样的(see documentation)来定义:

public static readonly RoutedEvent TapEvent = EventManager.RegisterRoutedEvent(
    "Tap", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MyButtonSimple)); 

// Provide CLR accessors for the event 
public event RoutedEventHandler Tap 
{ 
     add { AddHandler(TapEvent, value); } 
     remove { RemoveHandler(TapEvent, value); } 
} 
+0

正如我在文章中所说,我正在使用SL4。据我所知,那里没有EventManager。我错了吗? – 2011-03-10 11:49:16

+0

我试图将我的自定义事件处理程序声明为RoutedEventHandler,但仍然存在同样的问题。我可以看到Click事件中有一个EventTriggerBase实例,但实际上应该调用它时,什么都不会发生。任何帮助将非常感激!谢谢大家! – 2011-03-10 12:20:54

+0

对不起,没有意识到你正在使用SL(下次需要更好地阅读)。根据这篇文章(http://www.silverlightshow.net/items/Routed-Events-in-Silverlight.aspx),SL中不支持自定义路由事件。 – 2011-03-10 18:51:07

1

正如其他评论中指出,这可能与该Click事件不是一个RoutedEvent

作为一种快速入侵,您可能可以在UserControl上使用MouseLeftButtonDown而不是Click事件。

<!-- Kinda Hacky Click Interception --> 
<controls:KeypadButton x:Name="testBtn" Text="Hello"> 
     <i:Interaction.Triggers> 
      <i:EventTrigger EventName="MouseLeftButtonDown"> 
       <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding PressStandardKeyCommand}" /> 
      </i:EventTrigger> 
     </i:Interaction.Triggers> 
</controls:KeypadButton> 

你可以考虑另一种选择是从Button而不是UserControl继承。 Silverlight Show有一个article about inheriting from a TextBox,这可能与此相关。

+0

+1,特别是关于从你需要的东西继承的一点,而不是从Control/UserControl开始 - 除非绝对需要。 – 2011-03-10 19:40:04

+0

@亚历克斯:谢谢,但我相信我们错过了这里的观点。另外,我已经尝试过使用MouseLeftButtonUp(应该是相同的),它不起作用。 KeypadButton的事件已经生成,我可以从代码中以编程方式订阅它。问题似乎是EventTrigger无法捕获生成的事件。我在这里发布了这个问题,以了解silvelright usercontrols中的自定义事件是否需要以某种特定方式实现,以便在XAML和EventToCommand中工作。 – 2011-03-12 08:55:55