Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

App Icon doesn't appear in window title bar #20265

Closed
ThanhHaiDang96 opened this issue Jan 31, 2024 · 4 comments
Closed

App Icon doesn't appear in window title bar #20265

ThanhHaiDang96 opened this issue Jan 31, 2024 · 4 comments
Labels
area-controls-window Window platform/android 🤖 platform/iOS 🍎 platform/macOS 🍏 macOS / Mac Catalyst platform/windows 🪟 potential-regression This issue described a possible regression on a currently supported version., verification pending t/bug Something isn't working t/desktop The issue relates to desktop scenarios (MacOS/MacCatalyst/Windows/WinUI/WinAppSDK)

Comments

@ThanhHaiDang96
Copy link

Description

Hi, I'm using MAUI to create an application which's running on Windows and Mac platform.

In my main page, I have a button. And when button is clicked, another window appears.
I set the icon app in .csproj file:

!-- App Icon -->
		<MauiIcon Include="Resources\AppIcon\thumb_logo.svg" ForegroundFile="Resources\AppIcon\thumb_logo.svg" Color="#00FFFFFF" />

However, the icon doesn't appear in title bar in all of windows of this app.

image
image

My Button clicked handler:

private void OnCounterClicked(object sender, EventArgs e)
        {
            var win = new Window(new NewPage());

            win.Title = "Secondary Window";

            Application.Current?.OpenWindow(win);
        }

How can I show the icon in window title bar? Specially, in the secondary window.

Steps to Reproduce

  1. Create new MAUI App project
  2. Change App Icon in .csproj file
  3. Add new content page and set name as "New Page"
  4. Update OnCounterClicked() in MainPage.xaml.cs file
private void OnCounterClicked(object sender, EventArgs e)
        {
            var win = new Window(new NewPage());

            win.Title = "Secondary Window";

            Application.Current?.OpenWindow(win);
        }
  1. Run and Test

Link to public reproduction project repository

No response

Version with bug

8.0.6

Is this a regression from previous behavior?

Yes, this used to work in .NET MAUI

Last version that worked well

Unknown/Other

Affected platforms

iOS, Android, Windows, macOS

Affected platform versions

No response

Did you find any workaround?

Nothing

Relevant log output

No response

@ThanhHaiDang96 ThanhHaiDang96 added the t/bug Something isn't working label Jan 31, 2024
@drasticactions
Copy link
Contributor

drasticactions commented Jan 31, 2024

tl;dr I don't think this is a bug, but it's confusing. This is a bug, read second comment for answer.

As far as I'm aware, for the current version of the TitleBar, it's never supported that. Reading the documentation that field is for the package icon (Used in the start menu, etc), not for a window icon.

By default, WinUI 3 apps look like this.

image

The default AppWindow bar uses the Windows title bar, which includes an icon. But this icon isn't set by WinUI, even though it includes a package icon

image

To change it, you would need to set it. However, MAUI doesn't use the default Windows toolbar, it extends the baseline Window into it. This implementation includes an app name, but no icon.

That said,

void SetIcon()
{
var processPath = Environment.ProcessPath;
if (!string.IsNullOrEmpty(processPath))
{
var index = IntPtr.Zero; // 0 = first icon in resources
_windowIcon = ExtractAssociatedIcon(IntPtr.Zero, processPath, ref index);
if (_windowIcon != IntPtr.Zero)
{
var appWindow = AppWindow.GetFromWindowId(Win32Interop.GetWindowIdFromWindow(WindowHandle));
if (appWindow is not null)
{
var iconId = Win32Interop.GetIconIdFromIcon(_windowIcon);
appWindow.SetIcon(iconId);
}
}
}
}

They do pinvoke into Win32 and set the AppIcon. But this icon will only appear if you disable the Extended toolbar.

image

This is not the default behavior, and I don't think there is a MAUI API to do this AFAIK. You would need to know how to access the handler and call that bool yourself. And it would also break any TitleBar customization you have, since it's not the title bar. I would need to look through the history more, but my guess is that for net6.0, MAUI either didn't use ExtendsIntoToolbar and would then show the icon, or it always did and this code wasn't hit unless you knew enough to set it.

But, for the existing titlebar, AFAIK, it doesn't have a way to set that icon yourself. I could be missing something, but I think that would need to be a feature to be added.

@PureWeen what do you think?

@drasticactions
Copy link
Contributor

You see what I just said above? Yeah, I was wrong, this is a bug

<Image
x:Name="AppFontIcon"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Source="Square44x44Logo.png"
Visibility="{Binding WindowTitleIconVisibility}"
Width="16"
Height="16"/>

That's the default template for the titlebar. It's linking to an icon that doesn't exist in the package.

image

@ThanhHaiDang96 if you go into your Platform/Windows/App.xaml and add the following resource dictionary

    <maui:MauiWinUIApplication.Resources>
        <DataTemplate x:Key="MauiAppTitleBarTemplate">
            <Border
            Canvas.ZIndex="1" 
            VerticalAlignment="Stretch"
            Margin="0,0,0,0">
                <StackPanel Orientation="Horizontal" Margin="12, 0, 0, 0" x:Name="RootStackPanel">
                    <Image 
                    x:Name="AppFontIcon"
                    HorizontalAlignment="Left" 
                    VerticalAlignment="Center"
                    Source="{YOUR APPICON NAME HERE}Logo.png"
                    Visibility="Visible"
                    Width="16" 
                    Height="16"/>
                    <TextBlock 
                    x:Name="AppTitle"
                    VerticalAlignment="Center"
                    Margin="{Binding WindowTitleMargin}"
                    Text="{Binding WindowTitle}"
                    Foreground="{Binding WindowTitleForeground}"
                    Style="{StaticResource CaptionTextBlockStyle}" />
                </StackPanel>
            </Border>
        </DataTemplate>
    </maui:MauiWinUIApplication.Resources>

Replace YOUR APPICON NAME HERE with the name if your icon. If it doesn't work, go into the bin net8.0-windows10.0.19041.0\win10-x64\AppX folder and see what got generated. That should get the icon back.

That's what an extra 5 minutes of searching gets me.

@samhouts samhouts added potential-regression This issue described a possible regression on a currently supported version., verification pending legacy-area-desktop Windows / WinUI / Project Reunion & Mac Catalyst / macOS specifics (Menus & other Controls)) platform/macOS 🍏 macOS / Mac Catalyst platform/windows 🪟 platform/android 🤖 platform/iOS 🍎 labels Jan 31, 2024
@PureWeen
Copy link
Member

Duplicate of #6908

@PureWeen PureWeen marked this as a duplicate of #6908 Feb 22, 2024
@Foda
Copy link
Member

Foda commented Feb 22, 2024

@ThanhHaiDang96 as an alternative, you just need an image with the name square44x44logo in your project:

Ex:
<MauiImage Include="Resources\AppIcons\square44x44logo.svg" />

@github-actions github-actions bot locked and limited conversation to collaborators Mar 24, 2024
@Eilon Eilon added t/desktop The issue relates to desktop scenarios (MacOS/MacCatalyst/Windows/WinUI/WinAppSDK) area-controls-window Window and removed legacy-area-desktop Windows / WinUI / Project Reunion & Mac Catalyst / macOS specifics (Menus & other Controls)) labels May 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-controls-window Window platform/android 🤖 platform/iOS 🍎 platform/macOS 🍏 macOS / Mac Catalyst platform/windows 🪟 potential-regression This issue described a possible regression on a currently supported version., verification pending t/bug Something isn't working t/desktop The issue relates to desktop scenarios (MacOS/MacCatalyst/Windows/WinUI/WinAppSDK)
Projects
None yet
Development

No branches or pull requests

6 participants