Galasoft RelayCommand 未触发

发布于 2024-10-21 15:23:40 字数 1699 浏览 0 评论 0原文

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

ShellView 包含一个键盘(自定义用户控件),其中包含多个键盘按钮(自定义用户控件)。

我非常确定(因为我已经检查过)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 的主体中设置断点,我可以看到Click 被正确捕获,并且它包含指向RelayCommand 的点。但是,什么也没有发生:“Click(sender, new KeypadButtonEventArgs());”已被执行,但仅此而已。

为什么会这样?不应该执行 RelayCommand 中定义的目标函数吗?也许是与范围相关的问题?

提前致谢, 干杯, 吉安卢卡.

I am using the MVVM Light framework to build a SL4 application. My simple app is composed primarily by a single main view (shellView), which is divided into multiple UserControls. They are just a convenient separation of the UI, therefore they don't have their own ViewModel.

The ShellView contains a Keypad (custom usercontrol) that contains multiple KeypadButtons (custom usercontrols).

I am quite sure (because I've checked) that the DataContext is set properly and it is used by all usercontrols in the hierarchy. (ShellView's Datacontext is ShellViewModel, Keypad's DataContext is ShellViewModel, etc.).

In the ShellViewModel I have a ICommand (RelayCommand) that is named "ProcessKey".

In the Keypad control, I have something like:

<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>

The KeypadButton is basically a Grid that contains a Button. The MouseLeftButtonUp event is caught and a custom "Click" event is fired. Let me show you some code to explain easily what I am doing:

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; }
}

Now, if I set a breakpoint to the body of innerButton_Click, I can see the Click is properly caught and it contains points to the RelayCommand. However, nothing happens: "Click(sender, new KeypadButtonEventArgs());" is executed but nothing more.

Why is this behaving so? Shouldnt execute the target function that is defined in the RelayCommand? Is maybe a scope-related issue?

Thanks in advance,
Cheers,
Gianluca.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

分开我的手 2024-10-28 15:23:40

正如其他评论所指出的,这可能与 Click 事件不是 RoatedEvent 有关。

作为一种快速技巧,您可以使用 MouseLeftButtonDown 而不是 UserControl 上的 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 有一篇有关从 TextBox 继承的文章< /a> 这可能与此相关。

As noted by other comments, this is probably related to the Click event not being a RoutedEvent.

As a quick hack you might be able to use MouseLeftButtonDown instead of the Click event on your UserControl.

<!-- 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>

Another option you could consider is inheriting from Button instead of UserControl. Silverlight Show has an article about inheriting from a TextBox that probably is relevant for this.

放我走吧 2024-10-28 15:23:40

路由事件应如下定义(请参阅文档):

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); }
}

Routed events should be defined like this (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); }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文