I have added the possibility of a customer of my Blazor Hybrid app to adjust the size of the window when running on Windows.
To achieve this I made a WindowService class
public class WindowService
{
private readonly Window _window;
public WindowService(Window window)
{
_window = window ?? throw new ArgumentNullException(nameof(window));
}
public void SetWindowSize(double width, double height)
{
_window.Width = width;
_window.Height = height;
}
}
In the MauiProgram.cs I added:
builder.Services.AddSingleton(serviceProvider =>
{
var window = Application.Current?.Windows.FirstOrDefault();
return new WindowService(window);
});
And in the Blazor component I launch the following methods from 2 numeric inputs
private void SetWindowWidth(double width)
{
options.WindowWidth = Convert.ToInt32(width);
windowService.SetWindowSize(options.WindowWidth, options.WindowHeight);
}
private void SetWindowHeight(double height)
{
options.WindowHeight = Convert.ToInt32(height);
windowService.SetWindowSize(options.WindowWidth, options.WindowHeight);
}
But I would like also to center the window and couldn't find a way to do it.
I tried WinUIEx, but it doesn't seem to work in a Blazor Hybrid app.