question

seihyungoh avatar image
1 Vote"
seihyungoh asked youki edited

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

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?

windows-uwp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Castorix31 avatar image
1 Vote"
Castorix31 answered youki edited

In WinUI, you can use : OverlappedPresenter.IsResizable


· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for the answer.

then, where can I use this class? for example, in MainWindow.xaml or MainWindow.xaml.cs? show me proper example if you can.

0 Votes 0 ·

Yes, in MainWindow.xaml.cs
For example, in the constructor :

At beginning :

 using Microsoft.UI;
 using Microsoft.UI.Windowing;

Variables :

 private IntPtr hWnd = IntPtr.Zero;
 private AppWindow appW = null;
 private OverlappedPresenter presenter = null;

then :

         public MainWindow()
         {
             this.InitializeComponent();
                
             hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
             WindowId wndId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(hWnd);               
             appW = AppWindow.GetFromWindowId(wndId);
             presenter = appW.Presenter as OverlappedPresenter;
             presenter.IsResizable = false;
         }





0 Votes 0 ·

How to do it in c++:

    // Retrieve the WindowId that corresponds to hWnd.
         Microsoft::UI::WindowId windowId = Microsoft::UI::GetWindowIdFromWindow(hWnd);
         // Lastly, retrieve the AppWindow for the current (XAML) WinUI 3 window.
         Microsoft::UI::Windowing::AppWindow appWindow = Microsoft::UI::Windowing::AppWindow::GetFromWindowId(windowId);
         Microsoft::UI::Windowing::OverlappedPresenter presenter = appWindow.Presenter().as<Microsoft::UI::Windowing::OverlappedPresenter>();
         presenter.IsResizable(false);










0 Votes 0 ·
patricktalon38 avatar image
0 Votes"
patricktalon38 answered youki commented

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" />



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.