79451976

Date: 2025-02-19 15:53:18
Score: 0.5
Natty:
Report link

The most elegant way for Windows desktop apps (.NET 9), to set the initial app size and position (1/3-2/3) based on the users screen size, goes like this. Be sure to edit the App.xaml.cs in the base map, not the file in the platforms maps. Edit the code there like this:

    protected override Window CreateWindow(IActivationState? activationState)
    {
        // App start-window size derived from screen properties
        double screenWidth = DeviceDisplay.MainDisplayInfo.Width;                               // the user screen
        double screenHeight = DeviceDisplay.MainDisplayInfo.Height;

        double  appWidth = screenWidth / 2.35d;                                                 // the app screen
        double  appHeight = screenHeight / 2.35d;

        return new Window(new AppShell())
        {
            Width = appWidth,
            Height = appHeight,
            X = (screenWidth - appWidth) / 2.0d,                                                // center horizontal
            Y = (screenHeight - appHeight) / 3.0d,                                              // 1/3 vertical
        };
    }

Do not forget to comment out the existing CreateWindow(...).

Reasons:
  • Blacklisted phrase (1): to comment
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Frank Zwarthoed