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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:46:13  来源:igfitidea点击:

How do i get a page instance from a frame?

c#wpfxamlframe

提问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

按类型查找 WPF 窗口中的所有控件

 FindVisualChildren<Frame>(this).FirstOrDefault()