在我的WP7应用程序中使用MVVMLight解析xaml时的事件触发器问题

时间:2011-04-08 19:20:25

标签: c# windows-phone-7 mvvm-light xamlparseexception

对于一般的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”行。

有没有人知道为什么会这样?我之前看到过这种情况,而且根本没有经验可以辨别出它为什么不适合我。

我有义务提供任何帮助,谢谢你的时间。

1 个答案:

答案 0 :(得分: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>