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

时间:2019-03-29 13:31:45

标签: c# uwp-xaml

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

的异常
  

System.ArgumentException HResult = 0x80070057 Message =值不正确   落在预期范围内。来源= Windows StackTrace:位于   Windows.UI.Xaml.Controls.Primitives.FlyoutBase.ShowAt(FrameworkElement   placementTarget)位于   ProjectName.MainPage.TextBlock_ContextRequested(UIElement发送者,   C:\ Users \ <\ long中的ContextRequestedEventArgs args)   path> \ MainPage.xaml.cs:第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>

MainPage.xaml.cs(C#):

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;内部版本17763)

1 个答案:

答案 0 :(得分:1)

您需要为TextBlock设置 <Tabs defaultTab="income" onChange={(tabId) => { console.log(tabId) }}> <TabList> <Tab tabFor="income">Income</Tab> <Tab tabFor="expense">Expense</Tab> <Tab tabFor="liability">Liabilities</Tab> </TabList> <TabPanel tabId="income"> <p>Income</p> </TabPanel> <TabPanel tabId="expense"> <p>Expense</p> </TabPanel> <TabPanel tabId="liability"> <p>Liability</p> </TabPanel> </Tabs> ,然后调用FlyoutBase.AttachedFlyout方法来显示它。

然后,在我的测试中,如果您设置Placement =“ Auto”,它将像您发布的那样引发异常。

作为一种解决方法,请像下面这样删除CommandBarFlyout的FlyoutBase.ShowAttachedFlyout

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>