如何删除表单的NavigationService?

时间:2015-05-16 15:00:30

标签: c# wpf

我创建了一个包含几页的框架。框架有自己的日记:

JournalOwnership="OwnsJournal"

当我传递2页时,我想要清除框架NavigationService

Frame parent_frame = (Frame)Application.Current.MainWindow.FindName("content_frame");

JournalEntry remove = parent_frame.RemoveBackEntry();

while (parent_frame.NavigationService.CanGoBack) {
    parent_frame.NavigationService.RemoveBackEntry();
    MessageBox.Show("Remove");
}

但是在清理NavigationService后我可以回去。

2 个答案:

答案 0 :(得分:2)

最简单的方法是处理框架导航事件:

frame.Navigated += frame_Navigated;

void frame_Navigated(object sender, NavigationEventArgs e)
{
    frame.NavigationService.RemoveBackEntry();
}

答案 1 :(得分:0)

我删除了NavigationService.BackStack中的所有可能页面,并在删除最后一页时添加了事件。

Frame root = (Frame)Application.Current.MainWindow.FindName("content_frame");
int i = 0;

while (root.NavigationService.CanGoBack) {
    i++;
    root.NavigationService.RemoveBackEntry();
}
root.NavigationService.RemoveBackEntry();
MessageBox.Show("Deleted - " + i);


NavigatedEventHandler navigated_handle = null;
navigated_handle = (o, e) => {
    ((Frame)o).RemoveBackEntry();
    MessageBox.Show("Do and remove itself!");
    root.Navigated -= navigated_handle;
};
root.Navigated += navigated_handle;