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

Unable to change the title bar icon for a winui 3.0 app #1914

Closed
MarkvanWinkelen opened this issue Dec 16, 2021 · 8 comments
Closed

Unable to change the title bar icon for a winui 3.0 app #1914

MarkvanWinkelen opened this issue Dec 16, 2021 · 8 comments

Comments

@MarkvanWinkelen
Copy link

I am unable to change the title bar icon for my packaged winui 3.0 app. It is possible to change the title bar title, to in my case 'Koraalbewerkingen'. Can you make it possible to change the icon as easy as the title is changed in C#?
image

@andrewleader
Copy link
Contributor

Hi @MarkvanWinkelen, you can change the title bar icon using the AppWindow APIs. Here's an example...

// Get the AppWindow from the XAML Window ("this" is your XAML window)
IntPtr hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
WindowId myWndId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(hWnd);
var appWindow = AppWindow.GetFromWindowId(myWndId);

// And then set the icon
appWindow.SetIcon("yourIconPath");

In the future we're going to work on better integrating the AppWindow APIs with the XAML Window so you can more easily use all the great functionality that's within AppWindow without writing those 3 lines of verbose code! Here's the feature request for that item: Integrate AppWindow with WinUI 3.

@chausner
Copy link

It's worth mentioning that SetIcon appears to accept only files in ICO format. Also, it does not seem possible to use ms-appx or similar URIs. The path must be a full path to the file on disk.

@greatoceansoftware
Copy link

I get {"Specified cast is not valid."} error on the first line of andrew's example. I'm trying to use it in the C# partial class constructor code for the window, but I've also tried using it outside the constructor with the same error (I don't think it should matter).

image

@mluepkes
Copy link

Same problem here, I also get an InvalidCastException

image

@riverar
Copy link
Contributor

riverar commented Jun 23, 2022

GetWindowHandle expects a window, not a page.

@mluepkes
Copy link

Ah, well, that makes sense and when run with the window from App.OnLaunched() it works. Thanks for the help!

@ApoorvaPetkar
Copy link

ApoorvaPetkar commented Sep 26, 2022

And also I'm getting the same cast exception. Where did you make the above changes? Can you please help me by posting the screenshot or code?
And also how you are setting the App title?
@mluepkes @greatoceansoftware @riverar

@lopehole
Copy link

And also I'm getting the same cast exception. Where did you make the above changes? Can you please help me by posting the screenshot or code? And also how you are setting the App title? @mluepkes @greatoceansoftware @riverar

Regarding App Title - it can be set under MainWindow.xaml.cs, under public MainWindow add a line for:
this.Title = "app_name"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants