wpf 如何从页面级别获取父框架?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26968843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get parent frame from page level?
提问by MajkeloDev
i have a window with a frame and few pages. When the window is loaded the frame is navigating to welcome page and when i click a button inside welcome page i want parent frame to navigate to another page. To do that i need to access parent frame from page level but i can't figure out how to do this. I tried code below but it's returning null:
我有一个带框架和几页的窗口。当窗口加载时,框架导航到欢迎页面,当我单击欢迎页面内的按钮时,我希望父框架导航到另一个页面。为此,我需要从页面级别访问父框架,但我不知道如何执行此操作。我尝试了下面的代码,但它返回空值:
private void myButton_click(object sender, RoutedEventArgs e)
{
SecondPage secPage = new SecondPage();
((this.Parent) as Frame).Navigate(secPage);
}
I checked what 'this.parent' is returning and it is null. How can I get parent frame so i can navigate from one page to another ?
我检查了 'this.parent' 返回的是什么,它是空的。如何获取父框架以便我可以从一页导航到另一页?
回答by King King
A Page also has a property called NavigationService. You can use this property for convenience to navigate between pages:
页面还有一个名为 的属性NavigationService。您可以使用此属性方便地在页面之间导航:
private void myButton_click(object sender, RoutedEventArgs e) {
SecondPage secPage = new SecondPage();
NavigationService.Navigate(secPage);
}
To access the parent Frame, you have to use VisualTreeHelperto walk up, and find the Frame. You can search more about it. Anyway using the NavigationServiceproperty is better.
要访问父 Frame,您必须使用VisualTreeHelper向上走,然后找到 Frame。您可以搜索更多相关信息。无论如何使用该NavigationService属性更好。

