当点击MainWindow上的按钮时,我一直在试图弄清楚如何从我的MainWindow.xaml
切换到名为StudentView.xaml
的View文件夹中的WPF页面。
这就是我到目前为止......
private void ButtonStartQuiz_Click(object sender, RoutedEventArgs e) {
var window = new View.StudentView();
}
我想要的是当用户点击按钮时MainWindow切换到StudentView页面。我尝试将StudentView作为新窗口打开,但每次用户单击按钮时我都不想要新窗口。我已经尝试使用谷歌搜索并查看其他帖子,但我不明白我应该如何实现它们。请帮忙!
答案 0 :(得分:0)
如果您想从MainPage.xaml导航到另一个.xaml,请使用:
NAMEOFYOURCURRENTFRAME.Navigate(typeof(PAGENAME));
如果您想从不是MainPage.xaml的页面导航到另一个.xaml,请使用:
var rootFrame = Window.Current.Content as Frame;
var mainPage = rootFrame.Content as MainPage;
rootFrame.Navigate(typeof(PAGETONAVIGATETO));
答案 1 :(得分:0)
然后在button
点击后,您可以创建instance
page
并将instance
设置为content
mainwindow
private NavigatingPageName Instance;
private void Button_Click(object sender, RoutedEventArgs e)
{
if (Instance == null)
{
Instance = new NavigatingPageName();
}
this.Content = Instance;
}
答案 2 :(得分:0)
如果您希望保持MainWindow.xaml
内容的状态,请使用Frame + Pages
。
否则,请使用ContentControl
并使用ContentTemplateSelector
。