我在我已放置在应用程序第二页的Web浏览器控件中使用导航事件。似乎前进和后退的导航事件正常工作。但是,我执行某些操作,例如维护导航事件处理程序中导航事件中使用的当前浏览器URL堆栈,根据某些情况,这些操作并不总是在页面导航到时进行注册。维护当前堆栈的浏览器URL的目的是确保如果主页(导航到我加载的应用程序的第二页的第一页导航)被导航到使用后栈,应用程序应该返回到Microsoft的要求所述的应用程序的第一页。
根据我的调试,如果我使用以下网络浏览器控制导航方案,我不会单步执行导航事件(如下所示)
MainPage>带浏览器的SecondPage(主页>转发>转发>返回>转发>返回(未发生导航事件))
这似乎令人困惑,但基本上一旦浏览器被导航回来(一次或多次用户想要),然后继续前进导航,下次用户希望导航到上一页时,导航即使页面被导航回来,也不会发生事件。
说明
//should always pass through this event when ANY navigation occurs
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;
_IsNavigatingBackward = false; //to eliminate staying as true when navigating forward and forward btn not pressed
}
public void NavigateForward()
{
_IsNavigatingBackward = false;
TheWebBrowser.InvokeScript("eval", "history.go(1)");
//_IsNavigating = true;
}
public void NavigateBack()
{
_IsNavigatingBackward = true;
TheWebBrowser.InvokeScript("eval", "history.go(-1)");
}
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
if (CanNavigateBack)
{
e.Cancel = true;
NavigateBack();
}
else
base.OnBackKeyPress(e);
}
导航事件未发生的此问题会导致NavigatingUrls计数失败,因此CanNavigateBack标志在主页上保持为真(当用户尝试再次通过后台堆栈导航到MainPage时)是假的,我的申请的主页永远无法导航到。
答案 0 :(得分:0)
我已经测试了你的代码(没有NavigateForward方法)并且它的工作正常。我不确定你是如何使用这种方法的。
根据我的观察,您的NavigateForward方法使用可能有问题。你怎么称呼它,你什么时候打电话?