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 に答える