2014-09-19 4 views
1

WP8 개발에 익숙하며 웹 사이트에 액세스 할 수있는 WebBrowserControl이있는 앱을 만들고 있습니다. 앱이 백그라운드에서 작동 할 때 WebBrowser의 상태를 저장하고 싶지만 이렇게 할 수 없습니다. 즉, 사용자가 다시 열려고하는 경우 WebBrowser가 마지막 페이지를로드합니다. 내 코드 based in this은 다음과 같습니다.WebBrowser 컨트롤 상태 유지

웹 브라우저 속성 : 기본 및 소스가 비어 있습니다.

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

어떻게이 작업을 수행 할 수 없습니까? 어떤 오류가 발생하고 있습니까? – APerson

+0

Windows 버튼 (배경으로 전환)을 밀고 응용 프로그램을 다시 열면 base_Url이 다시로드됩니다. –

답변

0

난 당신이 bring it to the foregroundrelaunching 혼란 것 같아요. 전경으로 되돌리려면 windows 단추를 누른 후 뒤로 단추를 누르십시오. 귀하의 애플리케이션은 귀하가 그 곳을 떠난 곳과 정확히 일치합니다. 제목을 누르거나 시작 메뉴를 사용하여 프로그램을 다시 시작하면 새 인스턴스가 시작되어 기본 URL이 표시됩니다.


기본적으로 FirstRun은 프로그램에서 중요하지 않습니다. 당신이 당신의 프로그램을 실행하면이 두 이벤트 모두 당신의 MainPage가로 이동을로드되는

// 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을 저장이

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을 잡아해야 저장된 URL

public MainPage() 
{ 
    InitializeComponent(); 

    App myApp = Application.Current as App; 

    webBrowser.Navigated += new EventHandler<NavigationEventArgs>(WebBrowserNavigation); 

    webBrowser.Navigate(myApp.Url); 
} 

이렇게하면 항상 상태를 저장하므로 제목이나 시작 메뉴에서 다시 포어라운드로 가져 오거나 시작하면 상관 없습니다. 이 시점에서 올바르게 tomestoned 웹 브라우저 URL이라고 말할 수 있습니다.

+0

해결책을 시험해보고 알려 드리겠습니다. WP8 개발에 초보자입니다.). 감사! –

+0

당신이 나에게 말한 모든 방법을 바꾸었고 Application_Activated 메서드를 유지하고 전경 작업을 훌륭하게 수행했지만 재 활성화 (프레스 Windows 및 다시 응용 프로그램 열기)가 작동하지 않습니다. 이 방법이 없어도 작동합니다. 앱이 닫힐 때 앱을 정리할 수 있기 때문일 수 있습니다. –

+0

Application_Activated를 없애면 필요하지 않습니다. 나는 당신의 코드에'Closing' 이벤트와 상충되는 다른 것이 있다고 생각합니다. 좋아, 다시 포 그라운드로 가져 오려면 다음과 같이하십시오. 윈도우 버튼을 누른 다음 뒤로 화살표를 누릅니다. 응용 프로그램을 닫으려면 뒤로 화살표를 누르십시오. 여기 내 드라이브 하나에 최소한의 프로젝트를 게시했습니다. https://onedrive.live.com/redir?resid=9F2F16D176D29EFC!8398&authkey=!AGjuxnhJD07Hr1Y&ithint=folder%2csln 행운을 빌어 App.xaml.cs 파일에도주의하십시오. MainPage.xaml.cs 파일로 저장하십시오. 다운로드하고 배포하십시오. 먼저 google.com으로 이동합니다. –