最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How can I center a Blazor Hybrid app running on Windows - Stack Overflow

programmeradmin3浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论