Hello,
Window.WindowStartupLocation is a method in WPF, MAUI for windows platform based on the WINUI 3, if you want to implement it, you can get the appwindow and calculate the center position of screen, then move your screen to this position in the MauiProgram.cs
.
You can refer to the following code.
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});
// add following code
#if WINDOWS
builder.ConfigureLifecycleEvents(events =>
{
// Make sure to add "using Microsoft.Maui.LifecycleEvents;" in the top of the file
events.AddWindows(windowsLifecycleBuilder =>
{
windowsLifecycleBuilder.OnWindowCreated(window =>
{
window.ExtendsContentIntoTitleBar = false;
var handle = WinRT.Interop.WindowNative.GetWindowHandle(window);
var id = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(handle);
var appWindow = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(id);
if (appWindow is not null)
{
Microsoft.UI.Windowing.DisplayArea displayArea = Microsoft.UI.Windowing.DisplayArea.GetFromWindowId(id, Microsoft.UI.Windowing.DisplayAreaFallback.Nearest);
if (displayArea is not null)
{
var CenteredPosition = appWindow.Position;
CenteredPosition.X = ((displayArea.WorkArea.Width - appWindow.Size.Width) / 2);
CenteredPosition.Y = ((displayArea.WorkArea.Height - appWindow.Size.Height) / 2);
appWindow.Move(CenteredPosition);
}
}
});
});
});
#endif
return builder.Build();
}
}
Best Regards,
Leon Lu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.