问题
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?
回答1:
Define a public property Url in App.xaml.cs to store an Url
public Uri Url { get; set; }
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;
On Application_Deactivated event (send app to background), save current app's state to IsolatedStorage
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
settings["Url"] = Url;
settings.Save();
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"];
Then from the restored Url, you can re-load the last navigated page.
App app = Application.Current as App;
WebBrowser.Navigate(app.Url);
回答2:
You can try this:
bool isNew = true;
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
isNew = false;
}
this property will be maintained until your page is closed, so you can use it to test if this is the first time your page is navigated to.
来源:https://stackoverflow.com/questions/16514046/windows-phone-8-sdk-issue-with-screen-locking-and-application-starting-over