Is there any method to fix Window/Page size in UWP / WinUI?

c00012 716 Reputation points
2021-12-12T10:30:51.903+00:00

Hello,

In WPF, we can fix the window size using "ResizeMode = NoResize" option in XAML. but I couldn't find similar method in UWP / WinUI.
Is there any method to implement this in UWP/WinUI?

Universal Windows Platform (UWP)
0 comments No comments
{count} vote

2 answers

Sort by: Most helpful
  1. Castorix31 81,061 Reputation points
    2021-12-12T12:54:45.393+00:00

    In WinUI, you can use : OverlappedPresenter.IsResizable

    1 person found this answer helpful.

  2. PatrickTalon38 46 Reputation points
    2021-12-18T23:27:44.223+00:00

    Although this does not set a Window to stay at absolutely fixed size not adjustable, there is a way to modify the App.xaml.cs file to prefer a specific "starting size" to the application window.

    Here's the App.xaml.cs file:

    using System;
    using Windows.ApplicationModel;
    using Windows.ApplicationModel.Activation;
    using Windows.ApplicationModel.Core;
    using Windows.Foundation;
    using Windows.UI.Core;
    using Windows.UI.ViewManagement;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Navigation;
    
    namespace Melanite
        {
        sealed partial class App : Application
            {
            public App()
                {
                InitializeComponent();
                Suspending += OnSuspending;
                }
    
            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 )
                        {
                        //UNNECESSARY!: 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
                    Window.Current.Activate();
    
                    // I'm placing this here, instead of in the App Constructor, such that the App Window exists properly at function call...
                    // DEPRECATED BELOW:
                    // AppCenter.Start("1859fbd9-7996-43b1-877e-724521388725", typeof(Analytics), typeof(Crashes));
                    }
    
                // COMPLETE! - Use these only if no app local settings exist yet, otherwise, after first run, write the WindowWidthLast and WindowHeightLast to Local Settings, and try to Read them at every startup...
                ApplicationView.PreferredLaunchViewSize = new Size(2100, 1400);
                ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;
                CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = false;
                }
    
            void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
                {
                throw new Exception("Failed to load Page " + e.SourcePageType.FullName);
                }
    
            private void OnSuspending(object sender, SuspendingEventArgs e)
                {
                var deferral = e.SuspendingOperation.GetDeferral();
                //UNNECESSARY!: Save application state and stop any background activity
                deferral.Complete();
                }
            }
        }
    

    Key Lines of Code in OnLaunched

    ApplicationView.PreferredLaunchViewSize = new Size(2100, 1400);
    ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;
    

    Other ways to optimize UWP layout control reflow behaviors

    Otherwise, with the layout controls being so adaptive, such as with grid and stackpanel layout controls, I work over my graphical interface such that fairly small 60% x 60% of screen size views of my application are handled well by changing visibility to collapsed of some controls when sizing very small. Disabling and hiding controls when the window is snapped to a corner of the screen is also a wise way to disallow the user interaction with controls that are only partially showing. Finally, a TextBlock that is usually collapsed in visibility could become visible when the window is at very small size, by reacting to MainPage event handlers, and that text could alert the user to some message like "window is too small - resize larger."

    This could be accomplished by way of coding in content control visibility values for when sizing went below specific virtual pixel numbers, by handling the Page SizeChanged event.

    <Page
    .
    .
    .
    SizeChanged="MainPage_OnSizeChanged" />
    
    0 comments No comments