我对XAML和WPF很新,我有一个问题。如何从一个Xaml文件导航到另一个Xaml文件?
我有两个文件。 first.xaml和second.Xaml。在first.xaml中有一个按钮,点击它应该导航到second.xaml。我该如何实现这一点。
我对XAML和WPF很新,我有一个问题。如何从一个Xaml文件导航到另一个Xaml文件?
我有两个文件。 first.xaml和second.Xaml。在first.xaml中有一个按钮,点击它应该导航到second.xaml。我该如何实现这一点。
这是一个方式组织代码:
你second.xaml
应该包含你的窗口definiton如:
<Window x:Class="MediaCheckerWPF.AboutBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="About Media Checker" Height="300" Width="400" ResizeMode="NoResize"
Icon="/MediaCheckerWPF;component/Resources/checker.ico"
ShowInTaskbar="False">
<Grid>
...
</Grid>
</Window>
你first.xaml
有按钮,例如:
<Button Height="23" HorizontalAlignment="Right" Name="aboutButton"
VerticalAlignment="Top" Width="23" Click="AboutButton_Click"
Content="{DynamicResource TInformationButton}"
ToolTip="{DynamicResource TInformationButtonTooltip}" Margin="0,0,8,0"/>
然后在后面的代码:
private void AboutButton_Click(object sender, RoutedEventArgs e)
{
var about = new AboutBox { Owner = this };
about.Initialise();
about.Show();
}
ChrisF的回答是在Windows风格的应用程序中弹出一个新窗口的好方法,除非您不应该以这种方式调用Initialize(它应该从构造函数中调用)。
如果您想改为使用网页式导航,则应使用Page类而不是Window以及NavigationService。这也允许您的WPF应用程序在真实的Web浏览器中运行。