我是WP8开发的新手,我正在创建一个具有WebBrowserControl的应用程序来访问我的网站。我想在应用程序到达后台时保存WebBrowser的状态,但我不能这样做(即当用户想要重新打开应用程序时,WebBrowser将加载最后一页看到的内容)。 我的代码based in this如下:
WebBrowser属性:Base和Source为空。
App.xaml.cs :
public partial class App : Application
{
//Url to store current address to maintain state when application is in background
public Uri Url { get; set; }
//Boolean to get if phone has been previously in background
public Boolean firstRun = true;
private void Application_Activated(object sender, ActivatedEventArgs e)
{
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
Uri currentUrl;
if (settings.TryGetValue("Url", out currentUrl))
Url = (Uri)settings["Url"];
}
private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
settings["Url"] = Url;
firstRun = false;
settings.Save();
}
MainPage.xaml.cs :
public partial class MainPage : PhoneApplicationPage
{
Uri baseUrl = new Uri("http://my_url");
// Constructor
public MainPage()
{
InitializeComponent();
webBrowser.Navigated += new EventHandler<NavigationEventArgs>(WebBrowserNavigation);
App app = Application.Current as App;
if (app.firstRun)
{
webBrowser.Navigate(baseUrl);
}
}
async void WebBrowserNavigation(Object sender, NavigationEventArgs navArgs)
{
string url = navArgs.Uri.ToString();
App app = Application.Current as App;
app.Url = navArgs.Uri;
}
答案 0 :(得分:0)
我认为你让bring it to the foreground
和relaunching
感到困惑。要将其恢复到前台,请在按下Windows按钮后按后退按钮。您将看到您的应用程序正是您离开它的位置。如果您通过按标题或使用开始菜单重新启动程序,它将启动它的新实例...从而显示您的基本URL。
所以基本上FirstRun在你的程序中并不重要。启动程序时,您应该通过执行此操作获取最后一个URL
private void Application_Launching(object sender, LaunchingEventArgs e)
{
// this.FirstRun = true; // doesn't matter
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
Uri currentUrl;
if (settings.TryGetValue("Url", out currentUrl))
{
Url = (Uri)settings["Url"]; // got the last url
}
else
{
Url = new Uri(@"http://www.msn.com"); // last url not defined, set it to default base
}
}
在两个活动中保存您的上一个网址
// Code to execute when the application is deactivated (sent to background)
// This code will not execute when the application is closing
private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
settings["Url"] = Url;
settings.Save();
}
// Code to execute when the application is closing (eg, user hit Back)
// This code will not execute when the application is deactivated
private void Application_Closing(object sender, ClosingEventArgs e)
{
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
settings["Url"] = Url;
settings.Save();
}
当您的MainPage加载后,导航到保存的URL
public MainPage()
{
InitializeComponent();
App myApp = Application.Current as App;
webBrowser.Navigated += new EventHandler<NavigationEventArgs>(WebBrowserNavigation);
webBrowser.Navigate(myApp.Url);
}
这样做,它总是保存您的状态,如果您将它带到前台或从标题或开始菜单再次启动它并不重要。您可以说,此时您正好tomestoned
了您的WebBrowser网址。