【发布时间】:2019-10-20 05:33:24
【问题描述】:
我目前正在构建 WPF 应用程序,但在从另一个页面(已编译页面)导航回 MainWindow 时遇到问题。
我能够导航回以前的页面,但不能导航到 MainWindow 本身。
MainWindow.xaml.cs(窗口)
private void startTroubleshootButton_Clicknew(object sender, RoutedEventArgs e)
{
// instantiate the Q1 page and assign it to "pg1" variable
Q1 pg1 = new Q1();
// Sets the current content control view to Q1 page
this.Content = pg1;
}
Q1.xaml.cs(页面)
private void naviQ2ButtonClick(object sender, RoutedEventArgs e)
{
// instantiate mainwindow and store it in the window variable
var window = (MainWindow)Application.Current.MainWindow;
// instantiate Q2 page and pass the string variable "sb" as arguments to Q2 page/class
Q2 pg2 = new Q2(sb.ToString());
// set the current content control to Q2 page
window.Content = pg2;
}
Q2.xaml.cs(页面)
private void compileButtonClick(object sender, RoutedEventArgs e)
{
// instantiate mainwindow and store it in the window variable
var window = (MainWindow)Application.Current.MainWindow;
// instantiate compiledpage and pass the string variable "sb" and valueFromQ1 as arguments to compiledpage/class
Compiledpage pg3 = new Compiledpage(sb.ToString(), valueFromQ1);
// Show compiledpage in the current window
window.Content = pg3;
}
compiledpage.xaml.cs(页面)
private void backButton_Click(object sender, RoutedEventArgs e)
{
MainWindow pg = new MainWindow();
var window = (MainWindow)Application.Current.MainWindow;
window.Content = pg; //<- this doesn't work
}
compiledpage 的后退按钮点击应该返回到 MainWindow 窗口,但它似乎不起作用。有没有更好的编码方法?谢谢。
【问题讨论】:
-
你的问题没有意义,窗口不能是页面。您的窗口托管一个您认为是主页的页面,这是完全不同的。此外,请使用更完整的示例编辑您的问题,因为实现基于导航的 WPF 应用程序有不同的方法。
标签: c# .net wpf navigation