The Center property is invalid. #1043

Closed
opened 2026-01-29 16:56:26 +00:00 by claunia · 4 comments
Owner

Originally created by @Jaffoo on GitHub (Dec 22, 2025).

  • Version: 0.4.0
  • Target: Windwos

Steps to Reproduce:

1.Code:

private static async Task InitializeApp()
{
    // Create main window
    var browserWindow = await Electron.WindowManager.CreateWindowAsync(
        new BrowserWindowOptions
        {
            Width = 800,
            Height = 600,
            Center = true,
        });
    var fileInfo = new FileInfo(Environment.ProcessPath!);
    var exeFolder = fileInfo.DirectoryName!;
    var htmlPath = Path.Combine(exeFolder, "index.html");
    var url = new Uri(htmlPath, UriKind.Absolute);
    await browserWindow.WebContents.LoadURLAsync(url.ToString());
    await browserWindow.WebContents.Session.ClearCacheAsync();
    // Show window when ready
    browserWindow.OnReadyToShow += () => browserWindow.Show();
}

2.screenshot:(VS is in full-screen mode.)

Image
Originally created by @Jaffoo on GitHub (Dec 22, 2025). <!-- Please search existing issues to avoid creating duplicates. --> <!-- Which version of Electron.NET CLI and API are you using? --> <!-- Please always try to use latest version before report. --> * **Version**: 0.4.0 <!-- Which version of .NET Core and Node.js are you using (if applicable)? --> <!-- What target are you building for? --> * **Target**: Windwos <!-- Enter your issue details below this comment. --> <!-- If you want, you can donate to increase issue priority (https://donorbox.org/electron-net) --> Steps to Reproduce: 1.Code: ```C# private static async Task InitializeApp() { // Create main window var browserWindow = await Electron.WindowManager.CreateWindowAsync( new BrowserWindowOptions { Width = 800, Height = 600, Center = true, }); var fileInfo = new FileInfo(Environment.ProcessPath!); var exeFolder = fileInfo.DirectoryName!; var htmlPath = Path.Combine(exeFolder, "index.html"); var url = new Uri(htmlPath, UriKind.Absolute); await browserWindow.WebContents.LoadURLAsync(url.ToString()); await browserWindow.WebContents.Session.ClearCacheAsync(); // Show window when ready browserWindow.OnReadyToShow += () => browserWindow.Show(); } ``` 2.screenshot:(VS is in full-screen mode.) <img width="2559" height="1516" alt="Image" src="https://github.com/user-attachments/assets/029a0c2b-5bc9-4c3f-893c-3c00ca49ecfd" />
claunia added the bugapi labels 2026-01-29 16:56:26 +00:00
Author
Owner

@markatosi commented on GitHub (Dec 22, 2025):

Try it this way instead

private static async Task InitializeApp()
{
    // Create main window
    var browserWindow = await Electron.WindowManager.CreateWindowAsync(
        new BrowserWindowOptions
        {
            Width = 800,
            Height = 600,
           //Center = true,
        });
    var fileInfo = new FileInfo(Environment.ProcessPath!);
    var exeFolder = fileInfo.DirectoryName!;
    var htmlPath = Path.Combine(exeFolder, "index.html");
    var url = new Uri(htmlPath, UriKind.Absolute);
    await browserWindow.WebContents.LoadURLAsync(url.ToString());
    await browserWindow.WebContents.Session.ClearCacheAsync();
    // Show window when ready

    browserWindow.Center(); // <=======

    browserWindow.OnReadyToShow += () => browserWindow.Show();
}
@markatosi commented on GitHub (Dec 22, 2025): Try it this way instead ``` private static async Task InitializeApp() { // Create main window var browserWindow = await Electron.WindowManager.CreateWindowAsync( new BrowserWindowOptions { Width = 800, Height = 600, //Center = true, }); var fileInfo = new FileInfo(Environment.ProcessPath!); var exeFolder = fileInfo.DirectoryName!; var htmlPath = Path.Combine(exeFolder, "index.html"); var url = new Uri(htmlPath, UriKind.Absolute); await browserWindow.WebContents.LoadURLAsync(url.ToString()); await browserWindow.WebContents.Session.ClearCacheAsync(); // Show window when ready browserWindow.Center(); // <======= browserWindow.OnReadyToShow += () => browserWindow.Show(); } ```
Author
Owner

@Jaffoo commented on GitHub (Dec 22, 2025):

browserWindow.Center();

This method works, but the window will first appear in the top left corner and then flash to the center, which doesn't provide a very good user experience.

@Jaffoo commented on GitHub (Dec 22, 2025): > browserWindow.Center(); This method works, but the window will first appear in the top left corner and then flash to the center, which doesn't provide a very good user experience.
Author
Owner

@markatosi commented on GitHub (Dec 22, 2025):

Ok.... then do this.

 async Task<Rectangle> CenteredCoordinates(int width, int height)
    {
        var display = await Electron.Screen.GetPrimaryDisplayAsync();
        var screenSize = display.Size;
    
        var w = Math.Min(width, screenSize.Width);
        var h = Math.Min(height, screenSize.Height);
    
        var x = Math.Max(0, (screenSize.Width - w) / 2);
        var y = Math.Max(0, (screenSize.Height - h) / 2);
    
        return new Rectangle
        {
            Width = w,
            Height = h,
            X = x,
            Y = y
        };
    }

private static async Task InitializeApp()
{
    // Create main window
   var width = 800;
   var height = 600;
    var rectangle = await CenteredCoordinates(width, height);
    var browserWindow = await Electron.WindowManager.CreateWindowAsync(
        new BrowserWindowOptions
        {
            Width = width,
            Height = height,
            X = rectangle.X,
            Y = rectangle.Y,
        });
    var fileInfo = new FileInfo(Environment.ProcessPath!);
    var exeFolder = fileInfo.DirectoryName!;
    var htmlPath = Path.Combine(exeFolder, "index.html");
    var url = new Uri(htmlPath, UriKind.Absolute);
    await browserWindow.WebContents.LoadURLAsync(url.ToString());
    await browserWindow.WebContents.Session.ClearCacheAsync();
    // Show window when ready
    browserWindow.OnReadyToShow += () => browserWindow.Show();
}
@markatosi commented on GitHub (Dec 22, 2025): Ok.... then do this. ``` async Task<Rectangle> CenteredCoordinates(int width, int height) { var display = await Electron.Screen.GetPrimaryDisplayAsync(); var screenSize = display.Size; var w = Math.Min(width, screenSize.Width); var h = Math.Min(height, screenSize.Height); var x = Math.Max(0, (screenSize.Width - w) / 2); var y = Math.Max(0, (screenSize.Height - h) / 2); return new Rectangle { Width = w, Height = h, X = x, Y = y }; } private static async Task InitializeApp() { // Create main window var width = 800; var height = 600; var rectangle = await CenteredCoordinates(width, height); var browserWindow = await Electron.WindowManager.CreateWindowAsync( new BrowserWindowOptions { Width = width, Height = height, X = rectangle.X, Y = rectangle.Y, }); var fileInfo = new FileInfo(Environment.ProcessPath!); var exeFolder = fileInfo.DirectoryName!; var htmlPath = Path.Combine(exeFolder, "index.html"); var url = new Uri(htmlPath, UriKind.Absolute); await browserWindow.WebContents.LoadURLAsync(url.ToString()); await browserWindow.WebContents.Session.ClearCacheAsync(); // Show window when ready browserWindow.OnReadyToShow += () => browserWindow.Show(); } ```
Author
Owner

@FlorianRappl commented on GitHub (Dec 22, 2025):

Fixed in the most recent preview (0.4.1-pre.21).

@FlorianRappl commented on GitHub (Dec 22, 2025): Fixed in the most recent preview (0.4.1-pre.21).
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/Electron.NET#1043