尽管我对 WPF 完全陌生,但我需要编写一段代码,在其中单击按钮后,应用程序应该打开另一个 xaml。在网上搜索后,我按照以下方式进行了操作:
1.我创建了两个xaml文件,即“Window1.xaml”和“Window2.xaml”。
2.在我的“App.xaml”文件中,我让:
<Application x:Class="DiagramDesigner.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml">
3.然后在我的“Window1.xaml”中,我创建了一个按钮:
<Button Name="Button" Click="Button_Click_1" MouseEnter="Button_MouseEnter_1" IsDefault="True"
HorizontalAlignment="Center" VerticalAlignment="Center">
Start
</Button>
4.在我的“Windwo1.xaml.cs”文件中,我创建了这两个函数:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
}
private void Button_MouseEnter_1(object sender, MouseEventArgs e)
{
}
5.然后,为了在单击按钮后打开“Window2.xaml”,我更改为:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
NavigationService service = NavigationService.GetNavigationService(this);
service.Navigate(new Uri("Window2.xaml", UriKind.RelativeOrAbsolute));
}
但这给了我错误,说service
为空,程序崩溃了。我没有想出任何方法来解决这个问题。有什么建议么?谢谢。