I am writing an UWP C# application. I am trying to fix my window size during the app launch. To achieve this, I am resizing the window in App.xaml.cs file. Below, the method for the resizing the window.
private void MaximizeWindowOnLoad()
{
var displayInformation = DisplayInformation.GetForCurrentView();
var widthOfScreen = displayInformation.ScreenWidthInRawPixels;
var heightOfScreen = displayInformation.ScreenHeightInRawPixels;
var scaleFactor = displayInformation.RawPixelsPerViewPixel;
var bounds = new Size(widthOfScreen / scaleFactor, heightOfScreen / scaleFactor);
var currentApplicationView = ApplicationView.GetForCurrentView();
currentApplicationView.SetPreferredMinSize(bounds);
ApplicationView.PreferredLaunchViewSize = new Size(bounds.Width, bounds.Height);
ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;
currentApplicationView.TryResizeView(bounds);
}
I call MaximizeWindowOnLoad from OnLaunched method in App.xaml.cs . Below the OnLaunched method.
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
Frame rootFrame = Window.Current.Content as Frame;
// Do not repeat app initialization when the Window already has content,
// just ensure that the window is active
if (rootFrame == null)
{
// Create a Frame to act as the navigation context and navigate to the first page
rootFrame = new Frame();
rootFrame.NavigationFailed += OnNavigationFailed;
if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
//TODO: Load state from previously suspended application
}
// Place the frame in the current Window
Window.Current.Content = rootFrame;
}
if (e.PrelaunchActivated == false)
{
if (rootFrame.Content == null)
{
// When the navigation stack isn't restored navigate to the first page,
// configuring the new page by passing required information as a navigation
// parameter
rootFrame.Navigate(typeof(MainPage), e.Arguments);
}
// Ensure the current window is active
MaximizeWindowOnLoad();
Window.Current.Activate();
}
}
The code works fine except one case. After installing the application, on the first launch, the code does not work. I checked, and during debugging the method is being called. I am using the methods from the following thread in stackoverflow. LINK .
I tried to debug to understand the issue. However, the behavior of the method is different for app opening cases (e.g., opening at first and following times). I expected the code to work correct but I don't understand whether it is the problem of UWP or the method is incorrect.