I originally wanted to update an article at noon. As a result, it was more than half past three P.M.. I had been plagued by a small problem for a long time. I finally solved it through hard work (well, it should be solved, and it will take more time to test ).
For specific problems, I need to obtain the GPS location information of the mobile phone on the homepage of my application, convert it to a geographical location through the Network API, and then bind this value to a control to display it. It's very simple, isn't it, So I directly called these required methods in the loaded on the homepage. In fact, writing this function won't use too much code, but after I finish writing it, I find that, my program becomes very unstable. In 80% cases, it is good, and sometimes the interface gets stuck. The specific manifestation is that after you exit the program, you can re-open it. Sometimes the interface is stuck until the system forcibly ends my program. Sometimes, you can connect to the real machine in vs for debugging, And the debugging will not move on the homepage.
Some friends should say: are you not using multithreading? If the request GPS and request network are all carried out in the UI thread, it must be stuck.
I'm sure I have manually written a new process. I also tried to use the breakpoint to track where the debugging was going wrong, but I was never able to get it.
As there is no abnormal response in the case of close to 80%, the priority of handling this problem is not high, but I know that such a bug is always uncomfortable. It is indeed not refreshing.
Until an hour ago, I suddenly came up with a solution:
I moved all the things I did in loaded to onnavigatedto TO DO IT. After testing several times, they all performed normally (if not unexpected, I believe I have found the correct solution ).
I have talked a lot about the above. I will review and introduce the subject: loaded vs onnavigatedto.
Friends who are doing winform or ASP. NET are generally used to doing things in loaded when dealing with page initialization. Of course, I am no exception. However, in WP7, you need to pay attention to it. Now I have analyzed the problem I encountered. I vaguely think that something written in load may conflict with the data binding on the loading interface, or the resource is deadlocked. Of course, I cannot give an accurate judgment on the specific cause. This is just a feeling.
Xxx_loaded and onnavigatedto are both triggered when the page is loaded (I am talking about loading refers to the user, that is, before they see the interface display ), this can be called what they have in common. They use resources on this page together. As for the differences, the xxx_loaded method is not executed every time the page is activated. This is estimated to be scheduled by the system, and the onnavigatedto method is executed every time the page is activated. There is also their order,
I personally think that (for fear of misleading everyone to write "I personally think" and wait for the exact information to be found and then changed) should be before loaded and after onnavigatedto, when loaded is executed, the page is being loaded. When onnavigatedto is executed, the page is fully loaded.
After checking the information, a friend suggested that onnavigatedto be earlier than loaded (thanks to "agan @ net"). He immediately wrote two lines of programs to test and found that this was the case, writing a program cannot depend on your feelings ~ The problem I encountered just now is probably the resource deadlock caused by a thread conflict ("may be"), while onnavigatedto seems to have avoided this situation. After writing the article, I also tried Baidu. I found many friends think that onnavigatedto is better than loaded in the page.
Therefore, it is better to write the value of the variable bound to the data on the interface to onnavigatedto. Of course, if you want to execute it only once, for example, if you only need to obtain the GPS Position on the top, it is also easy:
Private bool _ isfirst = true;
Protected override void onnavigatedto (system. Windows. Navigation. navigationeventargs E)
{
Base. onnavigatedto (E );
If (_ isfirst)
{
// Todo :.......
_ Isfirst = false;
}
}