[英]How can I access a page's content inside a frame?
我在主窗口中有一個框架,其中有一個包含面板和各種內容的頁面。 主窗口決定要加載的夾頁,然后必須與它們的內容進行交互(這是問題所在)。
我嘗試了很多解決方案,最好的是這個,但是將pageLogin作為空對象返回
_mainFrame.Source = new Uri(@"/Pages/Login.xaml", UriKind.Relative);
Page pageLogin = this._mainFrame.Content as Page;
其中_mainFrame當然是t mainwindow內部框架的名稱,而Login.xaml是內部包含Login_panel Stackpanel的內容
好。 所以首先,我認為您可能會以錯誤的方式開始。 簽出此項目。 http://www.wpfsharp.com/2011/04/05/navigation-and-pages-using-model-view-viewmodel-mvvm/
試試這個例子:
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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.