UWP NavigationView将IsPaneOpen设置为false

时间:2018-05-25 10:32:00

标签: 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"
    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>

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

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

在代码隐藏处将IsPaneOpen设置为隐藏窗格的位置?

5 个答案:

答案 0 :(得分:2)

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

navSample.OpenPaneLength = 0;

如果你想隐藏菜单切换按钮,也可以这样做:

navSample.IsPaneToggleButtonVisible = false;

这里有一些有用的链接可用于其他一些解决方案:UWP - Prevent NavigationViewItemHeader from being clipped

答案 1 :(得分:2)

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

<NavigationView 
    Loaded="nvSample_Loaded" 

在nvSample_Loaded事件背后的代码中:

private void nvSample_Loaded(object sender, RoutedEventArgs e)
{
     nvSample.IsPaneOpen = false;
}

答案 2 :(得分:1)

使用PaneDisplayMode="LeftCompact"显示压缩菜单。 Reference

答案 3 :(得分:0)

要使用左侧的折叠菜单启动您的应用,您可以进行设置:

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

答案 4 :(得分:0)

由Jerry'answer

引导动画存在折叠的窗格动画。

有关避免崩溃的动画的解决方法

xaml

    xmlns:controls="using:MyControls"
    <controls:FixedNavigationView InitialIsPaneOpen="False" x:Name="NavigationView">
        <NavigationView.MenuItems>
            <NavigationViewItem Content="Home" Icon="Home"></NavigationViewItem>
        </NavigationView.MenuItems>
    </controls:FixedNavigationView>

c#代码

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;
            }
        }


    }
}