【发布时间】:2020-08-02 08:22:07
【问题描述】:
我已经阅读了许多关于绑定和命令的帖子,但我很难得到我想要的工作。
下面的工作正常
public partial class TailoredReading : Window
{
public static RoutedUICommand myRoutingCommand = new RoutedUICommand("myCommand", "myCommand", typeof(InputGestureWindow));
public TailoredReading()
{
InitializeComponent();
}
private void SaveResource_Click(object sender, RoutedEventArgs e)
{
//ViewModel.SaveResource();
}
void myRoutingCommandExecuted(object target, ExecutedRoutedEventArgs e)
{
String command = ((RoutedCommand)e.Command).Name;
MessageBox.Show("The \"" + command + "\" command has been invoked NOW. ");
}
void myRoutingCommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
}
<Window x:Class="ESL_Master_Suite.Components.Core.Resources.TailoredReading"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:this="clr-namespace:ESL_Master_Suite.Components.Core.Resources"
xmlns:this1="clr-namespace:ESL_Master_Suite.Components.Controls"
xmlns:local="clr-namespace:ESL_Master_Suite.Components.Core.Resources"
mc:Ignorable="d"
Title="TailoredReading" WindowStartupLocation="CenterScreen" Width="1024">
<Window.DataContext>
<this:ViewModel />
</Window.DataContext>
<Window.InputBindings>
<KeyBinding Command="{x:Static this:TailoredReading.myRoutingCommand}" Key="F1" />
</Window.InputBindings>
<Window.CommandBindings>
<CommandBinding Command="{x:Static this:TailoredReading.myRoutingCommand}" CanExecute="myRoutingCommandCanExecute" Executed="myRoutingCommandExecuted"/>
</Window.CommandBindings>
但是,我想将命令逻辑分开,放在它自己的类中。
public class Commands
{
public static readonly RoutedUICommand myRoutingCommand = new RoutedUICommand("myCommand", "myCommand", typeof(InputGestureWindow));
void myRoutingCommandExecuted(object target, ExecutedRoutedEventArgs e)
{
String command = ((RoutedCommand)e.Command).Name;
MessageBox.Show("The \"" + command + "\" command has been invoked. ");
}
void myRoutingCommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
}
<Window x:Class="ESL_Master_Suite.Components.Core.Resources.TailoredReading"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:this="clr-namespace:ESL_Master_Suite.Components.Core.Resources"
xmlns:this1="clr-namespace:ESL_Master_Suite.Components.Controls"
xmlns:local="clr-namespace:ESL_Master_Suite.Components.Core.Resources"
mc:Ignorable="d"
Title="TailoredReading" WindowStartupLocation="CenterScreen" Width="1024">
<Window.DataContext>
<this:ViewModel />
</Window.DataContext>
<Window.InputBindings>
<KeyBinding Command="{x:Static this:Commands.myRoutingCommand}" Key="F1" />
</Window.InputBindings>
<Window.CommandBindings>
<CommandBinding Command="{x:Static this:Commands.myRoutingCommand}" CanExecute="myRoutingCommandCanExecute" Executed="myRoutingCommandExecuted"/>
</Window.CommandBindings>
当我这样做时,即使在清理和重建之后,我也会收到命令不在命名空间中的错误。即使它位于窗口类的正下方。
有什么想法吗?
保罗
【问题讨论】:
-
为窗口发布 XAML 命名空间。
-
要么问题标题错误,要么这是一个非常奇怪的实现。这是一个单独的静态类,它不是视图模型。但是,如果您正在尝试 Henrik 的代码,它应该是 local:Commands.MyCommand 假设您的代码与他的代码匹配,那就是。除非这仅特定于该窗口,否则您可能希望 Window 而不是 TailoredReading 作为命令绑定将与之关联的类型。如果它是特定于窗口的,那么将您的事件处理程序放在一个单独的类中似乎很奇怪。
-
对不起。我不确定它应该如何设置。我有上面的窗口。我有一个 ViewModel 类,其中包含 UI 的所有数据。最初,我想将命令逻辑添加到 View Model 类中,但后来我想为什么不将它单独放在它自己的类中。我不知道哪种方式是正确的或最好的方式。
标签: c# wpf routed-commands