【问题标题】:How to show Command bar menu at desired position?如何在所需位置显示命令栏菜单?
【发布时间】:2018-03-31 15:19:57
【问题描述】:

我们可以在想要的位置展开弹出菜单..

FrameworkElement senderElement = sender as FrameworkElement;
myFlyout.ShowAt(sender as UIElement, e.GetPosition(sender as UIElement));

但是,如何在所需位置展开命令栏菜单?任何解决方法? IsOpen 属性仅在应用程序的默认右/左/顶部打开命令栏!我想在我想要的控制/位置附近打开它。

【问题讨论】:

    标签: c# uwp


    【解决方案1】:

    CommandBar 不提供 ShowAt 方法来显示与指定元素相关的命令栏菜单。

    如果您想在所需位置展开命令栏菜单,您应该能够将AppBarButton 放在Flyout 中,而不是将AppBarButton 放在CommandBar.SecondaryCommands 中。

    您可以添加CommandBarOpening 事件,并使用ShowAt 方法在事件中显示Flyout。当您将CommandBarIsOpen 属性设置为true 时,将触发Opening 事件。

    例如:

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <CommandBar x:Name="MyCommandBar"  Width="800" HorizontalAlignment="Center" VerticalAlignment="Center" Opening="MyCommandBar_Opening">
            <CommandBar.Resources>
                <Flyout x:Name="MyBtnFly" Placement="Right">
                    <StackPanel>
                        <AppBarButton x:Name="CommandBarAppBarButton" Icon="Like" Label="Like" />
                        <AppBarButton Icon="Dislike" Label="Dislike" />
                    </StackPanel>
                </Flyout>
            </CommandBar.Resources>
            <AppBarToggleButton Icon="Shuffle" Label="Shuffle"  />
            <AppBarToggleButton Icon="RepeatAll" Label="Repeat" />
            <AppBarSeparator/>
            <AppBarButton Icon="Back" Label="Back" />
            <AppBarButton Icon="Stop" Label="Stop" />
            <AppBarButton Icon="Play" Label="Play" />
            <AppBarButton Icon="Forward" Label="Forward" />
            <CommandBar.Content>
                <TextBlock Text="Now playing..." Margin="12,14"/>
            </CommandBar.Content>
        </CommandBar>
        <TextBlock x:Name="MyText" Tapped="Button_Click" HorizontalAlignment="Center" VerticalAlignment="Bottom" Text="Click"></TextBlock>
    </Grid>
    

    后面的代码:

    private void Button_Click(object sender, TappedRoutedEventArgs e)
    {
        MyCommandBar.IsOpen = true;
    }
    
    private void MyCommandBar_Opening(object sender, object e)
    {
        FrameworkElement senderElement = MyText as FrameworkElement;
        MyBtnFly.ShowAt(senderElement);
    }
    

    【讨论】:

      猜你喜欢
      • 2013-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多