我如何从一个框架中获得一个页面实例?[英] How do i get a page instance from a frame?

本文是小编为大家收集整理的关于我如何从一个框架中获得一个页面实例?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有一个我在XAML中初始初始化框架:

<window>
   <Frame Name="myframe" NavigationUIVisibility="Hidden" Source="mypage.xaml"/>
</window>

我正在尝试从包含框架(按顺序包含页面)中的窗口中获取页面实例,而我不知道如何获得.

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

我如何得到它?

谢谢

推荐答案

您的代码是正确的,但缺乏内容的返回.

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

其他推荐答案

我想这个解决方案应该解决问题吗?

by Type /p>

 FindVisualChildren<Frame>(this).FirstOrDefault()

本文地址:https://www.itbaoku.cn/post/2091161.html

问题描述

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

推荐答案

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

其他推荐答案

I imagine this solution should do the trick?

Find all controls in WPF Window by type

 FindVisualChildren<Frame>(this).FirstOrDefault()