如何从页面导航回到MainWindow?

时间:2019-10-20 05:33:24

标签: c# .net wpf navigation

我目前正在构建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
}

本应返回到MainWindow窗口的已编译页面的后退按钮单击,但似乎不起作用。有没有更好的方法编写此代码?谢谢。

2 个答案:

答案 0 :(得分:0)

我认为您应该将ContentControl用于解决方案。 创建多个视图(用户控件),然后将此实例传递给ContentControl.Content属性。

...
<ContentControl x:Name="navigatedRegion"/>
...

当您需要浏览视图时,请执行以下操作:

public void NavigateToA()
{
    var viewA = new ViewA();
    navigatedRegion.Content = viewA;
}

viewA是自定义用户控件的实例。

答案 1 :(得分:0)

您可以将Frame控件添加到MainWindow并使用该框架更改窗口内容。看到这个例子。

MainWindow.xaml

<Grid Background="#eeeeee">
    <Frame 
        Name="MainFrame"
        Margin="16"
        Background="White"
        NavigationUIVisibility="Hidden"/>
</Grid>

MainWindow.xaml.cs

public partial class MainWindow
{
    public MainWindow()
    {
        InitializeComponent();
        Application.Current.MainWindow = this;
        Loaded += OnMainWindowLoaded;
    }

    private void OnMainWindowLoaded(object sender, RoutedEventArgs e)
    {
        ChangeView(new Page1());
    }

    public void ChangeView(Page view)
    {
        MainFrame.NavigationService.Navigate(view);
    }
}

的Page1.xaml

<StackPanel>
    <Label 
        Foreground="YellowGreen" 
        Content="Page 1 Content"
        FontSize="32"/>
    <Button 
        Content="Go to Page2"
        Margin="8"
        Click="OnGoToPage2ButtonClicked"/>
</StackPanel>

Page1.xaml.cs

public partial class Page1
{
    public Page1()
    {
        InitializeComponent();
    }

    private void OnGoToPage2ButtonClicked(object sender, RoutedEventArgs e)
    {
        var mainWindow = (MainWindow)Application.Current.MainWindow;
        mainWindow?.ChangeView(new Page2());
    }
}

Page2.xaml

<StackPanel>
    <Label 
        Foreground="YellowGreen" 
        Content="Page 2 Content"
        FontSize="32"/>
    <Button 
        Content="Go to Page3"
        Margin="8"
        Click="OnGoToPage3ButtonClicked"/>
</StackPanel>

Page2.xaml.cs

public partial class Page2
{
    public Page2()
    {
        InitializeComponent();
    }

    private void OnGoToPage3ButtonClicked(object sender, RoutedEventArgs e)
    {
        var mainWindow = (MainWindow)Application.Current.MainWindow;
        mainWindow?.ChangeView(new Page3());
    }
}

Page3.xaml

<StackPanel>
    <Label 
        Foreground="BlueViolet" 
        Content="Page 3 Content"
        FontSize="32"/>
    <Button 
        Content="Back to Page1"
        Margin="8"
        Click="OnBackToPage1ButtonClicked"/>
</StackPanel>

Page3.xaml.cs

public partial class Page3
{
    public Page3()
    {
        InitializeComponent();
    }

    private void OnBackToPage1ButtonClicked(object sender, RoutedEventArgs e)
    {
        var mainWindow = (MainWindow) Application.Current.MainWindow;
        mainWindow?.ChangeView(new Page1());
    }
}

Example full code

输出:

enter image description here