首页 > 解决方案 > MenuFlyout 失败

问题描述

当在某些 MapItems 和 ListViewItems 上显示由 RightTapped 事件触发时,MenuFlyouts 也存在一些问题,所以我认为它也发生在其他控件上。

所以,当我打开第一个 MenuFlyout 时,它工作正常。我选择另一个 MapItem 并打开它的 MenuFlyout,然后它又可以正常工作了。但是,如果我在没有关闭上一个的情况下打开 MenuFlyout,它会打开,但是新的 MenuFlyout 背景变为黑色,并且包含 (Edit, View, ...) 他们不响应鼠标单击的 MenuFlyoutItems ,也不点击,...如果我尝试打开另一个 MenuFlyout,无论是在关闭前一个还是不关闭之后,它都会再次正常工作。

然后,我进行了调试以跟踪幕后发生的事情,我发现当一切正常时,触发的事件是下一个(打开过程):

关闭过程(ok):

打开失败的过程:

看有些事件触发了两次,当打开正常时,前三个事件有点奇怪(开始打开,关闭再次打开)。

输出窗口上没有错误或异常消息。

为什么会这样?我该如何解决?

也许,尝试显示第二个 MenyFlyout 时出现了一些问题,这是不可能的?在这种情况下,如何在显示下一个之前关闭上一个?

下面是我认为有用的所有代码。

谢谢

我的 MenuFlyout 是一个 ContextFlyout。这是 XAML 代码:

 <Maps:MapItemsControl x:Name="mapSpotsItems">
<Maps:MapItemsControl.ItemTemplate>
    <DataTemplate>
        <StackPanel x:Name="spotPin" Visibility="{Binding isVisible}" Maps:MapControl.NormalizedAnchorPoint="{Binding NormalizedAnchorPoint}"
                    Maps:MapControl.Location="{Binding geopoint}" Tag="{Binding ID}" ToolTipService.ToolTip="{Binding AdvancedDescription}" 
                    RenderTransformOrigin="0.5,1" 

                    Tapped="spotPin_Tapped" DoubleTapped="spotPin_DoubleTapped" RightTapped="spotPin_RightTapped"
                    PointerEntered="spotPin_PointerEntered" PointerExited="spotPin_PointerExited"
                    >

            <StackPanel.ContextFlyout>
                <MenuFlyout Closing="MenuFlyout_Closing" Closed="MenuFlyout_Closed" Opened="MenuFlyout_Opened" Opening="MenuFlyout_Opening">

                    <MenuFlyout.MenuFlyoutPresenterStyle>
                        <Style TargetType="MenuFlyoutPresenter">                
                            <Setter Property="Background" Value="{ThemeResource AcrylicDarkGrey50Background}"/>
                            <Setter Property="BorderBrush" Value="{ThemeResource DarkGrey10ThemeColor}"/>
                        </Style>
                    </MenuFlyout.MenuFlyoutPresenterStyle>

                    <MenuFlyoutItem Text="Open in Planner" Tag="{Binding ID}" Tapped="spotOpenInPlannerFlyoutItem_Tapped">

                    </MenuFlyoutItem>

                    <MenuFlyoutSeparator  />

                    <MenuFlyoutItem Text="View" Tag="{Binding ID}" Tapped="spotViewFlyoutItem_Tapped">            
                    </MenuFlyoutItem>
                    <MenuFlyoutItem Text="Edit" Tag="{Binding ID}" Tapped="spotEditFlyoutItem_Tapped">
                    </MenuFlyoutItem>

                    <MenuFlyoutSeparator Visibility="{Binding Exportable}"/>

                    <MenuFlyoutItem Text="Move" Tag="{Binding ID}" Tapped="spotMoveFlyoutItem_Tapped">
                    </MenuFlyoutItem>

                    <MenuFlyoutSeparator/>

                    <MenuFlyoutItem Text="Export" Tag="{Binding ID}" Visibility="{Binding Exportable}" Tapped="spotExportFlyoutItem_Tapped">
                    </MenuFlyoutItem>

                    <MenuFlyoutSeparator  Visibility="{Binding Exportable}" />

                    <MenuFlyoutItem Text="Delete" Tag="{Binding ID}" Tapped="spotDeleteFlyoutItem_Tapped">
                    </MenuFlyoutItem>

                </MenuFlyout>
            </StackPanel.ContextFlyout>


            <StackPanel Orientation="Horizontal" Background="{StaticResource SpotBackgroundColor}" Tag="{Binding ID}">
                <Grid>
                    <Rectangle Width="22" Height="22"  Opacity="0.5"/>
                    <Image Source="{Binding MainTag}" Height="20" Width="20" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Grid>

                <Grid  HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                    <TextBlock Text="{Binding Title}" Margin="5,0" HorizontalAlignment="Center" VerticalAlignment="Center" 
                               Width="50" 
                               MaxLines="2" TextWrapping="WrapWholeWords" TextTrimming="CharacterEllipsis" LineHeight="11"
                               Style="{StaticResource BaseTextBlockStyle}" 
                               FontSize="11" FontWeight="Normal" Foreground="{ThemeResource SpotForegroundColor}" />
                </Grid>
            </StackPanel>

            <Canvas HorizontalAlignment="Center">
                <Polygon Fill="{StaticResource SpotBackgroundColor}" Points="-5,0, 0,10, 5,0, -5,0" />
            </Canvas>


        </StackPanel>

    </DataTemplate>
</Maps:MapItemsControl.ItemTemplate>

</Maps:MapItemsControl>

以及打开 MenuFlyout 的 C#:

private async void spotPin_RightTapped(object sender, RightTappedRoutedEventArgs e)
{
    onSpotPin = true;

    StackPanel sp = sender as StackPanel;
    sp.ContextFlyout.ShowAt((FrameworkElement)sender);

    await Task.Delay(globalVars.SINGLE_TAP_MIN_TIME * 2);
    onSpotPin = false;

}

这里是 MenuFlyouts 的事件(为什么 Opening、Opened 和 Closed 参数类型是对象和 Closing 另一方面,有它自己的吗?):

private void MenuFlyout_Opening(object sender, object e)
{

}

private void MenuFlyout_Opened(object sender, object e)
{

}

private void MenuFlyout_Closed(object sender, object e)
{

}

private void MenuFlyout_Closing(FlyoutBase sender, FlyoutBaseClosingEventArgs args)
{

}

更新:

在尝试了不同的方法之后,我终于实现了我的目标:我创建了 MenuFlyout 和所有包含代码的项目。

尽管结果如此,但在尝试的过程中,我意识到通过代码添加或显示 MenuFlyout 也有它自己的问题。

奇怪的是,当显示由代码创建的 MenuFlyout 并附加到控件时,使用这行代码时:

f.ShowAt(sender as FrameworkElement);

它工作正常并满足我的需求,但如果使用相同的命令但使用第二个参数,它会失败,如前所述:

f.ShowAt(sender as FrameworkElement, e.GetPosition(sender as FrameworkElement));

有什么想法,为什么?

第二次更新和解决方案

使用 XAML(XAML 中定义的 MenuFlyout)时,应选择 Click 事件而不是 Tap 事件。

在代码中创建 MenuFlyout 时,Tap 事件可以正常工作。

标签: uwpuwp-xaml

解决方案


推荐阅读