保留WebBrowser控件状态

本文关键字:控件状态 WebBrowser 保留 | 更新日期: 2023-09-27 18:23:41

我是WP8开发的新手,我正在制作一个可以访问我的网站的WebBrowserControl应用程序。当应用程序进入后台时,我想保存WebBrowser的状态,但我无法做到这一点(即,当用户想重新打开应用程序时,WebBrowser将加载最后一个页面)。基于此,我的代码如下:

WebBrowser属性:Base和Source为空。

应用程序.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();
    }

主页.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;
    }

保留WebBrowser控件状态

我认为您混淆了bring it to the foregroundrelaunching。要将其恢复到前台,请在按下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
    }
}

在两个事件中保存您的最后一个URL

// 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();
}

当你的主页被加载时,导航到保存的URL

public MainPage()
{
    InitializeComponent();
    App myApp = Application.Current as App;
    webBrowser.Navigated += new EventHandler<NavigationEventArgs>(WebBrowserNavigation);
    webBrowser.Navigate(myApp.Url);
}

这样做,它总是会保存你的状态,无论你是将它放在前台还是从标题或开始菜单再次启动它都无关紧要。您可以说您在这一点上正确地tomestoned您的WebBrowser URL。