开发者

Event trigger issue when parsing xaml using MVVMLight in my WP7 app

I am a bit of a novice when it comes to MVVM and C# in general, but I do not understand why I am getting the following xaml parse exception: AG_E_PARSER_BAD_TYPE

The exception occurs when attempting to parse my event trigger:

    <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">

...and inside my grid:

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

The exception occurs at the i:EventTrigger EventName="Click" line.

Does anyone have any insight as to why this is happening? I have seen this used before, and am simply too inexperienced to discern why it isn't working for开发者_StackOverflow社区 me.

I am obliged for any help, and thank you for your time.


I did not resolve this issue, but created a work around... I thought it might be helpful to some, so here it is:

I extended the button class by adding a command property to my new "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);
    }
}

Following this, I just bind a command in my xaml:

<b:BindableButton x:Name="LoginButton" Command="{Binding LoginCommand}"></b:BindableButton>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜