MVVMcross 命令绑定触发异常

MVVMcross Command Binding fire Exception

我在尝试使用命令打开第二个视图模型时遇到问题 V4.0.Beta5。 我遵循了 N+1 视频系列 https://www.youtube.com/playlist?list=PLR6WI6W1JdeYSXLbm58jwAKYT7RQR31-W

中描述的示例

第一个视图模型

public class FirstViewModel
    : MvxViewModel
{
    private string _hello = "Hello MvvmCross";

    public string Hello
    {
        get { return _hello; }
        set
        {
            _hello = value;
            RaisePropertyChanged(() => Hello);
        }
    }

    private Cirrious.MvvmCross.ViewModels.MvxCommand _goSecondViewCommand;

    public System.Windows.Input.ICommand GoSecondViewCommand
    {
        get
        {
            _goSecondViewCommand = _goSecondViewCommand ??
                                   new Cirrious.MvvmCross.ViewModels.MvxCommand(DoGoSecondView);
            return _goSecondViewCommand;
        }
    }

    private void DoGoSecondView()
    {
        base.ShowViewModel<SecondViewModel>();
    }
}

第二视图模型

public class SecondViewModel :MvxViewModel
{
    private string _hello2 = "Hello2 MvvmCross";

    public string Hello2
    {
        get { return _hello2; }
        set
        {
            _hello2 = value;
            RaisePropertyChanged(() => Hello2);
        }
    }
}

第一次观看

<views:MvxWindowsPage
x:Class="TestCommand.UWP.Views.FirstView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TestCommand.UWP"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:views="using:Cirrious.MvvmCross.WindowsUWP.Views"
mc:Ignorable="d">

<Grid>
    <TextBox x:Name="textBox" HorizontalAlignment="Left" Margin="70,92,0,0" TextWrapping="Wrap" Text="FirstView" VerticalAlignment="Top" Width="223"/>
    <Button x:Name="button" Command="{Binding GoSecondViewCommand}" Content="Button" HorizontalAlignment="Left" Height="108" Margin="70,346,0,0" VerticalAlignment="Top" Width="223"/>
</Grid>

第二个视图

<views:MvxWindowsPage
x:Class="TestCommand.UWP.Views.SecondView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TestCommand.UWP.Views"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:views="using:Cirrious.MvvmCross.WindowsUWP.Views"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">

<Grid />

设置 Class:

    public class Setup : MvxWindowsSetup
{
    public Setup(Frame rootFrame) : base(rootFrame)
    {
    }

    protected override IMvxApplication CreateApp()
    {
        return new TestCommand.Core.App();
    }
}

如果需要,可以在此处下载解决方案: https://onedrive.live.com/redir?resid=A5D9789788DE33CB!36079&authkey=!AKs9nsG28iI6nQQ&ithint=file%2czip.

可能的原因是您没有在您的 UWP 应用程序中正确使用安装程序,以下是我为完成此工作所做的工作:

1) 在 UWP 应用程序中创建两个 ViewModel:FirstViewModel 和 SecondViewModel

2)Setup.cs 文件中创建设置 class:

public class Setup : MvxWindowsSetup
{
        public Setup(Frame rootFrame) : base(rootFrame)
        {
        }

        protected override IMvxApplication CreateApp()
        {
            return new AppSetup();
        }
}

public class AppSetup : MvxApplication
{
        public override void Initialize()
        {
            RegisterAppStart<FirstViewModel>();
        }
}

3) FirstView.xaml:

<StackPanel>
        <TextBox x:Name="textBox" HorizontalAlignment="Left" Margin="70,92,0,0" TextWrapping="Wrap" Text="FirstView" VerticalAlignment="Top" Width="223"/>
        <TextBlock Height="50" Text="{Binding Hello}" />
        <Button x:Name="button" Command="{Binding GoSecondViewCommand}" Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="223" Height="50" />
    </StackPanel>

4) SecondView.xaml:

<StackPanel>
        <TextBox x:Name="textBox" HorizontalAlignment="Left" Margin="70,92,0,0" TextWrapping="Wrap" Text="SecondView" VerticalAlignment="Top" Width="223"/>
        <TextBlock Height="50" Text="{Binding Hello2}" />
    </StackPanel>

5)App.xaml.cs 文件中,在 OnLaunched 方法中进行以下更改:

protected override void OnLaunched(LaunchActivatedEventArgs e)
{

......

            if (rootFrame.Content == null)
            {
                var setup = new Setup(rootFrame);
                setup.Initialize();

                var start = Mvx.Resolve<IMvxAppStart>();
                start.Start();
            }
            // Ensure the current window is active
            Window.Current.Activate();
}

对了,MvvmCross版本是3.5.1

检查 Github

上的已完成样本

更新 OnNavigationFailed 方法中的异常:

请在FirstView和SecondView后面的代码中注释这一行:

ViewModel = new FirstViewModel();

ViewModel = new SecondViewModel();

MvvmCross 已自动设置 ViewModel。