如何在uwp win10中以编程方式将图标传输到Appbar的SecondaryCommands

时间:2016-10-05 06:31:44

标签: c# uwp windows-10-universal

如何以编程方式将图标传输到SecondaryCommands(humberger) UWP Win10中的Appbar溢出?

换句话说,

如何在主要命令中删除图标并在UWP Win10中以Appbar溢出时以编程方式添加到SecondaryCommands(humberger)? 我的代码:

 <RelativePanel  VerticalAlignment="Bottom"  Grid.Row="1" >
        <CommandBar x:Name="myappbar" RelativePanel.AlignLeftWithPanel="True" RelativePanel.AlignRightWithPanel="True" >
        <AppBarButton Icon="Back"
                      Label="Back" />
        <AppBarButton Icon="Stop"
                      Label="Stop" />
        <AppBarButton Icon="Play"
                      Label="Play" />
        <AppBarButton Icon="Forward"
                      Label="Forward" />

        <CommandBar.SecondaryCommands>
            <AppBarButton Icon="Like"
                          Label="Like" />
            <AppBarButton Icon="Dislike"
                          Label="Dislike" />
        </CommandBar.SecondaryCommands>

            <!--<CommandBar.PrimaryCommands>
                <AppBarButton Icon="Like"
                              Label="Like" />
                <AppBarButton Icon="Dislike"
                              Label="Dislike" />
            </CommandBar.PrimaryCommands>-->

            <CommandBar.Content>
            <StackPanel Orientation="Horizontal">
                <AppBarButton Icon="Home" />
                <AppBarButton Icon="Like" />
            </StackPanel>
        </CommandBar.Content>
    </CommandBar>
    </RelativePanel>

1 个答案:

答案 0 :(得分:2)

从Windows 10版本1607开始,CommandBar引入了动态溢出功能。您可以将Bulk Insert Sql Server millions of record设置为true以启用此行为。

对于1607之前的版本,这是一个示例:

Select
代码背后的代码:

<CommandBar x:Name="myappbar" VerticalAlignment="Bottom" Opening="myappbar_Opening">
    <AppBarButton Icon="Back"
              Label="Back" />
    <AppBarButton Icon="Stop"
              Label="Stop" />
    <AppBarButton Icon="Play"
              Label="Play" />
    <AppBarButton Icon="Forward"
              Label="Forward" />
    <AppBarButton Icon="Like"
                  Label="Like" />
    <AppBarButton Icon="Dislike"
                  Label="Dislike" />

    <CommandBar.Content>
        <StackPanel Orientation="Horizontal">
            <AppBarButton Icon="Home" />
            <AppBarButton Icon="Like" />
        </StackPanel>
    </CommandBar.Content>
</CommandBar>

由于我们在public BlankPage() { this.InitializeComponent(); this.Loaded += Page_Loaded; this.SizeChanged += Page_SizeChanged; } private ItemsControl itemscontrol; private double width; private double itemwidth; private bool commandschanged; private void Page_Loaded(object sender, RoutedEventArgs e) { itemscontrol = FindChildOfType<ItemsControl>(myappbar); var appbarbutton = myappbar.PrimaryCommands.FirstOrDefault() as AppBarButton; itemwidth = appbarbutton.ActualWidth; width = itemscontrol.ActualWidth; } private void Page_SizeChanged(object sender, SizeChangedEventArgs e) { var commands = myappbar.SecondaryCommands; if (commands.Count != 0) foreach (var command in commands.Reverse()) { var appbarbutton = command as AppBarButton; myappbar.SecondaryCommands.Remove(appbarbutton); myappbar.PrimaryCommands.Add(appbarbutton); appbarbutton.IsEnabled = true; } commandschanged = false; } private void myappbar_Opening(object sender, object e) { var windowwidth = Window.Current.Bounds.Width; if (width > windowwidth && !commandschanged) { var secondarycommandsCount = Math.Ceiling((width - windowwidth) / itemwidth); for (int i = 0; i < secondarycommandsCount; i++) { var command = myappbar.PrimaryCommands.Last() as AppBarButton; Debug.WriteLine(command.IsEnabled); myappbar.PrimaryCommands.Remove(command); myappbar.SecondaryCommands.Add(command); } commandschanged = true; } } public static T FindChildOfType<T>(DependencyObject root) where T : class { var queue = new Queue<DependencyObject>(); queue.Enqueue(root); while (queue.Count > 0) { DependencyObject current = queue.Dequeue(); for (int i = 0; i < VisualTreeHelper.GetChildrenCount(current); i++) { var child = VisualTreeHelper.GetChild(current, i); var typedChild = child as T; if (typedChild != null) { return typedChild; } queue.Enqueue(child); } } return null; } 关闭时无法看到辅助命令栏,因此强制所有CommandBar停留在主命令栏中。在AppbarButton Opening事件中,我将部分Commandbar放入辅助命令栏,如果您想要找到这些AppbarButton,则会导致一个问题稍后代码背后的代码,我们可能无法确定这些AppbarButton属于哪个命令栏,为了解决这个问题,您可以使用AppbarButton来查找所有这些{{1 }}第