简体   繁体   English

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

[英]C#/UWP Hide NavigationView after button selection

Im bumping into an issue currently where clicking on an AppBarButton doesnt minimise the NavView after navigating. 目前,我遇到一个问题,即在导航后单击AppBarButton不会最小化NavView。 Unlike the NavView_SelectionChanged void which minimises NavView automatically after selecting a NavigationViewItem. 与NavView_SelectionChanged void不同,后者会在选择NavigationViewItem后自动最小化NavView。 Im not sure how to hide the NavView after updating the Content Frame. 我不确定更新内容框架后如何隐藏NavView。

Here is the MainPage.xaml.cs 这是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));
        }
    }

And the MainPage.XAML AppBarButtons: 和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>

All of these are closed off correctly so these are just the relevant bits. 所有这些都已正确关闭,因此它们只是相关的位。 Please excuse my lack of knowledge on this, still learning 请原谅我对此知识不足,仍在学习

first of all I would recommend you to use the latest version of NavigationView in the new winui library : https://docs.microsoft.com/en-us/windows/uwp/design/controls-and-patterns/navigationview 首先,我建议您在新的winui库中使用最新版本的NavigationViewhttps : //docs.microsoft.com/zh-cn/windows/uwp/design/controls-and-patterns/navigationview

Relevent parts from the docs above 从上面的文档中释放零件

NavigationView can be set to different display modes, via the PaneDisplayMode property: for your scenario you can toggle between values : Left and LeftCompact . 可以通过PaneDisplayMode属性将NavigationView设置为不同的显示模式:对于您的方案,可以在以下值之间切换: LeftLeftCompact

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. 每当用户单击应用程序栏按钮时,都会调用它的clicked事件,在该事件中,您可以将PaneDisplayMode属性设置为所需的值,并且通过这种方式,您可以控制何时显示NavigationView隐藏器或您想要执行的所有操作,基本上,您可以做很多事情使用此更新版本的控件,请阅读上面的docs链接,以获取更多详细文档。 Hope this helps. 希望这可以帮助。

SettingsButton_Click ContentFrame.Navigate调用之后添加以下行:

NavView.IsPanelOpen = false;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM