简体   繁体   English

WP7退出时的运行方法

[英]WP7 running method on exit

I have a page in my WP7 which contains some text boxes. 我的WP7中有一个页面,其中包含一些文本框。 When pressing the back key this method is run to save the contents: 当按返回键时,将运行此方法来保存内容:

 protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
    {
        var settings = IsolatedStorageSettings.ApplicationSettings;

        settings["remZone"] = txtBoxZone.Text;
        settings["remSpace"] = txtBoxSpace.Text;
    }

The problem is some users would not press the back button, but would press the home button to exit the app, so the contents are not saved. 问题是某些用户不会按“后退”按钮,而是会按“主页”按钮退出应用程序,因此不会保存内容。

I think there are two avenues round this if possible: 1. Is there a function that would run this method when the home button is pressed, like the onBackKeyPress. 我认为,如果可能的话,有两种解决方法:1.是否存在可以在按下主屏幕按钮时运行此方法的函数,例如onBackKeyPress。 2. Is there a simple way to save the contents of the text box as the user is entering it? 2.在用户输入文本框时,是否有一种简单的方法来保存文本框的内容?

Thanks. 谢谢。

EDIT: 编辑:

solution was this: 解决方案是这样的:

   protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
     {
         var settings = IsolatedStorageSettings.ApplicationSettings;

         settings["remZone"] = txtBoxZone.Text;
         settings["remSpace"] = txtBoxSpace.Text;
     }

The simplest solution to your problem would be overriding Page.OnNavigatingFrom method. 解决您的问题的最简单方法是重写Page.OnNavigatingFrom方法。

more here 这里更多

Backpress is not the only event that you receive, there's also an App.Deactivated event, that is guaranteed to be called whenever your app goes off the screen in whatever way. Backpress不是您收到的唯一事件,还有一个App.Deactivated事件,该事件可以确保您的应用程序以任何方式退出屏幕时都会被调用。

In your Page (either the .ctor or Loaded eventhandler), attach a handler to that event, and in that handler collect everything you must store in case of being closed. 在您的Page(.ctor或Loaded事件处理程序)中,将处理程序附加到该事件,并在该处理程序中收集在关闭时必须存储的所有内容。 This is the simpliest way to be sure that it will not evaporate. 这是确保它不会蒸发的最简单方法。 Just remember to unlink that handler upon Page.Unloaded, or else you will greatly leak the memory/resources!! 只要记住要在Page.Unloaded上取消链接该处理程序,否则您将大大泄漏内存/资源!

BTW. 顺便说一句。 The code 编码

var settings = IsolatedStorageSettings.ApplicationSettings;

settings["remZone"] = txtBoxZone.Text;
settings["remSpace"] = txtBoxSpace.Text;

is not sufficient in some cases. 在某些情况下还不够。 If you really need to be sure that your settings are persisted, you must manually call Save() after the changes: 如果确实需要确保设置保持不变,则必须在更改后手动调用Save():

var settings = IsolatedStorageSettings.ApplicationSettings;

settings["remZone"] = txtBoxZone.Text;
settings["remSpace"] = txtBoxSpace.Text;

settings.Save(); // think about it, you may want it!

this is because the Settings are stored on some specific occasions, like application quit, tombstoning etc. I don't remember about them being saved upon deactivation, so just pressing Back may not save them to the ISO. 这是因为设置是在某些特定的情况下存储的,例如应用程序退出,逻辑删除等。我不记得它们在停用时会被保存,因此仅按Back可能不会将它们保存到ISO。 What's more, if you set them and the App crashes - for example due to unhandled exception - or if the battery/power goes down, or the user simply turns off the phone - they will not be stored! 更重要的是,如果您设置了它们并且应用程序崩溃(例如,由于未处理的异常而导致)或电池/电源出现故障,或者用户只是关闭了手机,它们将不会被存储!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM