How do i get a page instance from a frame?

依然范特西╮ 提交于 2019-12-23 12:09:50

问题


I have a frame that i initialized in xaml like this:

<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.

public partial class mywindow : Window
    {
        public mywindow()
        {
            BusinessLogic.Initialize();
            InitializeComponent();
            var a = myframe.Content;
         }
}

how do i get it?

thank you


回答1:


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;
         }
}



回答2:


I imagine this solution should do the trick?

Find all controls in WPF Window by type

 FindVisualChildren<Frame>(this).FirstOrDefault()


来源:https://stackoverflow.com/questions/15122299/how-do-i-get-a-page-instance-from-a-frame

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!