Mvvmcross 6.2.3 ViewModel导航的奇怪行为

时间:2019-03-26 09:37:59

标签: mvvmcross

我正在使用MVVMCROSS 6.2.3,并且在XForms UWP应用中存在嵌套导航问题。

这里是一个描述: VM1导航到VM2, VM1正在等待VM2的结果

VM2导航到VM3,VM2不需要VM3的结果。 从VM2导航到VM3和VM1后,意外得到空结果。

public class MainViewModel : MvxViewModel
{
    public IMvxAsyncCommand GoToSecondPageCommand =>
        new MvxAsyncCommand(async () =>
        {
            var param = new Dictionary<string, string> { { "ButtonText", ButtonText } };

            var result = await navigationService.Navigate<SecondViewModel, Dictionary<string, string>, SecondViewModelResult>(param);
            var breakpoint = 1;


            //HERE IS THE ISSUE, result IS NULL because it fires immediately after Navigate<ThirdViewModel>()


        });

    public IMvxAsyncCommand BackCommand => new MvxAsyncCommand(async () =>
    {
        await navigationService.Close(this, new SecondViewModelResult { Result = "Back button clicked" });
    });
}

public class SecondViewModel : MvxViewModel<Dictionary<string, string>, SecondViewModelResult>
{
    public IMvxAsyncCommand GoToThirdPageCommand =>
        new MvxAsyncCommand(async () =>
        {
            var t = await navigationService.Navigate<ThirdViewModel>();
            var breakpoint = 2;
        });
}

public class ThirdViewModel : MvxViewModel
{
}

您可以从我的GitHub https://github.com/JTOne123/XamFormsMvxTemplate

自己运行它

此行为在5.7版中是好的,并且 VM1正在等待VM2返回结果

我在做什么错了?

1 个答案:

答案 0 :(得分:-1)

await navigationService.Navigate<ThirdViewModel>();中调用GoToThirdPageCommand之前,您可以通过调用以下命令来关闭当前SecondViewModelawait navigationService.Close(this, new SecondViewModelResult { Result = "Third page command clicked" });

这将返回第二个参数作为结果。

所以你会得到这样的东西:

public IMvxAsyncCommand GoToThirdPageCommand =>
    new MvxAsyncCommand(async () =>
    {
        await navigationService.Close(this, new SecondViewModelResult { Result = "Third page command clicked" });
        await navigationService.Navigate<ThirdViewModel>();
    });