I have a CommandBar. The main part works, including detecting taps. The overflow menu shows the tab names, but when I tap one of the tabs, nothing happens, and I can't see that any code fires. What am I missing?
<Page
x:Class="Jockusch.Calculator.WindowsStore.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Jockusch.Calculator.WindowsStore"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:system="using:System"
mc:Ignorable="d">
<Page.TopAppBar>
<CommandBar x:Name="TabBar">
<CommandBar.PrimaryCommands>
</CommandBar.PrimaryCommands>
<CommandBar.SecondaryCommands>
</CommandBar.SecondaryCommands>
</CommandBar>
</Page.TopAppBar>
<Grid Background="White" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" x:Name="PageGrid">
<local:WindowsAdAndContentWrapperView Grid.Row="0" Grid.Column="0" x:Name="ContentView"></local:WindowsAdAndContentWrapperView>
</Grid>
</Page>
Command bar setup code that fires not long after app launch. There are a lot of tabs, so some of them get pushed into the overflow menu. The ones that remain in the primary menu work fine.
static class WindowsTabContextAdditions
{
public static void SetupWindowsCommandBar(this TabContext context, CommandBar bar)
{
// TabContext and Tab are both my custom classes.
foreach (Tab tab in context.Tabs)
{
AppBarButton button = new AppBarButton();
IconElement icon = IconElements.ForTab(tab);
button.Label = tab.DisplayString;
button.Icon = icon;
bar.PrimaryCommands.Add(button);
}
}
}
static class IconElements
{
public const string IconPathPrefix = @"ms-appx:///Images/";
public static IconElement ForTab(Tab tab)
{
BitmapIcon r = null;
string name = tab.WindowsBitmapName();
string path = IconPathPrefix + name + ".png";
r = new TabBitmapIcon(tab);
r.UriSource = new Uri(path);
r.Tapped += Icon_Tapped;
return r;
}
private static void Icon_Tapped(Object sender, TappedRoutedEventArgs e)
{
// This method fires when the tab is selected. For the non-overflow ones, it works.
MyDebuggingClass.WriteLine("Tap detected!");
}
}
In general, the SecondaryCommands collection can contain only AppBarButton, AppBarToggleButton, or AppBarSeparator command elements. Icons are by default not shown in it.
Source from MSDN document CommandBar.SecondaryCommands property
So if the Icon wasn't shown, it wouldn't fire the Tap event.If you want to fire Tap event, you need to make Icon show first.
I followed DamirArh's suggestion in this thread Show icon on SecondaryCommands (UWP) to make the SecondaryCommands show the Icons. Then I registered Tap event for BitmapIcon. It worked well. When I clicked Icon, the Tap event will be fired.
You can refer to my code for reference: