OnNavigatedTo is called two twice on app resuming

江枫思渺然 提交于 2019-12-11 06:29:31

问题


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.

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.

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.

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.

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.


回答1:


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

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



来源:https://stackoverflow.com/questions/16899509/onnavigatedto-is-called-two-twice-on-app-resuming

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!