internal async Task UpdateToolbarItems() { var toolbarProvider = GetToolbarProvider(); if (toolbarProvider == null) { return; } CommandBar commandBar = await toolbarProvider.GetCommandBarAsync(); if (commandBar == null) { return; } commandBar.PrimaryCommands.Clear(); commandBar.SecondaryCommands.Clear(); var toolBarForegroundBinder = GetToolbarProvider() as IToolBarForegroundBinder; foreach (ToolbarItem item in _toolbarTracker.ToolbarItems) { toolBarForegroundBinder?.BindForegroundColor(commandBar); var button = new AppBarButton(); button.SetBinding(AppBarButton.LabelProperty, "Text"); if (commandBar.IsDynamicOverflowEnabled && item.Order == ToolbarItemOrder.Secondary) { button.SetBinding(AppBarButton.IconProperty, "IconImageSource", _imageSourceIconElementConverter); } else { var img = new WImage(); img.SetBinding(WImage.SourceProperty, "Value"); img.SetBinding(WImage.DataContextProperty, "IconImageSource", _imageConverter); button.Content = img; } button.Command = new MenuItemCommand(item); button.DataContext = item; button.SetValue(NativeAutomationProperties.AutomationIdProperty, item.AutomationId); button.SetAutomationPropertiesName(item); button.SetAutomationPropertiesAccessibilityView(item); button.SetAutomationPropertiesHelpText(item); button.SetAutomationPropertiesLabeledBy(item); ToolbarItemOrder order = item.Order == ToolbarItemOrder.Default ? ToolbarItemOrder.Primary : item.Order; if (order == ToolbarItemOrder.Primary) { toolBarForegroundBinder?.BindForegroundColor(button); commandBar.PrimaryCommands.Add(button); } else { commandBar.SecondaryCommands.Add(button); } } }
static StackPanel CreateContentContainer(Button.ButtonContentLayout layout, WImage image, string text) { var container = new StackPanel(); var textBlock = new TextBlock { Text = text, VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center }; var spacing = layout.Spacing; container.HorizontalAlignment = HorizontalAlignment.Center; container.VerticalAlignment = VerticalAlignment.Center; switch (layout.Position) { case Button.ButtonContentLayout.ImagePosition.Top: container.Orientation = Orientation.Vertical; image.Margin = new WThickness(0, 0, 0, spacing); container.Children.Add(image); container.Children.Add(textBlock); break; case Button.ButtonContentLayout.ImagePosition.Bottom: container.Orientation = Orientation.Vertical; image.Margin = new WThickness(0, spacing, 0, 0); container.Children.Add(textBlock); container.Children.Add(image); break; case Button.ButtonContentLayout.ImagePosition.Right: container.Orientation = Orientation.Horizontal; image.Margin = new WThickness(spacing, 0, 0, 0); container.Children.Add(textBlock); container.Children.Add(image); break; default: // Defaults to image on the left container.Orientation = Orientation.Horizontal; image.Margin = new WThickness(0, 0, spacing, 0); container.Children.Add(image); container.Children.Add(textBlock); break; } return(container); }
async void UpdateContent() { var text = Element.UpdateFormsText(Element.Text, Element.TextTransform); var elementImage = await Element.ImageSource.ToWindowsImageSourceAsync(); // No image, just the text if (elementImage == null) { Control.Content = text; Element?.InvalidateMeasureNonVirtual(InvalidationTrigger.RendererReady); return; } var size = elementImage.GetImageSourceSize(); var image = new WImage { Source = elementImage, VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center, Stretch = Stretch.Uniform, Width = size.Width, Height = size.Height, }; // BitmapImage is a special case that has an event when the image is loaded // when this happens, we want to resize the button if (elementImage is BitmapImage bmp) { bmp.ImageOpened += (sender, args) => { var actualSize = bmp.GetImageSourceSize(); image.Width = actualSize.Width; image.Height = actualSize.Height; Element?.InvalidateMeasureNonVirtual(InvalidationTrigger.RendererReady); }; } // No text, just the image if (string.IsNullOrEmpty(text)) { Control.Content = image; Element?.InvalidateMeasureNonVirtual(InvalidationTrigger.RendererReady); return; } // Both image and text, so we need to build a container for them Control.Content = CreateContentContainer(Element.ContentLayout, image, text); Element?.InvalidateMeasureNonVirtual(InvalidationTrigger.RendererReady); }