【问题标题】:How to navigate from a page back to the MainWindow?如何从页面导航回主窗口?
【发布时间】: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


【解决方案1】:

您可以将 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

输出:

【讨论】:

    【解决方案2】:

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

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-08
      • 1970-01-01
      相关资源
      最近更新 更多