【问题标题】:C# UWP: The parameter is incorrect - NavigationViewC# UWP:参数不正确 - NavigationView
【发布时间】:2021-09-23 00:28:41
【问题描述】:

我正在使用 Visual Studio 在 C# 中构建 UWP,并且需要根据需要隐藏和显示名为“navigationView”的 NavigationView。 我正在使用属性.IsPaneVisible 来实现这一点,但遇到了一个奇怪的错误:

我可以在 xaml 文档中设置 IsPaneVisible = true 或 false,但如果我在相关的 C# 文档中设置 navigationView.IsPaneVisible = false;,并从 Visual Studio 运行应用程序的调试版本,应用程序会崩溃并出现错误

System.ArgumentException: '参数不正确。

更奇怪的是,如果我运行相同的调试版本,但直接在 外部 Visual Studio 启动,应用程序可以正常运行而不会在此行崩溃。

有没有人知道为什么会发生这种情况或我该如何解决?

这是 xaml 代码:

<Page
    x:Class="Sheet_Music_Reader.Views.ShellPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:i="using:Microsoft.Xaml.Interactivity"
    xmlns:behaviors="using:Sheet_Music_Reader.Behaviors"
    xmlns:winui="using:Microsoft.UI.Xaml.Controls"
    xmlns:helpers="using:Sheet_Music_Reader.Helpers"
    xmlns:views="using:Sheet_Music_Reader.Views"
    Loaded="OnLoaded"
    mc:Ignorable="d">
    <Page.Resources>
        <!--This top margin is the height of the custom TitleBar-->
        <Thickness x:Key="NavigationViewContentMargin">0,48,0,0</Thickness>
        <Thickness x:Key="NavigationViewContentGridBorderThickness">0</Thickness>
        <SolidColorBrush x:Key="NavigationViewContentBackground" Color="Transparent"></SolidColorBrush>
    </Page.Resources>

    <Grid>
        <Border x:Name="AppTitleBar"
                IsHitTestVisible="True"
                VerticalAlignment="Top"
                Background="Transparent"
                Height="40"
                Canvas.ZIndex="1" 
                Margin="48,8,0,0">
            <StackPanel Orientation="Horizontal">
                <Image x:Name="AppFontIcon"
                    HorizontalAlignment="Left" 
                    VerticalAlignment="Center"
                    Source="Assets/Square44x44Logo.png" 
                    Width="16" 
                    Height="16"/>
                <TextBlock x:Name="AppTitle"
                    Text="enScore"
                    VerticalAlignment="Center"
                    Margin="12,0,0,0"
                    Style="{ThemeResource CaptionTextBlockStyle}"/>
            </StackPanel>
        </Border>

        <winui:NavigationView
            IsBackButtonVisible="Visible"
            IsBackEnabled="{x:Bind IsBackEnabled, Mode=OneWay}"
            SelectedItem="{x:Bind Selected, Mode=OneWay}"
            ItemInvoked="OnItemInvoked"
            IsSettingsVisible="True"
            IsTitleBarAutoPaddingEnabled="False"
            DisplayModeChanged="NavigationViewControl_DisplayModeChanged"
            Canvas.ZIndex="0"
            x:Name="navigationView">
            <winui:NavigationView.MenuItems>
              
                <winui:NavigationViewItem x:Uid="Shell_Main" Icon="Copy" helpers:NavHelper.NavigateTo="views:MainPage" />
                
            </winui:NavigationView.MenuItems>

            <i:Interaction.Behaviors>
                <behaviors:NavigationViewHeaderBehavior
                DefaultHeader="{x:Bind Selected.Content, Mode=OneWay}">
                    <behaviors:NavigationViewHeaderBehavior.DefaultHeaderTemplate>
                        <DataTemplate>
                            <Grid x:Name="headerGrid">
                                <TextBlock
                                Text="{Binding}"
                                Style="{ThemeResource TitleTextBlockStyle}"
                                Margin="{StaticResource SmallLeftRightMargin}" />
                                <Button Style="{ThemeResource ButtonRevealStyle}"  Content="Refresh Library Contents" HorizontalAlignment="Right" Margin="0,45,8,0" Click="RefreshLibrary"/>
                                <Button Style="{ThemeResource ButtonRevealStyle}" Content="Import PDF To Current Folder" HorizontalAlignment="Right" Margin="0,-35,140,0" Click="AddScore"/>
                                <Button Style="{ThemeResource ButtonRevealStyle}" Content="Add New Folder" HorizontalAlignment="Right" Margin="0,-35,8,0" Click="AddFolderAsync"/>
                                <ToggleButton Style="{ThemeResource ToggleButtonRevealStyle}"  x:FieldModifier="public" x:Name="tbtndelete" Content="Delete Items" HorizontalAlignment="Right" Margin="0,45,191,0" Tapped="DeleteItem"/>
                            </Grid>
                        </DataTemplate>
                    </behaviors:NavigationViewHeaderBehavior.DefaultHeaderTemplate>
                </behaviors:NavigationViewHeaderBehavior>
            </i:Interaction.Behaviors>
            <Grid>
                <Frame x:Name="shellFrame" />
            </Grid>
        </winui:NavigationView>
      
    </Grid>

    
</Page>

【问题讨论】:

  • 我认为这与设置IsPaneVisible 没有直接关系。您能否展示一个引发相同异常的最小可重现示例?
  • @PharazFadaei,奇怪的是 navigationView.IsPaneVisible = true;工作正常。只是不带假。当我尝试整理一个更好的例子时,你能想到什么会影响一个布尔值而不影响另一个吗?
  • 你的WinUI版本和项目目标版本是多少?另外,你好像用的是MVVM,建议你尝试创建一个简单的uwp app(不使用MVVM)看看是否也出现这个问题。
  • @AryaDing-MSFT WinUI版本为2.6.1,项目目标版本为Windows 10,版本2004(10.0;Build 19041)
  • 我同意Pharaz Fadaei 的意见。首先,您可以创建一个空白的 uwp 应用程序,并在此应用程序中仅使用 WinUI 2.6.1 的 NavigationView 来检查是否出现此错误。

标签: c# uwp uwp-xaml


【解决方案1】:

尝试改变菜单的位置:

PaneDisplayMode="Top"

对于我的情况,Microsoft.UI.Xaml.Controls for Microsoft.WindowsAppSDK v1.0.0。 菜单栏的显示/隐藏工作正常。

Left、LeftMinimal、LeftCompact 使应用程序崩溃。 我知道您可能希望菜单在左侧,但仍然在顶部并不是那么糟糕的主意 ;-) 至少它有效.. Greetz。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-20
    • 1970-01-01
    • 2021-04-02
    • 2019-05-10
    • 1970-01-01
    相关资源
    最近更新 更多