WP7アプリケーションに配置したWebブラウザコントロールで適切なバックスタックナビゲーションを実行しようとしています。Webブラウザーは、ナビゲーションメソッドを実装するカスタムコントロールにあり、このコントロールをアプリケーションのページに配置します。バックスタックを除いて、ナビゲーションは正しく機能しているようです。前のページに戻る場合もあれば、(想定されていない場合は)アプリケーションを閉じる場合もあります。これまでの私の実装は次のとおりです。
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);
}
注:Javascriptは私のWebブラウザーで有効になっており、他のすべてのナビゲーションは正しく機能します。私の問題は、Webブラウザが正しくナビゲートする場合と、機能せずにすべて一緒に閉じる場合があることです。私の捏造に問題はありますか?これを解決するにはどうすればよいですか?