wpf 如何访问框架内的页面内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22080482/
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 11:02:46  来源:igfitidea点击:

How can I access a page's content inside a frame?

wpf

提问by Fehu

I have a frame inside mainwindow, inside it there's a page with panels and various contents. The mainwindow decides wich page to load, and then must interact with their contents (here is the problem).

我在主窗口中有一个框架,里面有一个带有面板和各种内容的页面。主窗口决定加载哪个页面,然后必须与其内容交互(这是问题所在)。

I've tried many solution and the best is this, but returns pageLogin as a null object

我已经尝试了很多解决方案,最好的是这个,但是将 pageLogin 作为空对象返回

    _mainFrame.Source = new Uri(@"/Pages/Login.xaml", UriKind.Relative);
    Page pageLogin = this._mainFrame.Content as Page;

where _mainFrame is of course the name of the frame inside t mainwindow, and Login.xaml is the content with the Login_panel Stackpanel inside

其中_mainFrame当然是t mainwindow里面的frame的名字,Login.xaml就是里面有Login_panel Stackpanel的内容

回答by Rhyous

OK. So first, I think you might be going about this the wrong way to start. Check out this project. http://www.wpfsharp.com/2011/04/05/navigation-and-pages-using-model-view-viewmodel-mvvm/

好的。所以首先,我认为你可能会以错误的方式开始。看看这个项目。 http://www.wpfsharp.com/2011/04/05/navigation-and-pages-using-model-view-viewmodel-mvvm/

Try this example:

试试这个例子:

MainWindow.xaml

主窗口.xaml

<Window x:Class="Test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        Loaded="Window_Loaded"
        >
    <Grid>
        <Frame Name="MainFrame" Source="/Login.xaml"></Frame>
    </Grid>
</Window>

MainWindow.xaml.cs

主窗口.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;

namespace Test
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var a = MainFrame.Content as Page;
            var grid = a.Content as Grid;
            var textBlock = grid.Children[0];
            // bla bla, you logged in
            MainFrame.Source = new Uri("/Home.xaml", UriKind.Relative);
            var b = MainFrame.Content as Page; // Still Login.xaml
            MainFrame.ContentRendered +=MainFrame_ContentRendered;
        }

        private void MainFrame_ContentRendered(object sender, EventArgs e)
        {
            var b = MainFrame.Content as Page; // Is now Home.xaml
        }
    }
}

Login.xaml

登录.xaml

<Page x:Class="Test.Login"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"
    Title="Login">

    <Grid>
        <TextBlock>This is a sample login page.</TextBlock>
    </Grid>
</Page>