如何在不同的控件中使用相同的 RoutedCommand?
我在不同的文件中有 2 个不同的 UI 元素:一个 menuItem (在 window.xaml 中)和一个 userControl (在 foo.xaml 中)。
两者具有完全相同的逻辑:仅当条件匹配并调用相同的方法时才启用它们。 为了编写此逻辑,我使用RoatedCommands。
我可以在每个文件中使用 2 个不同的 RoutedCommands,在 CanExecute 和 Executed 中具有相同的条件。 有用。
我想改进代码以遵循 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据推测,您的 RoutedCommand 是类的静态成员(例如 Commands)。 您需要将该
static
成员传递到Command
属性中:Presumably your
RoutedCommand
is astatic
member on a class (for example,Commands
). You need to pass thatstatic
member into theCommand
property:我自己回答:
由于编译器不接受带有 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!