How to set window title for a MAUI Blazor App targeting Windows?

0

Issue

Ive created a small application from the MAUI Blazor app template in MAUI preview 10 and have it targeted and running on windows. I however wish to set the title of the application which I imagined would be done with the Title attribute in the MainPage.xaml ContentPage tag. This however does nothing when starting the application.

enter image description here

Solution

In App.xaml.cs under Platforms -> Windows, the AppWindow can be retreived with some reflection usage. The Title property can then be set on the appwindow instance.

using Microsoft.UI;
using Microsoft.UI.Windowing;
using System;
using WinRT.Interop;
.
.
.
    protected override void OnLaunched(LaunchActivatedEventArgs args)
        {
            base.OnLaunched(args);

            Microsoft.Maui.Essentials.Platform.OnLaunched(args);

            var currentWindow = Application.Windows[0].Handler.NativeView;
            IntPtr _windowHandle = WindowNative.GetWindowHandle(currentWindow);
            var windowId = Win32Interop.GetWindowIdFromWindow(_windowHandle);

            AppWindow appWindow = AppWindow.GetFromWindowId(windowId);
            appWindow.Title = "Title!";
        }

Answered By – ME KURA

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More