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