[英]C# - Changing From MainWindow to Another Page in MVVM Format WPF Application
当单击MainWindow上的按钮时,我一直试图弄清楚如何从我的MainWindow.xaml
切换到名为StudentView.xaml
View文件夹中的WPF页面。
这就是我到目前为止......
private void ButtonStartQuiz_Click(object sender, RoutedEventArgs e) {
var window = new View.StudentView();
}
我想要的是当用户点击按钮时MainWindow切换到StudentView页面。 我尝试将StudentView作为新窗口打开,但每次用户单击按钮时我都不想要新窗口。 我已经尝试使用谷歌搜索并查看其他帖子,但我不明白我应该如何实现它们。 请帮忙!
如果要从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));
在后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;
}
如果要保持MainWindow.xaml
内容的状态,请使用Frame + Pages
。
否则,使用ContentControl
并使用ContentTemplateSelector
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.