wpf 如何从框架中获取页面实例?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15122299/
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 do i get a page instance from a frame?
提问by osh
I have a frame that i initialized in xaml like this:
我有一个在 xaml 中初始化的框架,如下所示:
<window>
<Frame Name="myframe" NavigationUIVisibility="Hidden" Source="mypage.xaml"/>
</window>
I'm trying to get the page instance from the window that contains the frame (which in order contains the page) in c# code and i don't know how to get it.
我正在尝试从包含 c# 代码中的框架(按顺序包含页面)的窗口中获取页面实例,但我不知道如何获取它。
public partial class mywindow : Window
{
public mywindow()
{
BusinessLogic.Initialize();
InitializeComponent();
var a = myframe.Content;
}
}
how do i get it?
我怎么得到它?
thank you
谢谢你
回答by Loures
Your code is correct, but lack cast the return of Content.
您的代码是正确的,但缺少内容的返回。
public partial class mywindow : Window
{
public mywindow()
{
BusinessLogic.Initialize();
InitializeComponent();
var a = (MyPage)myframe.Content;
}
}
回答by Meirion Hughes
I imagine this solution should do the trick?
我想这个解决方案应该可以解决问题吗?
Find all controls in WPF Window by type
FindVisualChildren<Frame>(this).FirstOrDefault()

