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

后端 未结 1 1056
旧时难觅i
旧时难觅i 2021-01-25 12:30

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 content

相关标签:
1条回答
  • 2021-01-25 13:17

    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/

    Try this example:

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

    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

    <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>
    
    0 讨论(0)
提交回复
热议问题