BlazorDesktop icon indicating copy to clipboard operation
BlazorDesktop copied to clipboard

implement the use of CenterScreen

Open PabloKench opened this issue 1 year ago • 2 comments
trafficstars

Is there an existing issue for this?

  • [x] I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

I started using blazor desktop, I noticed that it uses wpf, but I can't find a way to use WindowStartupLocation as CenterScreen

Describe the solution you'd like

Implementing this would be good, since most apps today are opened as centralized

Additional context

No response

PabloKench avatar Nov 24 '24 02:11 PabloKench

Most apps should actually remember the last location an app was used / opened at which is the bigger problem as I don't believe that works currently. The current settings should be the windows default behavior, where Windows will open multiple windows from the top left corner, you can see this behavior in File Explorer.

As a workaround you should be able to get access to the BlazorDesktopWindow in Program.cs and set the property yourself:

using BlazorDesktop.Hosting;
using HelloWorld.Components;
using Microsoft.AspNetCore.Components.Web;

var builder = BlazorDesktopHostBuilder.CreateDefault(args);

builder.RootComponents.Add<Routes>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");

if (builder.HostEnvironment.IsDevelopment())
{
    builder.UseDeveloperTools();
}

var app = builder.Build();

var window = app.Services.GetRequiredService<BlazorDesktopWindow>();

window.WindowStartupLocation = WindowStartupLocation.CenterScreen;

await app.RunAsync();

andrewbabbittdev avatar Nov 25 '24 00:11 andrewbabbittdev

Realistically we need to evaluate better window options, this ties into #42

It would probably be better if we also exposed the main window as a property under the BlazorDesktopHostBuilder.

andrewbabbittdev avatar Nov 25 '24 00:11 andrewbabbittdev

I hope this feature can be easily implemented.

Currently, when I try to call app.Services.GetRequiredService<BlazorDesktopWindow>(), I get an error The calling thread must be STA, because many UI components require this, and when I try to get the Application, I get a cross-thread access error.

Even after I explicitly declared the Main method and added [STAThread], the error still occurred.

robincodex avatar Jun 16 '25 16:06 robincodex

Generally it's safer to access it from Blazor components for the time being, #63 has an example

andrewbabbittdev avatar Jun 16 '25 22:06 andrewbabbittdev

Generally it's safer to access it from Blazor components for the time being, #63 has an example

Thank you. Although it is now running properly, setting WindowStartupLocation has no effect. As I recall, WindowStartupLocation only works if it is set during the initialization phase; setting it in the Razor file has no effect. The only way to achieve this is by moving the window position manually, which is a bit of a flaw.

robincodex avatar Jun 17 '25 03:06 robincodex

You can probably do it in Program.cs but you probably need to use a dispatcher to do so https://learn.microsoft.com/en-us/dotnet/api/system.windows.threading.dispatcher.invoke?view=windowsdesktop-9.0#system-windows-threading-dispatcher-invoke(system-action)

andrewbabbittdev avatar Aug 05 '25 07:08 andrewbabbittdev

I started looking into this, id like to make it so the WPF app starts on the main thread so that you can access the window from Program.cs but unfortunately its hard to tie this into hosting as WPF doesnt have an async start method

andrewbabbittdev avatar Aug 05 '25 08:08 andrewbabbittdev