2012-09-17 9 views
3

내 WP7 응용 프로그램에 배치 한 웹 브라우저 컨트롤에서 적절한 백 스택 탐색을 수행하려고합니다. 웹 브라우저는 탐색 메서드를 구현하는 사용자 지정 컨트롤에있는 다음이 컨트롤을 응용 프로그램의 페이지에 배치합니다. 내비게이션은 백 스택을 제외하고 제대로 작동하는 것 같습니다. 때로는 이전 페이지로 돌아가고, 그렇지 않은 경우에는 응용 프로그램을 닫습니다. 여기 내 지금까지 구현 한 것입니다 :올바르게 웹 브라우저 컨트롤을 다시 처리하는 방법

WebBrowser.cs (사용자 제어)

//The navigation urls of the browser. 
    private readonly Stack<Uri> _NavigatingUrls = new Stack<Uri>(); 

    //The history for the browser 
    private readonly ObservableCollection<string> _History = 
     new ObservableCollection<string>(); 

    //Flag to check if the browser is navigating back. 
    bool _IsNavigatingBackward = false; 


    /// <summary> 
    /// Gets the History property for the browser. 
    /// </summary> 
    public ObservableCollection<string> History 
    { 
     get { return _History; } 
    } 

    /// <summary> 
    /// CanNavigateBack Dependency Property 
    /// </summary> 
    public static readonly DependencyProperty CanNavigateBackProperty = 
     DependencyProperty.Register("CanNavigateBack", typeof(bool), 
     typeof(FullWebBrowser), new PropertyMetadata((bool)false)); 

    /// <summary> 
    /// Gets or sets the CanNavigateBack property. This dependency property 
    /// indicates whether the browser can go back. 
    /// </summary> 
    public bool CanNavigateBack 
    { 
     get { return (bool)GetValue(CanNavigateBackProperty); } 
     set { SetValue(CanNavigateBackProperty, value); } 
    } 


    void TheWebBrowser_Navigating(object sender, 
     Microsoft.Phone.Controls.NavigatingEventArgs e) 
    { 
     //show the progress bar while navigating 
    } 

    void TheWebBrowser_Navigated(object sender, 
     System.Windows.Navigation.NavigationEventArgs e) 
    { 
     //If we are Navigating Backward and we Can Navigate back, 
     //remove the last uri from the stack. 
     if (_IsNavigatingBackward == true && CanNavigateBack) 
      _NavigatingUrls.Pop(); 

     //Else we are navigating forward so we need to add the uri 
     //to the stack. 
     else 
     { 
      _NavigatingUrls.Push(e.Uri); 

      //If we do not have the navigated uri in our history 
      //we add it. 
      if (!_History.Contains(e.Uri.ToString())) 
       _History.Add(e.Uri.ToString()); 
     } 

     //If there is one address left you can't go back. 
     if (_NavigatingUrls.Count > 1) 
      CanNavigateBack = true; 
     else 
      CanNavigateBack = false; 

     //Finally we hide the progress bar. 
     ShowProgress = false; 
    } 

    /// <summary> 
    /// Used to navigate back. 
    /// </summary> 
    public void NavigateBack() 
    { 
     _IsNavigatingBackward = true; 
     TheWebBrowser.InvokeScript("eval", "history.go(-1)"); 
    } 

BrowserPage.xaml.cs는

protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e) 
    { 
     if (TheBrowser.CanNavigateBack) 
     { 
      e.Cancel = true; 
      TheBrowser.NavigateBack(); 
     } 
     else 
      base.OnBackKeyPress(e); 
    } 

참고 : 자바 스크립트 내 웹 브라우저 및 기타 모든 탐색 작업에 사용 가능 바르게. 내 문제는 때로는 웹 브라우저가 올바르게 뒤로 이동하고 다른 경우에는 작동하지 않고 함께 닫는 것입니다. 내 impelementation에 문제가 있습니까? 이 문제를 해결하려면 어떻게해야합니까?

답변

1

base.OnBackKeyPress (e)를 호출 할 때 나만 무한 순환 참조를 작성하고 있습니까? 역방향 탐색을 사용할 수 없으면 사실 자체로 이벤트를 다시 보내려고 시도합니다. , 당신은 e.Cancel = true를하지 않음으로써이를 수행 할 필요가 없습니다; OnBackKeyPress 이벤트가 자동으로 문제없이 계속되도록 허용하므로 else 부분을 완전히 제거하고 어떤 일이 발생하는지 확인하십시오. back nav isnt가 사용 가능할 때 오류가 발생하기 때문에 순환 참조가 그때 만 시작되는 것은 당연합니다.

저는 VB.NET 프로그래머입니다. C#에 관해 전혀 다른 점이 없으면 솔루션에 대한 것일 수 있습니다.

e.cancel = true를 수행한다는 사실; 제안하지 않으면 이벤트가 계속 진행되므로 이벤트를 다시 시작하지 않아도됩니다 (취소하지 않으므로). 그러면 순환 참조가 생성됩니다. 그것을 제거하고 우리에게 무슨 일이 일어나는지 알려주십시오. 즉, 다음 줄을 제거해야합니다.

} 
     else 
      base.OnBackKeyPress(e); 
+0

응답 시간이 길어 져서 죄송합니다. 같은 문제가 여전히 발생하더라도 else 문을 제거하려고 시도했습니다. 때때로 네비게이션이 정확하고 다른 경우에는 웹 브라우저가있는 페이지에서 이전 페이지로 이동하지 않습니다. – Matthew

+0

@Matthew 브라우저를 뒤로 밀기 전에 브라우저가 뒤로 내비게이션을 시작한다는 것을 알고 있습니까? 그렇다면 뒤로 버튼을 눌러 계속 누르고 있으면 계속됩니다. 돌아갈 것이 없을 때까지 뒤로. 그래서 KeyPress 중 가로 챌 때 키 다운 + 키 업 이벤트를 가로 채는 것을 의미하므로 브라우저가 다시 돌아가서 코드를 실행하면 오류가 발생하거나 다시 탐색 할 수 있습니다. 날 잡아봐? 백 스페이스 키를 가로 채서 위의 코드를 그 이벤트에 배치하고 대신 어떻게 진행되는지 알려주십시오. –

관련 문제