单击浮出控件中应用栏按钮的事件在 UWP 中不起作用

click event of appbar button within a flyout not working in UWP

我有一个命令栏,在其中一个应用栏按钮上附加了一个正确显示的弹出按钮。此弹出按钮再次具有带应用栏按钮的命令栏。问题是我想在其中一个按钮上再次打开弹出按钮,但这不起作用。请帮忙!

    <Page.Resources>
    <Flyout x:Key="SetBookmarkFlyout">
        <Grid Width="250" Height="250">
            <TextBlock Grid.Row="0">Name</TextBlock>
            <TextBox Grid.Row="1" x:Name="BookmarkName"></TextBox>
            <TextBlock Grid.Row="2">Create in</TextBlock>
            <ComboBox Grid.Row="3"></ComboBox>
        </Grid>
    </Flyout>
    <Flyout x:Key="SubMenuFlyout">
        <CommandBar Background="{ThemeResource     ApplicationPageBackgroundThemeBrush}" Height="250" >
            <AppBarButton Icon="Bookmarks" ToolTipService.ToolTip="Open Bookmarks"></AppBarButton>
            <AppBarButton x:Name="SetBookmark" Icon="Favorite" ToolTipService.ToolTip="Set Bookmark" Click="SetBookmark_Click" FlyoutBase.AttachedFlyout="{StaticResource SetBookmarkFlyout}" ></AppBarButton>
            <AppBarButton Icon="Page" ToolTipService.ToolTip="Recent Files"></AppBarButton>
        </CommandBar>
    </Flyout>
  </Page.Resources>

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Padding="0,0,0,-10" >

    <CommandBar x:Name="Commandbar" Height="70" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <AppBarButton Icon="Back" Label="" ToolTipService.ToolTip="Back" ></AppBarButton>
        <AppBarButton Icon="Forward" Label="" ToolTipService.ToolTip="Forward" ></AppBarButton>
        <AppBarButton x:Name="SpaceAppBtn"></AppBarButton>

        <AppBarButton Icon="Add" Label="" ToolTipService.ToolTip="Add Tab"></AppBarButton>
        <AppBarButton Icon="Favorite" Label="" ToolTipService.ToolTip="Favorites (Set Bookmark,open Bookmarks, and see recent files)" Click="AppBarButton_Click" FlyoutBase.AttachedFlyout="{StaticResource SubMenuFlyout}">
        </AppBarButton>
        <AppBarButton Icon="Find" Label="" ToolTipService.ToolTip="Search"></AppBarButton>


    </CommandBar>
    </Grid>

我已将您的 Flyout 直接添加到示例中的 Button 以进行测试。

<AppBarButton x:Name="FavoriteButton" Icon="Favorite" Label="" ToolTipService.ToolTip="Favorites (Set Bookmark,open Bookmarks, and see recent files)" 
                 Click="AppBarButton_Click">
        <AppBarButton.Flyout>
            <Flyout>
               <CommandBar Background="{ThemeResource     ApplicationPageBackgroundThemeBrush}" Height="250" >
                   <AppBarButton Icon="Bookmarks" ToolTipService.ToolTip="Open Bookmarks"></AppBarButton>
                    <AppBarButton x:Name="SetBookmark" Icon="Favorite" ToolTipService.ToolTip="Set Bookmark"
                      Click="SetBookmark_Cick">                                
                    </AppBarButton>
                    <AppBarButton Icon="Page" ToolTipService.ToolTip="Recent Files"></AppBarButton>
                    </CommandBar>
            </Flyout>
        </AppBarButton.Flyout>
    </AppBarButton>

使用一个 Popup 而不是第二个 Flyout。 在CommandBar之后添加

<CommandBar>...
</CommandBar>
<Popup x:Name="SetBookmarkPopup" HorizontalOffset="200" VerticalOffset="200">
    <Grid Width="250" Height="250">
        <TextBlock Grid.Row="0">Name</TextBlock>
        <TextBox Grid.Row="1" x:Name="BookmarkName"></TextBox>
        <TextBlock Grid.Row="2">Create in</TextBlock>
        <ComboBox Grid.Row="3"></ComboBox>
     </Grid>
</Popup>

并从您的 AppBarButton_Click 活动中拨打电话

private void AppBarButton_Click(object sender, RoutedEventArgs e)
{
     FavoriteButton.Flyout.ShowAt(FavoriteButton);
}  

终于打开弹窗

private void SetBookmark_Cick(object sender, RoutedEventArgs e)
{
    SetBookmarkPopup.IsOpen = true;
}

希望对你有帮助