【问题标题】:CommandBarFlyout keeps throwing a System.ArgumentException when trying to use as a ContextFlyout尝试用作 ContextFlyout 时,CommandBarFlyout 不断抛出 System.ArgumentException
【发布时间】:2019-03-29 13:31:44
【问题描述】:

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

System.ArgumentException HResult=0x80070057 消息=值不 落在预期范围内。源 = Windows StackTrace:在 Windows.UI.Xaml.Controls.Primitives.FlyoutBase.ShowAt(FrameworkElement 放置目标)在 ProjectName.MainPage.TextBlock_ContextRequested(UIElement 发件人, C:\Users\\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)

【问题讨论】:

    标签: c# uwp-xaml


    【解决方案1】:

    你需要为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);
    }
    

    【讨论】:

    • 看来我所要做的就是删除 Placement="Auto" 并且它可以工作。谢谢,不知道我是否会想到这一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-11
    • 2012-10-16
    • 2019-01-29
    • 1970-01-01
    • 1970-01-01
    • 2014-07-15
    • 1970-01-01
    相关资源
    最近更新 更多