【发布时间】:2021-09-10 08:51:08
【问题描述】:
我即将学习 WPF 中的 CommandBindings,但不知何故我无法创建自定义命令。
显示以下错误:(XDG0008) 名称空间“clr-namespace:CommandBindingWPF”中不存在名称“CustomCommands”
<Window.CommandBindings>
<CommandBinding Command="local:CustomCommands.Exit" CanExecute="ExitCommand_CanExecute" Executed="ExitCommand_Executed" />
</Window.CommandBindings>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = viewmodel;
}
//more code but not relevant for question
private void ExitCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
private void ExitCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
Application.Current.Shutdown();
}
}
public static class CustomCommands
{
public static readonly RoutedUICommand Exit = new RoutedUICommand
(
"Exit",
"Exit",
typeof(CustomCommands)
);
//Define more commands here, just like the one above
}
我什至尝试从https://wpf-tutorial.com/commands/implementing-custom-commands/ 复制粘贴它并粘贴CanExecute 和Executed 只是为了在MainWindow-class 中进行测试(当然我更改了xmlns:self),但它仍然可以不行。有人可以帮我吗?这让我发疯了。
【问题讨论】: