[UWP] Locking size of app's window.

VoyTec 671 Reputation points
2023-03-03T07:20:07.2266667+00:00

What I want, is to lock window of an app, so it can't be resized at all, with use of a 1920 x 1080 resolution, scaling the app for 100%, instead of default scaling made by user on their own settings.

So I have this kind of a code:

            var size = new Size(1920, 1080);
            ApplicationView.PreferredLaunchViewSize = size;
            ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;
            Window.Current.CoreWindow.SizeChanged += (s, e) => { ApplicationView.GetForCurrentView().TryResizeView(size); };

I have xaml desginer in: ""23" Desktop (1920 x 1080) 100% scale".

Or is there a way to make a custom resolution of a xaml designer in VS...?

Universal Windows Platform (UWP)
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,208 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
762 questions
0 comments No comments
{count} votes

Accepted answer
  1. Roy Li - MSFT 31,661 Reputation points Microsoft Vendor
    2023-03-06T06:38:57.3233333+00:00

    Hello,

    Welcome to Microsoft Q&A!

    If I understand you correctly, you are trying to prevent the UIElements from scaling if user has a different scale value than 100% on his device, right? You just make the UIElements still be shown in 100% scale value.

    Currently, there is no way to turn off scaling on PC devices. What you could do is to override the automatic scaling of text and UI as the scale factor changes by checking the DisplayInformation.RawPixelsPerViewPixel property. You need to override the size for all the elements to achieve what you want.

    I've made a simple demo about this. The sample contains a TextBlock and a Button. Both the sizes are 300*300. When the scale value changes, them will still looks the same as they are in 100% scale.

    Xaml:

     <StackPanel>
            <TextBlock x:Name="MyTextBlock" Text="123213" FontSize="45" Width="300" Height="300"/>
            <Button x:Name="MyButton" Content="Click" Click="Button_Click" Width="300" Height="300"/>
        </StackPanel>
    
    

    Code-behind:

      public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
    
                this.Loaded += MainPage_Loaded;
    
                var size = new Size(1920, 1080);
                ApplicationView.PreferredLaunchViewSize = size;
                ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;
                Window.Current.CoreWindow.SizeChanged += (s, e) => { ApplicationView.GetForCurrentView().TryResizeView(size); };
    
                DisplayInformation displayInformation = DisplayInformation.GetForCurrentView();
                displayInformation.DpiChanged += DisplayInformation_DpiChanged;
    
            }
    
            private void DisplayInformation_DpiChanged(DisplayInformation sender, object args)
            {
                reSetSize();
            }
    
            private void MainPage_Loaded(object sender, RoutedEventArgs e)
            {
                reSetSize();
            }
    
            private void reSetSize() 
            {
                DisplayInformation displayInformation = DisplayInformation.GetForCurrentView();
                double rawPixelsPerViewPixel = displayInformation.RawPixelsPerViewPixel;
    
                // Set the override rectangle size  by taking our desired
                // size in raw pixels and converting it to view pixels.
                const double rectSizeInRawPx = 300; //300 is the original size of the button 
                double rectSizeInViewPx = rectSizeInRawPx / rawPixelsPerViewPixel; //get new size for the new scaling
                MyButton.Width = rectSizeInViewPx;
                MyButton.Height = rectSizeInViewPx;
                // override text font size
                double fontSizeInRawPx = 45 * 96 / 72; // 45 is the original fontsize
                double fontSizeInViewPx = fontSizeInRawPx / rawPixelsPerViewPixel;
                MyTextBlock.FontSize = fontSizeInViewPx;
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
    
            }
        }
    
    
    

    You could also refer to the official sample DpiScaling about Overriding default scaling of UI elements

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful