내 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에 문제가 있습니까? 이 문제를 해결하려면 어떻게해야합니까?
응답 시간이 길어 져서 죄송합니다. 같은 문제가 여전히 발생하더라도 else 문을 제거하려고 시도했습니다. 때때로 네비게이션이 정확하고 다른 경우에는 웹 브라우저가있는 페이지에서 이전 페이지로 이동하지 않습니다. – Matthew
@Matthew 브라우저를 뒤로 밀기 전에 브라우저가 뒤로 내비게이션을 시작한다는 것을 알고 있습니까? 그렇다면 뒤로 버튼을 눌러 계속 누르고 있으면 계속됩니다. 돌아갈 것이 없을 때까지 뒤로. 그래서 KeyPress 중 가로 챌 때 키 다운 + 키 업 이벤트를 가로 채는 것을 의미하므로 브라우저가 다시 돌아가서 코드를 실행하면 오류가 발생하거나 다시 탐색 할 수 있습니다. 날 잡아봐? 백 스페이스 키를 가로 채서 위의 코드를 그 이벤트에 배치하고 대신 어떻게 진행되는지 알려주십시오. –