UWP/C# 框架导航问题

UWP/C# Frame Navigation Issues

抱歉,标题有点乱。 因此,我将以下处理程序链接到嵌套在 NavigationView

中的 AppbarButton 设置按钮
private void SettingsButton_Click(object sender, RoutedEventArgs e)
    {
        Frame.Navigate(typeof(Content_Pages.SettingsPage));
    }

它目前所做的只是导航到 pre-defined 的设置页面。现在我遇到的问题是我上面还有这个处理导航视图选择

  private void NavView_SelectionChanged(Windows.UI.Xaml.Controls.NavigationView sender, Windows.UI.Xaml.Controls.NavigationViewSelectionChangedEventArgs args)
        {
            NavigationViewItem item = args.SelectedItem as NavigationViewItem;

            switch (item.Tag.ToString())
            {
                case "OverView_Page":
                    ContentFrame.Navigate(typeof(Content_Pages.Overview_Page));
                    break;
             }
        }

这里的明显区别是 ContentFrame 部分。它允许所需页面显示为导航视图的一部分。

我完成 SettingsButton_Click 的方式意味着我在单击按钮时松开导航菜单,因为它调用 Frame.Navigate 而不是 Content.Navigate。

我曾尝试调整 SettingsButton_Click 以使用 ContentFrame.Navigate 但无济于事,因为它实际上是一个嵌套在 NavigationView 中的 AppBarButton。

<AppBarButton Icon="Setting" Margin="1, 2, 0, 0" Tag="Settings_Page" HorizontalAlignment="Center" Width="56.5" Height="56.5" ClickMode="Press" Click="SettingsButton_Click"/>

我不确定如何将其更改为内容Frame.Navigate 而不是 Frame.Navigate。你能帮忙看看我是哪里摔倒了吗?

编辑:

根据要求,MainPage.XAML

<Page
    x:Class="BudgetSheet.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:controls="using:Microsoft.UI.Xaml.Controls"
    xmlns:local="using:BudgetSheet"
    xmlns:mux="using:Windows.UI.Xaml.Controls"
    xmlns:muxcontrols="using:Microsoft.UI.Xaml.Controls"
    RequestedTheme="Dark">

    <Page.Resources>

    </Page.Resources>
    <Grid>
        <Grid.Resources>
            <ResourceDictionary>
                <ResourceDictionary.ThemeDictionaries>
                    <ResourceDictionary x:Key="Dark">
                        <SolidColorBrush x:Key="DarkBrush" Color="{StaticResource SystemBaseHighColor}"/>
                        <AcrylicBrush x:Key="NavigationViewDefaultPaneBackground"
                          BackgroundSource="Backdrop"
                          TintColor="#262626"
                          TintOpacity="0.8"
                          FallbackColor="#262626"/>
                    </ResourceDictionary>
                </ResourceDictionary.ThemeDictionaries>
            </ResourceDictionary>
        </Grid.Resources>

        <mux:NavigationView IsSettingsVisible="False" 
                            PaneTitle=" Budget Sheet Menu "                            
                            x:Name="NavView"                             
                            IsBackButtonVisible="Collapsed" 
                            PaneDisplayMode="LeftMinimal" 
                            AlwaysShowHeader="True"        
                            SelectionChanged="NavView_SelectionChanged"
                            Background="{StaticResource CustomAcrylicDarkBackground}">

            <mux:NavigationView.MenuItems>
                <StackPanel Orientation="Horizontal" UseLayoutRounding="False">
                    <AppBarButton Icon="Page2" Margin="0, 2, 1, 0" Tag="New_Sheet" HorizontalAlignment="Center" Width="56.5" Height="56.5" ClickMode="Press" />
                    <AppBarButton Icon="OpenFile" Margin="1, 2, 0, 0" Tag="Open_Sheet" HorizontalAlignment="Center" Width="56.5" Height="56.5" ClickMode="Press" />
                    <AppBarButton Icon="Save" Margin="1, 2, 0, 0" Tag="Save_Sheet" HorizontalAlignment="Center" Width="56.5" Height="56.5" ClickMode="Press"/>
                    <AppBarButton Icon="Setting" Margin="1, 2, 0, 0" Tag="Settings_Page" HorizontalAlignment="Center" Width="56.5" Height="56.5" ClickMode="Press" Click="SettingsButton_Click"/>
                    <AppBarButton Icon="Calculator" Margin="1, 2, 0, 0" Tag="Calculator_Open" HorizontalAlignment="Center" Width="56.5" Height="56.5" ClickMode="Press" />
                </StackPanel>

                <mux:NavigationViewItemSeparator/>
                <mux:NavigationViewItem Name="HomeItem" 
                                        Content="HOME" 
                                        Tag="HOME_Page" 
                                        FontSize="22" 
                                        HorizontalAlignment="Stretch" 
                                        FontWeight="Bold" 
                                        Foreground="#b880fc"/>
                <NavigationViewItemSeparator/>

                <mux:NavigationViewItem Name="OverviewItem" 
                                        Content="OVERVIEW" 
                                        Tag="OverView_Page" 
                                        FontSize="22" 
                                        HorizontalAlignment="Stretch" 
                                        FontWeight="Bold" 
                                        Foreground="#b880fc"/>

                <mux:NavigationViewItem Name="BillsItem" 
                                        Content="BILLS" 
                                        Tag="Bills_Page" 
                                        FontSize="22" 
                                        HorizontalAlignment="Stretch" 
                                        FontWeight="Bold" 
                                        Foreground="#b880fc"/>

                <mux:NavigationViewItem Name="PeopleItem" 
                                        Content="PEOPLE" 
                                        Tag="BillPayer_Page" 
                                        FontSize="22" 
                                        HorizontalAlignment="Stretch" 
                                        FontWeight="Bold" 
                                        Foreground="#b880fc"/>

                <mux:NavigationViewItem Name="TransfersItem" 
                                        Content="TRANSFERS" 
                                        Tag="Transfers_Page" 
                                        FontSize="22" 
                                        HorizontalAlignment="Stretch" 
                                        FontWeight="Bold" 
                                        Foreground="#b880fc"/>

                <mux:NavigationViewItem Name="PayDatesItem" 
                                        Content="PAY DATES" 
                                        Tag="PayDates_Page" 
                                        FontSize="22" 
                                        HorizontalAlignment="Stretch" 
                                        FontWeight="Bold" 
                                        Foreground="#b880fc"/>
            </mux:NavigationView.MenuItems>

            <Frame x:Name="ContentFrame">
                <Frame.ContentTransitions>
                    <TransitionCollection>
                        <NavigationThemeTransition/>
                    </TransitionCollection>
                </Frame.ContentTransitions>
            </Frame>

            <NavigationView.PaneFooter>
                <Button x:Name="ChangeUser" Style="{StaticResource TextBlockButtonStyle}" Foreground="#b880fc" >
                    <StackPanel HorizontalAlignment="Stretch" Orientation="Horizontal">
                        <SymbolIcon Symbol="Contact" Margin="8"/>
                        <TextBlock VerticalAlignment="Center" HorizontalAlignment="Right">      
                                    Change User
                        </TextBlock>
                    </StackPanel>
                </Button>
            </NavigationView.PaneFooter>
        </mux:NavigationView>
      </Grid>
</Page>

如果在 MainPage.xaml.cs 中添加 属性:

public Frame TheContentFrame
{
    get => ContentFrame;
}

..在使用 [= 获得对 MainPage 的引用后,您应该能够设置 MainPageContentFrameContent 17=] class:

private void SettingsButton_Click(object sender, RoutedEventArgs e)
{
    MainPage mainPage = FindParent<MainPage>(this);
    if (mainPage != null)
        mainPage.TheContentFrame.Navigate(typeof(Content_Pages.SettingsPage));
}

private static T FindParent<T>(DependencyObject dependencyObject) where T : DependencyObject
{
    var parent = VisualTreeHelper.GetParent(dependencyObject);

    if (parent == null) return null;

    var parentT = parent as T;
    return parentT ?? FindParent<T>(parent);
}

因此,我设法通过以下方式进行导航:

 private void SettingsButton_Click(object sender, RoutedEventArgs e)
        {
            ContentFrame.Navigate(typeof(Content_Pages.SettingsPage));
        }      

现在,导航成功了。它只是在导航后不会最小化导航视图。我要为此制定一个解决方案并从那里开始

感谢mm8到目前为止所做的一切

基本上我最初尝试做的事情绕了一圈,但现在可以了:(

编辑:

通过以下方法解决了最小化导航视图问题:

 private void SettingsButton_Click(object sender, RoutedEventArgs e)
        {        
            ContentFrame.Navigate(typeof(Content_Pages.SettingsPage));
            NavView.IsPaneOpen = false;
       }