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

C# CommandBarFlyout在尝试用作ContextFlyout时不断抛出System.ArgumentException,c#,uwp-xaml,C#,Uwp Xaml,当我尝试使用下面的代码时。我的调试器抛出 System.ArgumentException HResult=0x80070057消息=值不正确 在预期范围内。Source=Windows StackTrace:at Windows.UI.Xaml.Controls.Primitives.FlyoutBase.ShowAt(FrameworkElement 位置(目标)在 ProjectName.MainPage.TextBlock_ContextRequested(UIElement发送方, C

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

System.ArgumentException HResult=0x80070057消息=值不正确 在预期范围内。Source=Windows StackTrace:at Windows.UI.Xaml.Controls.Primitives.FlyoutBase.ShowAt(FrameworkElement 位置(目标)在 ProjectName.MainPage.TextBlock_ContextRequested(UIElement发送方, C:\Users\\MainPage.xaml.cs中的ContextRequestedEventArgs参数:第250行

我已经尝试了我知道怎么做的一切。我已尝试设置附加的弹出按钮,然后尝试如下所示显示附加的弹出按钮。我还尝试将弹出按钮与textblock元素内联。我甚至试着只在主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>
private void TextBlock_ContextRequested(UIElement sender, Windows.UI.Xaml.Input.ContextRequestedEventArgs args)
{
    AddDeviceFlyout.ShowAt((FrameworkElement)sender); //This is line 250
}
构建目标:

目标版本:Windows 10,版本1809(10.0;版本17763)


最小版本:Windows 10,版本1809(10.0;Build 17763)

您需要为文本块设置
弹出式窗口。AttachedFlyout
,然后调用
弹出式窗口。ShowAttachedFlyout
方法来显示它

然后,在我的测试中,如果您设置Placement=“Auto”,它将抛出异常,就像您发布的一样

作为一种解决方法,请删除CommandBar弹出按钮的
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>

似乎我所要做的就是移除Placement=“Auto”,它就可以工作了。谢谢你,我不知道我是否能弄明白。