maui
maui copied to clipboard
SetTitleBar not working on Windows
Description
I am trying to customize the titlebar in Windows by following these steps:
https://learn.microsoft.com/en-us/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.window.settitlebar
But no matter what you do, the SetTitleBar call does nothing.
Steps to Reproduce
builder.ConfigureLifecycleEvents(events =>
{
events.AddWindows(windowsLifecycleBuilder =>
{
windowsLifecycleBuilder.OnWindowCreated(window =>
{
window.ExtendsContentIntoTitleBar = true;
window.SetTitleBar(new MyWindow().AppTitleBar);
});
});
});
Link to public reproduction project repository
No response
Version with bug
8.0.21 SR4.1
Is this a regression from previous behavior?
Not sure, did not test other versions
Last version that worked well
Unknown/Other
Affected platforms
Windows
Affected platform versions
No response
Did you find any workaround?
No
Relevant log output
No response
Hi I'm an AI powered bot that finds similar issues based off the issue title.
Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!
Open similar issues:
- Not able to change title bar color in MAUI UWP (#19252), similarity score: 0.72
Closed similar issues:
- Net Maui Windows Titlebar color is not working as expected (#19709), similarity score: 0.79
- Unable to hide title bar fron window with blazor hybrid on Windows (#17181), similarity score: 0.76
- Custom Title Bar Colors not working after migrating to .NET 8 (#19787), similarity score: 0.73
- Title bar still not behaving correctly... (#7123), similarity score: 0.73
Note: You can give me feedback by thumbs upping or thumbs downing this comment.
Here's some info on how to customize the titlebar
https://github.com/dotnet/maui/issues/20976#issuecomment-1977173364
Here's some info on how to customize the titlebar
@PureWeen I tried that but the following happens to me:
<DataTemplate x:Key="MauiAppTitleBarTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock VerticalAlignment="Center" Text="MyText" />
<TextBlock VerticalAlignment="Center" Grid.Column="2" Text="MyText2" />
</Grid>
</DataTemplate>
Output (as you can see, on the right side it overlaps with the buttons of the window):
(AppShell.xaml -> Shell.FlyoutBehavior="Disabled")
(AppShell.xaml -> Shell.FlyoutBehavior="Flyout")
As you can see, even using this method I can't get it to work.
Besides, what you have not answered me is why SetTitleBar doesn't work, is it a MAUI bug? Am I doing something wrong? Because my idea was to follow the "Full customization" tutorial since I want the title bar to be interactive:
https://learn.microsoft.com/en-us/windows/apps/develop/title-bar#full-customization
I agree that what @PureWeen sent doesn't address your issue directly, but I don't think that "SetTitleBar" doesn't work, it's that you're not using it right, but you also couldn't because the way MAUI apps work. I'll try to explain my thought process.
- First, your example code is wrong. If you're building a WinUI app, you would need to pass in a
UIElementcontained within the existingWindow. You're passing in a new element (I don't know the type, since you didn't provide a sample) so it's not connected to anything, so it won't work no matter what. - So you would, generally, build a WinUI Window, create a UIElement within that Window to represent the TitleBar, then call SetTitleBar with that element. While you can create a new MAUI Window (which itself is a WinUI 3 Window, wrapped), you can't directly set elements contained within it, at least not easily.
- What @PureWeen's example does show is that the TitleBar is already abstracted within MAUI and you can add additional elements within that. however to the best of my knowledge, elements contained within this TitleBar can't be interactive, apart from the hamburger menu on the left nav, because you would need further customization to allow buttons, etc, to be clickable.
So TL;DR
SetTitleBardoes work if you pass in a UIElement...- But passing in a valid UIElement isn't trivial unless you hack underlying implementations of frameworks...
- But there is a TitleBar you can customize through the Platform XAML...
- But you probably can't add the elements you want to make it interactive without also hacking more of the window...
IMO, you would be better off using MAUI Native Embedding and creating a WinUI 3 app that contains MAUI Elements instead. That would give you full control of the WinUI 3 window without needing to deal with all of that overhead. I have an example of this here that could help you do it.
You can set interactive regions here https://github.com/dotnet/maui/blob/b183cae2982f7c318f796d202f5c811d4da0edd8/src/Controls/src/Core/Window/Window.Windows.cs#L27-L34
Hi @bdovaz. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.
This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. If it is closed, feel free to comment when you are able to provide the additional information and we will re-investigate.