C# 无法在WPF应用程序中创建自定义命令
概述 我有一个用C.NET4.0编写的WPF应用程序。此项目中有许多按钮。当前,按钮单击事件的每个EventHandler都使用不同的值参数调用相同的方法。因为这都是用C代码完成的,所以它相当笨拙,并且需要大量的复制/粘贴 我更愿意使用一个命令,而不是一堆事件处理程序,因为它使我的代码更干净 问题 无论我做什么,我都无法让XAML接受新命令:C# 无法在WPF应用程序中创建自定义命令,c#,.net,wpf,xaml,xamlparseexception,C#,.net,Wpf,Xaml,Xamlparseexception,概述 我有一个用C.NET4.0编写的WPF应用程序。此项目中有许多按钮。当前,按钮单击事件的每个EventHandler都使用不同的值参数调用相同的方法。因为这都是用C代码完成的,所以它相当笨拙,并且需要大量的复制/粘贴 我更愿意使用一个命令,而不是一堆事件处理程序,因为它使我的代码更干净 问题 无论我做什么,我都无法让XAML接受新命令: <Window.CommandBindings> <CommandBinding Command="ButtonClick" E
<Window.CommandBindings>
<CommandBinding Command="ButtonClick" Executed="ButtonClick_Executed" />
</Window.CommandBindings>
MainWindow.xaml.cs:
public partial class MainWindow : Window, IObserver<Model>
{
private Controller controller;
private IDisposable Unsubscriber;
public MainWindow()
{
Model model = new Model();
Controller controller = new Controller(model);
this.controller = controller; // MainWindow view = new MainWindow(c);
Unsubscriber = model.Subscribe(this);
InitializeComponent();
}
// This is the method I want to run
public void ButtonClick_Executed(object sender, EventArgs ev)
{
Console.WriteLine("Command Executed");
}
// I want to avoid having a bunch of these
private void Button_Click_1(object sender, RoutedEventArgs e)
{
controller.HandleButtonClick('1');
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
controller.HandleButtonClick('2');
}
... More ButtonClick Events ...
public void OnCompleted()
{
Unsubscriber.Dispose();
}
public void OnError(Exception error)
{
throw new NotImplementedException();
}
public void OnNext(Model value)
{
tb_text.Text = value.text;
}
}
非常感谢您的任何帮助。如果您需要任何其他信息,请告诉我
更新
我已经尝试了赫尔多提出的答案。我得到的新错误是未定义命名空间前缀T9Messager。经过研究,我的XAML现在是这样打开的:
<Window x:Class="T9Messager.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:T9Messager="clr-namespace:T9Messager"
Title="MainWindow" Height="600" Width="540">
这似乎解决了这个问题,但现在看来XAML完全忽略了命令参数。Command=T9Messager:Commands.Button单击创建的错误值不能为空。参数名称:value 此按钮单击是一个字符串:
您需要将其作为类的成员编写注意,您需要在窗口声明中添加名称空间:
此按钮是一个字符串:
您需要将其作为类的成员编写注意,您需要在窗口声明中添加名称空间:
我试过了。名称空间是T9Messager。我收到一个错误,名称空间前缀T9Messager未定义。我以前见过,但我只是有个想法。我是否需要在XAML中指定该前缀?我本以为它会从相应的C代码中推断出来,但也许不是?@RussellUhl嗯,它必须在XAML中定义,是的。我不熟悉XAML这是我第一次使用它,所以我不确定。在任何情况下,请查看问题的更新。就是这样!谢谢我试过了。名称空间是T9Messager。我收到一个错误,名称空间前缀T9Messager未定义。我以前见过,但我只是有个想法。我是否需要在XAML中指定该前缀?我本以为它会从相应的C代码中推断出来,但也许不是?@RussellUhl嗯,它必须在XAML中定义,是的。我不熟悉XAML这是我第一次使用它,所以我不确定。在任何情况下,请查看问题的更新。就是这样!谢谢
public class Commands : ICommand
{
public Commands()
{
CommandBinding ButtonClickBinding = new CommandBinding(ButtonClickCommand);
CommandManager.RegisterClassCommandBinding(typeof(Commands), ButtonClickBinding);
}
private RoutedUICommand ButtonClick = new RoutedUICommand("ButtonClick", "ButtonClick", typeof(Commands));
public RoutedCommand ButtonClickCommand
{
get { return ButtonClick; }
}
// Getting any of the following 3 methods to execute would be fine
public static void test()
{
}
public void Execute(object parameter)
{
ButtonClick_Executed(null, null);
}
public void ButtonClick_Executed(object sender, EventArgs ev)
{
}
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
}
<Window x:Class="T9Messager.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:T9Messager="clr-namespace:T9Messager"
Title="MainWindow" Height="600" Width="540">
<CommandBinding Command="ButtonClick" Executed="ButtonClick_Executed" />
<Window xmlns:t9m="clr-namespace:T9Messager"
...>
<CommandBinding Command="t9m:Commands.ButtonClick"
Executed="ButtonClick_Executed" />