【问题标题】:UWP NavigationView set IsPaneOpen falseUWP NavigationView 设置 IsPaneOpen false
【发布时间】:2018-05-25 10:32:00
【问题描述】:

我有一个这样的导航视图:

<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,在导航视图 Loaded 事件中没有结果。

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

在哪里设置 IsPaneOpen 以隐藏代码后面的窗格?

【问题讨论】:

    标签: uwp uwp-xaml


    【解决方案1】:

    在 xaml 中设置一个“加载”事件

    <NavigationView 
        Loaded="nvSample_Loaded" 
    

    在 nvSample_Loaded 事件后面的代码中:

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

    【讨论】:

      【解决方案2】:

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

      navSample.OpenPaneLength = 0;
      

      如果您想隐藏菜单切换按钮,请执行以下操作:

      navSample.IsPaneToggleButtonVisible = false;
      

      一些花药解决方案的有用链接在这里:UWP - Prevent NavigationViewItemHeader from being clipped

      【讨论】:

        【解决方案3】:

        为了在左侧使用折叠菜单启动您的应用,您可以设置:

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

        【讨论】:

        • 对于我们这些希望在特定宽度下隐藏窗格的人来说,这是一个很好的答案。
        【解决方案4】:

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

        【讨论】:

          【解决方案5】:

          杰瑞的回答

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

          关于逃避折叠动画的解决方法

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

          【讨论】:

            猜你喜欢
            • 2020-01-14
            • 2020-12-27
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-02-15
            • 2020-01-11
            相关资源
            最近更新 更多