无法在WPF应用程序中创建自定义命令

时间:2014-03-16 15:28:15

标签: c# .net wpf xaml xamlparseexception

概述

我有一个用C#.NET 4.0编写的WPF应用程序。这个项目中有很多按钮。目前,按钮单击事件的每个EventHandler都使用不同的值参数调用相同的方法。由于这一切都是在C#代码中完成的,因此它非常笨拙并且涉及大量复制/粘贴。

我更倾向于使用Command而不是一堆EventHandler,因为它可以让我的代码更清晰。

问题

无论我做什么,我都无法让XAML接受新命令:

<Window.CommandBindings>
    <CommandBinding Command="ButtonClick" Executed="ButtonClick_Executed" />
</Window.CommandBindings>

上面的代码有错误:&#34; CommandConverter无法从System.String&#34;转换。 Command="ButtonClick"上存在此错误。

研究/尝试

我已经尝试了所有我能想到的事情来使这项工作无济于事。我查看了至少30篇不同的博客文章/教程/等。如何正确地做到这一点,所以我没有适合你的实际清单。我看到有些人使用Binding关键字,所以我尝试了Command="{Binding ButtonClick}",但显然在CommandBinding部分中不允许这样做。我也试过Command="{x:Static ButtonClick}",但这也不起作用。

至于&#34; ButtonClick&#34;本身,我在那里尝试了几种可能的价值观。 &#34; ButtonClick&#34;是一个任意文本字符串,不对应任何类/方法/变量。我对这个参数的原始理解是,该值只是一个识别字符串。我也尝试在同一个类中使用实际方法&#34; ButtonClick_Executed&#34;。我还尝试使用实现ICommand(命令)的类名,以及该类中的方法(Commands.ButtonClick_Executed)。

作为参考,我也尝试按照WPF: Binding to commands in code behind在代码隐藏中执行此操作,但我也无法做到这一点。

代码

代码缩略为相关内容。据我所知,Button的Command参数需要与CommandBinding的Command值匹配。我还没有添加它,因为我甚至无法使CommandBinding工作。所有代码都在同一个命名空间中,在一个项目中。

MainWindow.xaml:

<Window x:Class="T9Messager.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="600" Width="540">
    <Window.CommandBindings>
        <CommandBinding Command="ButtonClick" Executed="ButtonClick_Executed" />
    </Window.CommandBindings>
    <Grid Margin="0,0,0,0">
        <Button x:Name="Button1" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="165" Height="113">
            <TextBlock HorizontalAlignment="Center" TextAlignment="Center" FontSize="18" FontWeight="Bold">1</TextBlock>
        </Button>
        ... More buttons ...
    </Grid>
</Window>

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;
    }
}

Commands.cs:

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;

}

非常感谢任何和所有帮助。如果您需要任何其他信息,请与我们联系。

更新 我尝试过Herdo提出的答案。我得到的新错误是The namespace prefix "T9Messager" is not defined。经过研究,我的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参数。 Command="T9Messager:Commands.ButtonClick"会创建错误Value cannot be null. Parameter name: value

1 个答案:

答案 0 :(得分:4)

ButtonClickString

<CommandBinding Command="ButtonClick" Executed="ButtonClick_Executed" />

您需要将其编写为类的成员(请注意,您需要在窗口声明中添加命名空间)

<Window xmlns:t9m="clr-namespace:T9Messager"
        ...>

<CommandBinding Command="t9m:Commands.ButtonClick"
                Executed="ButtonClick_Executed" />