如何在不同的控件中使用相同的 RoutedCommand?

发布于 2024-07-26 10:36:58 字数 1664 浏览 14 评论 0原文

我在不同的文件中有 2 个不同的 UI 元素:一个 menuItem (在 window.xaml 中)和一个 userControl (在 foo.xaml 中)。

两者具有完全相同的逻辑:仅当条件匹配并调用相同的方法时才启用它们。 为了编写此逻辑,我使用RoatedCommands

我可以在每个文件中使用 2 个不同的 RoutedCommands,在 CanExecuteExecuted 中具有相同的条件。 有用。

我想改进代码以遵循 DRY 规则并将routedCommand 和附加方法外部化。 这是两个文件中的绑定:

<CommandBinding Command="{x:Static RoutedCommands:TestRoutedCommand.test}"
                    Executed="RoutedCommands:TestRoutedCommand.OnTest"
                    CanExecute="RoutedCommands:TestRoutedCommand.CanTest" />

这是 TestRoulatedCmmand.cs :

public class TestRoutedCommand
{
    public static RoutedCommand test = new RoutedCommand();

    public static void OnTest(object sender, ExecutedRoutedEventArgs e)
    {
        System.Windows.MessageBox.Show("test -on");
    }

    public static void CanTest(object sender, CanExecuteRoutedEventArgs e)
    {
        System.Windows.MessageBox.Show("test -can");

        e.CanExecute = true;
    }
}

当我这样做时,编译器报告此错误:(

Error   1   unknown generation error, 'MC6005 :        
Executed="RoutedCommands:TestRoutedCommand.OnTest" is not valid.  
'RoutedCommands:TestRoutedCommand.OnTest' is not a valid name for an event handler method. 
Only instance methods of the generated class or code-behind are valid

翻译可能不准确,因为原始消息不是英文)

似乎不支持外部方法,但我确信我没有错过任何东西。 我想知道我的方法是否是静态的?

是否有另一种方法可以外部化我的方法,或者我是否必须继续使用这 4 种方法,仅调用另外 2 种方法?

注意:在我的项目中,有超过 30 个重复的 routerdCommand 方法,仅调用另外 2 个方法...这就是它让我如此困扰的原因。

谢谢。

I a have 2 distinct UI elements in distinct files : a menuItem (in window.xaml) and a userControl (in foo.xaml).

Both have exactly the same logic : they are enabled only if a condition is matched and call the same method. To code this logic, i use RoutedCommands.

I could use 2 different RoutedCommands in each file with same condition in the CanExecute and Executed. It works.

I would like to improve the code to follow the D.R.Y. rule and externalize the routedCommand and the attached methods. This is the binding in both files :

<CommandBinding Command="{x:Static RoutedCommands:TestRoutedCommand.test}"
                    Executed="RoutedCommands:TestRoutedCommand.OnTest"
                    CanExecute="RoutedCommands:TestRoutedCommand.CanTest" />

this is TestRoutedCmmand.cs :

public class TestRoutedCommand
{
    public static RoutedCommand test = new RoutedCommand();

    public static void OnTest(object sender, ExecutedRoutedEventArgs e)
    {
        System.Windows.MessageBox.Show("test -on");
    }

    public static void CanTest(object sender, CanExecuteRoutedEventArgs e)
    {
        System.Windows.MessageBox.Show("test -can");

        e.CanExecute = true;
    }
}

When I do that, the compiler report this error :

Error   1   unknown generation error, 'MC6005 :        
Executed="RoutedCommands:TestRoutedCommand.OnTest" is not valid.  
'RoutedCommands:TestRoutedCommand.OnTest' is not a valid name for an event handler method. 
Only instance methods of the generated class or code-behind are valid

(The translation may not be exact because the original message was not in English)

It seems that external methods are not supported, but i would be sure i did not miss something. I wonder if it is the fact that my methods are static ?

Is there another way to externalize my methods or do i have to stay with these 4 methods calling only 2 others ?

Note : in my project this is more 30 duplicated routedCommand methods callings only 2 others... this is why it bother me so much.

Thanks.

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

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

发布评论

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

评论(2

2024-08-02 10:36:58

据推测,您的 RoutedCommand 是类的静态成员(例如 Commands)。 您需要将该 static 成员传递到 Command 属性中:

<CommandBinding Command="{x:Static local:Commands.TestCommand}"
                    Executed="RoutedCommands.TestRoutedCommand.OnTest"
                    CanExecute="RoutedCommands.TestRoutedCommand.CanTest" />

Presumably your RoutedCommand is a static member on a class (for example, Commands). You need to pass that static member into the Command property:

<CommandBinding Command="{x:Static local:Commands.TestCommand}"
                    Executed="RoutedCommands.TestRoutedCommand.OnTest"
                    CanExecute="RoutedCommands.TestRoutedCommand.CanTest" />
红衣飘飘貌似仙 2024-08-02 10:36:58

我自己回答:

由于编译器不接受带有 RoutedCommand 的外部委托,因此最佳实践是使用 ICommand 的自定义实现,例如 这些。 不再需要 CommandBinding,问题就解决了!

I answer myself :

As the compiler does not accept extern delegates with RoutedCommand, the best practice is to use a custom implementation of ICommand like these. No more CommandBinding is required and the problem is solved!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文