hpw从窗口方法内部启动页面 [英] hpw to start a page from inside a window method
问题描述
你好,
我尝试了将窗口初始化为Page的通常方法,但是它不起作用,
我使用的代码是:
window.xaml.css:
Hello,
I tried the usual approach of initializing a window to a Page, but it does not work,
the code im using:
window.xaml.css:
private void show_page_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("This should open the -page- window");
var mypage = new Page();
mypage.Show();
}
甚至可以从window方法内部启动页面实例吗?
等待回复
谢谢,
Is it even possible to start an instance of a page from inside a window method?
waiting for replies
thanks
推荐答案
好吧,事实是,使用WPF时,只能在
Frame
或NavigationWindow
中显示Page
控件.应用程序,因此您需要做的是:
Ok, the thing is that the
Page
control can only be shown in aFrame
or aNavigationWindow
when using a WPF application, so what you need to do is:
在Window XAML中,您需要添加一个Frame
控件,如下所示:
In the Window XAML you need to add a Frame
control as shown bellow:
<Window x:Class="WpfTestPageQuestion.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">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button Content="Load Page" Click="show_page_Click"/>
<Frame Grid.Row="1" x:Name="frame"/>
</Grid>
</Window>
然后,您需要为按钮单击添加Click事件的处理程序:
Then you need to add the handler for the Click Event for the button click:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void show_page_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("This should open the -page- window");
// Start the Page control
var mypage = new Page();
// Set a Background to make a little more visible the change
mypage.Background = Brushes.Red;
// Add any content to your Page
mypage.Content = new TextBlock
{
Text = "Hello from the Page!",
FontSize = 30,
VerticalAlignment = System.Windows.VerticalAlignment.Center,
HorizontalAlignment = System.Windows.HorizontalAlignment.Center
};
// Add the Page instance to the Frame contained in the Window XAML
frame.Content = mypage;
// Happy message saying that we have successfully addend the Page
MessageBox.Show("Page shown =D");
}
}
您已完成.页面内容添加到窗口.
And you are done. A Page content addent to the Window.
希望这会有所帮助.
所有最佳
Raul Mainardi Neto
All Best
Raul Mainardi Neto
这篇关于hpw从窗口方法内部启动页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!