Ошибка триггера события при разборе xaml с использованием MVVMLight в моем приложении WP7 - PullRequest
0 голосов
/ 08 апреля 2011

Я немного новичок, когда дело доходит до 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 Ответ

1 голос
/ 14 апреля 2011

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

Я расширил класс кнопки, добавив свойство команды к моемуновый "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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...