CommandBar
不提供ShowAt
方法來顯示相對於指定元素放置的命令欄菜單。
如果你想擴大在需要的位置命令欄菜單中,你應該能夠把AppBarButton
在Flyout
,而不是把AppBarButton
在CommandBar.SecondaryCommands
。
您可以添加Opening
事件CommandBar
和使用ShowAt
方法顯示事件中的Flyout
。當您將IsOpen
配準CommandBar
設置爲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);
}