在应用恢复时,OnNavigatedTo被两次调用两次 [英] OnNavigatedTo is called two twice on app resuming

查看:115
本文介绍了在应用恢复时,OnNavigatedTo被两次调用两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用WP8应用程序时遇到了麻烦.它使用MVVM光源库创建ViewModel,还用于通过Messages进行页面导航.

I'm having trouble with my WP8 app. It uses the MVVM light library for the creation of the ViewModel and is also used for the page navigation with Messages.

现在,我的MainPage的OnNavigatedTo方法遇到了一些问题.如果我是第一次启动该应用程序,它将被调用一次.如果我使用后退按钮关闭并重新打开它,它也只会被调用一次.我的MainPage ViewModel的构造函数也只会被调用一次.

Right now I'm having some problems with the OnNavigatedTo Method of my MainPage. If I start the app for the first time, it is called once. If I close and reopen it with the back button, it is also only called once. The constructor of my MainPage ViewModel will also be called only once.

如果我让应用程序在后台打开并使用开始"或搜索"按钮退出,则OnNavigatedTo将被调用两次!构造函数将不再被调用.

If I let the application open in the background and exit with the Start or Search button, OnNavigatedTo will be called twice! The constructor will not be called any more.

现在的问题是,我正在与服务器同步的OnNavigatedTo中初始化.该操作仅应执行一次,但是现在由于两次调用OnNavigatedTo而有时被调用两次.我对WP8应用程序模型的理解是,当我进入页面时,OnNavigatedTo将仅被调用一次.我无法理解这种行为,这让我发疯.

My problem is now, that I'm initialising in the OnNavigatedTo a synchronsiation with a server. This should only be done once, but now it is sometimes called twice because of the double call of OnNavigatedTo. My understanding of the WP8 app model was that OnNavigatedTo will be only called once when I enter the page. I cannot understand this behaviour and it is driving me nuts.

我不使用FastAppResume,只是希望恢复正常行为.任何建议将不胜感激!我检查了所有可能两次加载页面的调用,但这无济于事.除了这个问题,我的应用程序的行为也应该如此.否则,我正在寻找一种创建异步任务的解决方案,该任务一次只能执行一次.

I'm not using the FastAppResume and just want the normal behaviour back. Any advice would be really appreciated! I've checked all calls where I could have possibly loaded the page twice, but this did not help. Besides this problem, my app behaves like it should. I'm otherwise looking for a solution to create an async task which can be executed only one at a time.

推荐答案

我以前曾发生过这种情况,这是因为消息已被多次注册.

I've had this happen before, it's because the Message was being registered multiple times.

解决此问题的最佳方法是将Messenger.Default.Register添加到ViewModel构造函数中,这样它只能被调用一次

Without seeing your code te best way to fix this is to add the Messenger.Default.Register to your ViewModel constructor so it's only called once

这篇关于在应用恢复时,OnNavigatedTo被两次调用两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆