UWP导航视图集IsPaneOpen false

UWP导航视图集IsPaneOpen false,uwp,uwp-xaml,Uwp,Uwp Xaml,我有这样一个导航视图: <NavigationView MenuItemsSource="{Binding HamMneuItems}" IsPaneOpen="False" Margin="0,0,0,0" Grid.Row="0" Grid.RowSpan="2" CompositeMode="SourceOver" x:Name="nvSample" IsSettingsV

我有这样一个导航视图:

<NavigationView           
    MenuItemsSource="{Binding HamMneuItems}"
    IsPaneOpen="False"
    Margin="0,0,0,0" 
    Grid.Row="0"
    Grid.RowSpan="2"
    CompositeMode="SourceOver"            
    x:Name="nvSample"
    IsSettingsVisible="True" 
    IsTabStop="False"            
    Header="{Binding Titulo,UpdateSourceTrigger=PropertyChanged,Mode=OneWay}" SelectionChanged="NvSample_SelectionChanged">
    <Frame x:Name="ScenarioFrame"
        Margin="5,0,5,5"
        Grid.Column="0"
        Grid.Row="0"
        Grid.RowSpan="2"
        d:IsHidden="True"/>
</NavigationView>
navSample.IsPaneToggleButtonVisible = false;

属性IsPaneOpen设置为false,但它始终显示已打开的窗格,在页面加载事件中的代码隐藏处,在导航视图加载事件中,尝试将IsPaneOpen设置为false,但没有结果

现在我的问题是如何在第一次显示NavigationView时以紧凑模式显示它


在何处设置IsPaneOpen以隐藏代码隐藏处的窗格

导航视图中的IsPaneOpen只是一个用于指定当前窗格视图状态的布尔标志,因此您不能在运行时使用它关闭窗格。 不幸的是,目前没有在运行时关闭菜单项的选项,将来可能会这样做,因此有一些解决方案可以关闭窗格或菜单项,如下所示:

navSample.OpenPaneLength = 0;
如果要隐藏菜单切换按钮,请执行以下操作:

<NavigationView           
    MenuItemsSource="{Binding HamMneuItems}"
    IsPaneOpen="False"
    Margin="0,0,0,0" 
    Grid.Row="0"
    Grid.RowSpan="2"
    CompositeMode="SourceOver"            
    x:Name="nvSample"
    IsSettingsVisible="True" 
    IsTabStop="False"            
    Header="{Binding Titulo,UpdateSourceTrigger=PropertyChanged,Mode=OneWay}" SelectionChanged="NvSample_SelectionChanged">
    <Frame x:Name="ScenarioFrame"
        Margin="5,0,5,5"
        Grid.Column="0"
        Grid.Row="0"
        Grid.RowSpan="2"
        d:IsHidden="True"/>
</NavigationView>
navSample.IsPaneToggleButtonVisible = false;

此处提供了一些其他解决方案的有用链接:

要启动应用程序,只需设置左侧的折叠菜单即可:

<NavigationView 
CompactModeThresholdWidth="1" 
ExpandedModeThresholdWidth="100000">

在xaml设置中,“已加载”事件

<NavigationView 
    Loaded="nvSample_Loaded" 
杰瑞的回答

引导动画存在一个折叠窗格动画

用于避开正在塌陷的动画的变通方法

xaml


使用
PaneDisplayMode=“LeftCompact”
显示压缩的菜单

对于我们这些希望将窗格隐藏在一定宽度下的人来说,这是一个很好的答案。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace MyControls
{
    public class FixedNavigationView : NavigationView
    {
        public bool InitialIsPaneOpen
        {
            get { return (bool)GetValue(InitialIsPaneOpenProperty); }
            set { SetValue(InitialIsPaneOpenProperty, value); }
        }

        // Using a DependencyProperty as the backing store for InitialIsPaneOpen.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty InitialIsPaneOpenProperty =
            DependencyProperty.Register("InitialIsPaneOpen", typeof(bool), typeof(FixedNavigationView), new PropertyMetadata(true));

        private double _orginOpenPaneLength;
        private Button _togglePaneButton;

        public FixedNavigationView()
        {
            this.Loaded += FixedNavigationView_Loaded;
            this.Unloaded += FixedNavigationView_Unloaded;
        }

        private void FixedNavigationView_Unloaded(object sender, RoutedEventArgs e)
        {
            if (this.InitialIsPaneOpen == false)
            {
                _togglePaneButton.PointerEntered -= _togglePaneButton_PointerEntered;
            }
        }

        protected override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            if (this.InitialIsPaneOpen == false)
            {
                _orginOpenPaneLength = this.OpenPaneLength;
                this.OpenPaneLength = 40;
            }
        }

        private void FixedNavigationView_Loaded(object sender, RoutedEventArgs e)
        {
            if (this.InitialIsPaneOpen == false)
            {
                this.IsPaneOpen = InitialIsPaneOpen;
                this._togglePaneButton = (Button)GetTemplateChild("TogglePaneButton");
                this._togglePaneButton.PointerEntered += _togglePaneButton_PointerEntered;
            }
        }

        private void _togglePaneButton_PointerEntered(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
        {
            if (this.InitialIsPaneOpen == false)
            {
                this.OpenPaneLength = _orginOpenPaneLength;
            }
        }


    }
}