Repository navigation
Description
Current behavior
ImageIconSource is silently ignored by every control that turns an IconSource into an IconElement through SharedHelpers.MakeIconElementFrom: TitleBar.IconSource, TabViewItem.IconSource, InfoBar.IconSource and TeachingTip.IconSource. No icon element is created, so the icon slot stays empty. There is no warning, and no error.
BitmapIconSource, FontIconSource, SymbolIconSource and PathIconSource work, so swapping the type is the only way to get an icon today.
Expected behavior
ImageIconSource produces an ImageIcon bound to its ImageSource (and Foreground), as in WinUI. WinUI's SharedHelpers::MakeIconElementFrom has an ImageIconSource branch, and it is placed before the BitmapIconSource one on purpose:
https://github.com/microsoft/microsoft-ui-xaml/blob/winui3/release/1.4.2/controls/dev/dll/SharedHelpers.cpp (SharedHelpers::MakeIconElementFrom, the try_as<winrt::ImageIconSource>() branch)
This is backed by reading the WinUI C++ source. I did not run a native WinUI repro.
How to reproduce it
- Create a Uno app with a
TitleBar(or aTabViewItem,InfoBar,TeachingTip). - Set an
ImageIconSourceas its icon:
<TitleBar Title="Hello">
<TitleBar.IconSource>
<ImageIconSource ImageSource="ms-appx:///Assets/StoreLogo.png" />
</TitleBar.IconSource>
</TitleBar>- Run the app. The icon slot is empty.
- Replace
ImageIconSourcewith<BitmapIconSource ShowAsMonochrome="False" UriSource="ms-appx:///Assets/StoreLogo.png" />. The icon shows.
Runtime-test repro, run on Skia Desktop (net11.0-desktop, Win32) on current master. Before the fix, FindFirstDescendant<ImageIcon>() returns null:
var bitmap = new BitmapImage(new Uri("ms-appx:///Assets/StoreLogo.png"));
var sut = new TitleBar { Title = "My Title", IconSource = new ImageIconSource { ImageSource = bitmap } };
await UITestHelper.Load(sut);
sut.FindFirstDescendant<ImageIcon>().Should().NotBeNull(); // fails on masterWorkaround
Use BitmapIconSource with ShowAsMonochrome="False".
Works on UWP/WinUI
WinUI (Windows App SDK). Source-backed: see the C++ reference above.
Environment
Uno Platform
NuGet package version
Current master (4393a8052de49dd04980b3f218ad8f56fa162aab). First seen on Uno.Sdk 7.0.0-dev.75.
Affected platforms
Skia Desktop (Win32) was verified at runtime. The code is shared C#, so I expect every target to be affected.
IDE
None
Relevant plugins
None
Anything else we need to know?
ImageIcon and ImageIconSource themselves are implemented. The gap is only the converter. Root cause: SharedHelpers.MakeIconElementFrom handles FontIconSource, SymbolIconSource, BitmapIconSource and PathIconSource, then returns null. The ImageIconSource branch from the WinUI source was left out of the port.
Suggested fix: add the branch before BitmapIconSource, mirroring WinUI:
else if (iconSource is ImageIconSource imageIconSource)
{
ImageIcon imageIcon = new ImageIcon();
if (imageIconSource.ImageSource != null) { imageIcon.Source = imageIconSource.ImageSource; }
if (imageIconSource.Foreground != null) { imageIcon.Foreground = imageIconSource.Foreground; }
return imageIcon;
}With that change the runtime test above passes locally (the icon is 50x50 and the Image inside it uses the same BitmapImage).
Side note: using the type in XAML never gives an Uno0001 warning, because it is not a [NotImplemented] type. The generated stubs for it are under #if false.
Activity
fix(icons): Handle ImageIconSource in MakeIconElementFrom