c# - 尝试用作 ContextFlyout 时,CommandBarFlyout 不断抛出 System.ArgumentException
问题描述
当我尝试使用下面的代码时。我的调试器抛出异常
System.ArgumentException HResult=0x80070057 Message=Value 不在预期范围内。Source=Windows StackTrace:位于 C:\Users\<\long path>\MainPage 中 ProjectName.MainPage.TextBlock_ContextRequested(UIElement sender, ContextRequestedEventArgs args) 的 Windows.UI.Xaml.Controls.Primitives.FlyoutBase.ShowAt(FrameworkElement placementTarget)。 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)
解决方案
您需要FlyoutBase.AttachedFlyout
为 TextBlock 设置 ,然后调用FlyoutBase.ShowAttachedFlyout
方法来显示它。
然后,通过我的测试,如果你设置 Placement="Auto",它会像你发布的那样抛出异常。
作为一种解决方法,请删除Placement="Auto"
CommandBarFlyout,如下所示:
<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);
}
推荐阅读
- python - 仅在 PythonAnywhere 上部署时出现 Django 错误:“没有类别与给定查询匹配”
- sql - 在 BigQuery 中查找上个月 MTD(当月至今)计算
- roles - 无法在 Blazor 服务器端使用 RoleManager 创建角色
- javascript - Javascript:强制div内的输入文本在里面
标签
- php - 为什么我的函数插入循环最后一个数据?
- bash - 使用 bash,通过 FOR 循环为 linux find 命令生成争论,会导致错误
- python-3.x - 这是一个简单的 Python 问题列表
- javascript - .getElementByID 问题(链接 html 和 js)
- flutter - 如何使用 Flutter 将文件上传到 Google Drive?
- python - 尝试导入证券交易所数据时出错(雅虎)