Проблема триггера события при анализе xaml с использованием MVVMLight в моем приложении WP7

Я немного новичок, когда дело доходит до MVVM и С# в целом, но я не понимаю, почему я получаю следующее исключение синтаксического анализа 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".

Кто-нибудь знает, почему это происходит? Я видел, как это использовалось раньше, и я просто слишком неопытен, чтобы понять, почему это не работает для меня.

Я обязан за любую помощь, и спасибо за ваше время.


person Dane    schedule 08.04.2011    source источник


Ответы (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>
person Dane    schedule 14.04.2011