Windows Phone 8 SDK - 屏幕锁定问题和应用程序重新启动

Windows Phone 8 SDK - 屏幕锁定问题和应用程序重新启动

问题描述:

我有一个带有 webbrowser 控件的应用程序.当我在该控件中导航然后离开一点,然后回到它时(由于不活动而解锁屏幕后),第一个/原始页面再次显示.如何维护浏览器的状态?

I have an application with a webbrowser control in it. When I navigate in that control then step away for a bit, then come back to it (after unlocking the screen due to inactivity), the first/original page shows up again. How can I maintain the state of the browser?

在 App.xaml.cs 中定义一个公共属性 Url 来存储一个 Url

Define a public property Url in App.xaml.cs to store an Url

public Uri Url { get; set; }

在 WebBrowser_LoadCompleted 事件中:将包含当前加载的 Url 的 WebBrowser.Source 属性保存到 Application 类的上述 Url 属性.

On WebBrowser_LoadCompleted event: save WebBrowser.Source property which contains the current loaded Url to above Url property of Application class.

App app = Application.Current as App; 
app.Url = WebBrowser.Source;

在 Application_Deactivated 事件(将应用程序发送到后台)时,将当前应用程序的状态保存到IsolatedStorage

On Application_Deactivated event (send app to background), save current app's state to IsolatedStorage

IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
settings["Url"] = Url;
settings.Save();

在 Application_Launching 事件(恢复应用程序)上,拉回存储的数据

On Application_Launching event (resume app), pull the stored data back

IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
Url currentUrl;
if (settings.TryGetValue("Url", out currentUrl)) 
   Url = (Uri)settings["Url"];

然后从恢复的 Url,您可以重新加载上次导航的页面.

Then from the restored Url, you can re-load the last navigated page.

App app = Application.Current as App;
WebBrowser.Navigate(app.Url);