CommandBarFlyout 在尝试用作 ContextFlyout 时不断抛出 System.ArgumentException

CommandBarFlyout keeps throwing a System.ArgumentException when trying to use as a ContextFlyout

当我尝试使用下面的代码时。我的调试器抛出异常

System.ArgumentException HResult=0x80070057 Message=Value does not fall within the expected range. Source=Windows StackTrace: at Windows.UI.Xaml.Controls.Primitives.FlyoutBase.ShowAt(FrameworkElement placementTarget) at ProjectName.MainPage.TextBlock_ContextRequested(UIElement sender, ContextRequestedEventArgs args) in C:\Users\<\long path>\MainPage.xaml.cs:line 250

我已经尝试了所有我知道该怎么做的事情。我已经尝试设置附加的弹出窗口,然后尝试显示如下所示的附加弹出窗口。我还尝试将弹出窗口与文本块元素内联。我什至试图只在主 ListView 元素上显示浮出控件(认为它可能不喜欢动态列表),但我仍然遇到相同的错误。任何帮助将不胜感激。

FlyoutBase.SetAttachedFlyout((FrameworkElement)sender, AddDeviceFlyout);
FlyoutBase.ShowAttachedFlyout((FrameworkElement)sender);

当前代码

MainPage.xaml(为简洁起见删除了很多):

<Page.Resources>
    <ResourceDictionary>
        <CommandBarFlyout Placement="Auto" x:Name="AddDeviceFlyout">
            <AppBarButton x:Name="AddDevice" Label="Add Device" Icon="Add" Click="AddDevice_Click"/>
        </CommandBarFlyout>
    </ResourceDictionary>
</Page.Resources>
<ListView x:Name="ProcessList" SelectionMode="Single"
           ItemsSource="{x:Bind MyProcesses, Mode=OneWay}"
           HorizontalContentAlignment="Stretch"
           SelectionChanged="ProcessList_SelectionChanged">
 <ListView.ItemTemplate>
    <DataTemplate>
        <Grid Height="auto" HorizontalAlignment="Stretch">
            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
            <TextBlock 
              Margin="5,20" Height="50"
              FontSize="20"
              ContextRequested="TextBlock_ContextRequested"
              Text="{Binding ProcessName, Mode=OneWay}">
            </TextBlock>
        </Grid>
    </DataTemplate>
 </ListView.ItemTemplate>
</ListView>

MainPage.xaml.cs (C#):

private void TextBlock_ContextRequested(UIElement sender, Windows.UI.Xaml.Input.ContextRequestedEventArgs args)
{
    AddDeviceFlyout.ShowAt((FrameworkElement)sender); //This is line 250
}

构建目标:

目标版本:Windows10,版本 1809(10.0;内部版本 17763)

最低版本:Windows10,版本 1809(10.0;内部版本 17763)

您需要为TextBlock设置FlyoutBase.AttachedFlyout,然后调用FlyoutBase.ShowAttachedFlyout方法显示它。

然后,根据我的测试,如果您设置 Placement="Auto",它会像您发布的那样抛出异常。

作为解决方法,请删除 CommandBarFlyout 的 Placement="Auto",如下所示:

<ListView x:Name="ProcessList" SelectionMode="Single"
       ItemsSource="{x:Bind MyProcesses, Mode=OneWay}"
       HorizontalContentAlignment="Stretch"
       SelectionChanged="ProcessList_SelectionChanged">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid Height="auto" HorizontalAlignment="Stretch">
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                    <TextBlock
          Margin="5,20" Height="50"
          FontSize="20"
          ContextRequested="TextBlock_ContextRequested"
          Text="{Binding ProcessName, Mode=OneWay}">
                        <FlyoutBase.AttachedFlyout>
                            <CommandBarFlyout x:Name="AddDeviceFlyout">
                                <AppBarButton x:Name="AddDevice" Label="Add Device" Icon="Add" Click="AddDevice_Click" />
                            </CommandBarFlyout>
                        </FlyoutBase.AttachedFlyout>
                    </TextBlock>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
private void TextBlock_ContextRequested(UIElement sender, ContextRequestedEventArgs args)
{
    FlyoutBase.ShowAttachedFlyout(sender as FrameworkElement);
}