我有一个WPF应用程序,我的按钮在我添加的一个窗口上,当我点击它时,我想要这个按钮打开一个页面。
NavigationService nav = NavigationService.GetNavigationService(this);
nav.Navigate(new Uri("xamlFeedbackPage.xaml", UriKind.RelativeOrAbsolute));
我尝试了在线的那段代码,但当我点击按钮时我的应用程序崩溃了。有什么帮助吗?
public PageFunction1()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
NavigationService nav = NavigationService.GetNavigationService(this);
nav.Navigate(new Uri("page2.xaml",UriKind.RelativeOrAbsolute));
}
不需要在代码中调用任何东西,因为它可以通过 XAML 自身完成。
App.xaml
<Application x:Class="WpfApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Page1.xaml">
</Application>
页面1
<Page x:Class="WpfApplication1.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Page1">
<StackPanel>
<TextBlock>
Go to <Hyperlink NavigateUri="Page2.xaml"> Page 2 </Hyperlink>
</TextBlock>
</StackPanel>
</Page>
第二页
<Page x:Class="WpfApplication1.Page2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Page2">
<StackPanel>
<TextBlock Margin="10">Welcome to Page2.</TextBlock>
<TextBlock Margin="10">
Go back to <Hyperlink NavigateUri="Page1.xaml"> Page 1 </Hyperlink>
</TextBlock>
</StackPanel>
</Page>
nav
不为空。 - Tejas Sharma