简体   繁体   English

C# UWP:参数不正确-NavigationView

[英]C# UWP: The parameter is incorrect - NavigationView

I'm building a UWP in C# with Visual Studio, and need to hide and show a NavigationView, named 'navigationView' as required.我正在使用 Visual Studio 在 C# 中构建一个 UWP,并且需要根据需要隐藏和显示名为“navigationView”的 NavigationView。 I am using the property .IsPaneVisible to achieve this, but am encountering a strange bug:我正在使用属性.IsPaneVisible来实现这一点,但遇到了一个奇怪的错误:

I can set IsPaneVisible = true or false in the xaml document, but if I set navigationView.IsPaneVisible = false;我可以在 xaml 文档中设置IsPaneVisible = true 或 false,但是如果我设置navigationView.IsPaneVisible = false; in the related C# document, and run a debug build of the app from Visual Studio, the app crashes with the error在相关的 C# 文档中,并从 Visual Studio 运行应用程序的调试版本,应用程序崩溃并出现错误

System.ArgumentException: 'The parameter is incorrect. System.ArgumentException: '参数不正确。

Even stranger, if I run the same debug build, but launched directly, outside Visual Studio, the app functions normally without crashing at this line.更奇怪的是,如果我运行相同的调试版本,但直接Visual Studio 外部启动,则应用程序正常运行而不会在此行崩溃。

Does anyone have an idea of why this might be happening or how I could fix it?有谁知道为什么会发生这种情况或我该如何解决?

Here's the xaml code:这是 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>

Try to change the placement of the menu with:尝试更改菜单的位置:

PaneDisplayMode="Top"

For my case with Microsoft.UI.Xaml.Controls for Microsoft.WindowsAppSDK v1.0.0.对于我的情况,Microsoft.UI.Xaml.Controls for Microsoft.WindowsAppSDK v1.0.0。 the showing/hidding of menu bar works fine.菜单栏的显示/隐藏工作正常。

Left, LeftMinimal, LeftCompact crashes the app. Left、LeftMinimal、LeftCompact 会使应用程序崩溃。 I know you may expect the menu on the left, but still on the top is not that bad idea;-) At least it works.. Greetz.我知道您可能希望菜单在左侧,但仍然在顶部并不是一个坏主意;-) 至少它有效.. Greetz。

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

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