0

一般来说,对于 MVVM 和 C#,我有点新手,但我不明白为什么会出现以下 xaml 解析异常:AG_E_PARSER_BAD_TYPE

尝试解析我的事件触发器时发生异常:

    <applicationspace:AnViewBase
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:c="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WP7">

...在我的网格内:

            <Button Name="LoginButton"
                Content="Login"
                Height="72"
                HorizontalAlignment="Left"
                Margin="150,229,0,0"
                VerticalAlignment="Top"
                Width="160">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Click">
                    <c:EventToCommand Command="{Binding LoginCommand}" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </Button>

异常发生在i:EventTrigger EventName="Click"行。

有没有人知道为什么会这样?我以前见过这种用法,而且经验不足,无法辨别为什么它对我不起作用。

我有义务提供任何帮助,并感谢您的宝贵时间。

4

1 回答 1

1

我没有解决这个问题,但创造了一个解决方法......我认为它可能对某些人有帮助,所以这里是:

我通过向我的新“BindableButton”添加命令属性来扩展按钮类

public class BindableButton : Button
{
    public BindableButton()
    {
        Click += (sender, e) =>
            {
                if (Command != null && Command.CanExecute(CommandParameter))
                    Command.Execute(CommandParameter);
            };
    }

    public ICommand Command
    {
        get { return (ICommand)GetValue(CommandProperty); }
        set { SetValue(CommandProperty, value); }
    }

    public static DependencyProperty CommandProperty =
        DependencyProperty.Register("Command", typeof(ICommand), typeof(BindableButton), new PropertyMetadata(null, CommandChanged));

    public object CommandParameter
    {
        get { return GetValue(CommandParameterProperty); }
        set { SetValue(CommandParameterProperty, value); }
    }

    public static DependencyProperty CommandParameterProperty =
        DependencyProperty.Register("CommandParameter", typeof(object), typeof(BindableButton), new PropertyMetadata(null));

    private static void CommandChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
    {
        BindableButton button = source as BindableButton;

        button.RegisterCommand((ICommand)e.OldValue, (ICommand)e.NewValue);
    }

    private void RegisterCommand(ICommand oldCommand, ICommand newCommand)
    {
        if (oldCommand != null)
            oldCommand.CanExecuteChanged -= HandleCanExecuteChanged;

        if (newCommand != null)
            newCommand.CanExecuteChanged += HandleCanExecuteChanged;

        HandleCanExecuteChanged(newCommand, EventArgs.Empty);
    }

    // Disable button if the command cannot execute
    private void HandleCanExecuteChanged(object sender, EventArgs e)
    {
        if (Command != null)
            IsEnabled = Command.CanExecute(CommandParameter);
    }
}

在此之后,我只是在我的 xaml 中绑定一个命令:

<b:BindableButton x:Name="LoginButton" Command="{Binding LoginCommand}"></b:BindableButton>
于 2011-04-14T14:19:23.867 回答