简体   繁体   English

为什么第二次导航到ViewModel时未调用Init()方法?

[英]Why Init() method is not called when I navigate to the ViewModel second time?

I develop Win 8.1 application using MvvmCross 3.5.1. 我使用MvvmCross 3.5.1开发Win 8.1应用程序。 The user sequentially goes through the some views and returns to the first view from the last view. 用户依次浏览一些视图并从最后一个视图返回到第一个视图。 Everything works perfect during first iteration of the workflow. 在工作流程的第一次迭代期间,一切工作都非常完美。 But when the user starts the workflow again - Init() methods in viewmodels are not called. 但是,当用户再次启动工作流程时,不会调用视图模型中的Init()方法。

For example, interaction between FirstViewModel and SecondViewModel looks like below. 例如,FirstViewModel和SecondViewModel之间的交互如下所示。

FirstViewModel: FirstViewModel:

ShowViewModel<SecondViewModel>(
    new
    {
        code = ItemCode,
        descr = ItemDescription
    });

SecondViewModel: SecondViewModel:

public void Init(string code, string descr)
{
    ...
}

So simple but works only one time :( 如此简单,但只能使用一次:(

What reasons may entail such behavior? 什么原因可能导致这种行为?


As workaround I tried to load viewmodel "manually": 解决方法中,我尝试“手动”加载视图模型:

var d = new Dictionary<string, string>
{
    {"code", ItemCode},
    {"descr", ItemDescription}
};

var b = new MvxBundle(d);

var r = new MvxViewModelRequest<SecondViewModel>(b, null, null);

var m = Mvx.Resolve<IMvxViewModelLoader>().LoadViewModel(r, null);

It solved the problem with Init() methods calling. 它通过Init()方法调用解决了该问题。 But I don't know how to show the viewmodel using the m variable. 但是我不知道如何使用m变量显示视图模型。 Anyone knows? 有谁知道?


Apologies for my poor english and thanks in advance! 抱歉我的英语不好,谢谢!

Init() is only being called once, because Windows 8.1 apps cache pages. Init()仅被调用一次,因为Windows 8.1应用程序缓存页面。 Hence, the ViewModel for that page is not ever destroyed and hence the Init() method is not called again. 因此,该页面的ViewModel不会被销毁,因此不会再次调用Init()方法。

You can make your own BasePage which overrides this behavior by overriding OnNavigatedTo: 您可以通过覆盖OnNavigatedTo来创建自己的BasePage来覆盖此行为:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    if (e.NavigationMode == NavigationMode.New)
        ViewModel = null;

    base.OnNavigatedTo(e);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 多次调用 HttpModule Init 方法 - 为什么? - HttpModule Init method is called several times - why? 更新方法第一次分配该值,但是第二次调用更新方法时,该值将丢失 - Update method assigns the value first time but when the update method is called second time, that value is lost 当我调用方法时,为什么不调用覆盖的方法? - When I call a method, why isn't the overridden method called? WebClient-第二次调用DownloadFileAsync不起作用 - WebClient - DownloadFileAsync not working when called second time C# - 当使用Moq第二次调用时,模拟一个返回不同值的方法 - C# – Mocking a method to return a different value when called a second time using Moq 当IsInputKey设置为true时,为什么第二次调用PreviewKeyDown事件? - Why does the PreviewKeyDown event get called a second time when IsInputKey is set true? C# 方法或 Object 不能第二次调用 - C# Method or Object cannot be called second time 为什么第二次不调用该函数? - Why doesn't the function get called the second time? 为什么此WMI查询在第二次调用时挂起? - Why is this WMI query hanging on the second time it's called? 运行计时器时,在意外时间调用了方法 - A method was called at an unexpected time, when running a timer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM