C#/UWP 在选择按钮后隐藏 NavigationView

C#/UWP Hide NavigationView after button selection

我目前遇到一个问题,即在导航后单击 AppBarButton 不会最小化 NavView。与选择 NavigationViewItem 后自动最小化 NavView 的 NavView_SelectionChanged void 不同。 我不确定如何在更新内容框架后隐藏 NavView。

这是MainPage.xaml.cs

public sealed partial class MainPage : Page
    {

        public MainPage()
        {
            this.InitializeComponent();

            string appName = Windows.ApplicationModel.Package.Current.DisplayName;
        }

        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;
                case "Bills_Page":
                    ContentFrame.Navigate(typeof(Content_Pages.Bills_Page));
                    break;
                case "BillPayer_Page":
                    ContentFrame.Navigate(typeof(Content_Pages.BillPayer_Page));
                    break;
                case "Transfers_Page":
                    ContentFrame.Navigate(typeof(Content_Pages.Transfers_Page));
                    break;
                case "PayDates_Page":
                    ContentFrame.Navigate(typeof(Content_Pages.PayDates_Page));
                    break;               
            }
        }
       private void SettingsButton_Click(object sender, RoutedEventArgs e)
        {
            ContentFrame.Navigate(typeof(Content_Pages.SettingsPage));
        }
    }

以及 MainPage.XAML AppBarButtons:

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

所有这些都已正确关闭,因此这些只是相关位。请原谅我这方面的知识不足,还在学习中

首先,我建议您在新的 winui 库中使用最新版本的 NavigationViewhttps://docs.microsoft.com/en-us/windows/uwp/design/controls-and-patterns/navigationview

以上文档中的相关部分

NavigationView can be set to different display modes, via the PaneDisplayMode property: for your scenario you can toggle between values : Left and LeftCompact.

whenever user clicks an appbar button its clicked event will be invoked, within that event you can set the PaneDisplayMode property to your desired value and that way you can control when the NavigationView hidesor shows or whatever you want it to do basically u can do a lot with this newer version of the control, read the docs link above for more detailed docs. Hope this helps.

SettingsButton_Click 中调用 ContentFrame.Navigate 后添加此行:

NavView.IsPanelOpen = false;