Search code examples
windowsxamarinxamarin.formsxamarin.uwp

Xamarin UWP app not restricting the user from resizing the window to lower size in 150(recommended) resolution


     protected override void OnLaunched(LaunchActivatedEventArgs e)
            { 
              
             Window.Current.SizeChanged += Current_SizeChanged;
            }
    


    private void Current_SizeChanged(object sender, Windows.UI.Core.WindowSizeChangedEventArgs e)
        {
            var displayInformation = DisplayInformation.GetForCurrentView();
            if (displayInformation.ResolutionScale == ResolutionScale.Scale125Percent || displayInformation.ResolutionScale == ResolutionScale.Scale100Percent)
            {
                var screenSize = new Size(displayInformation.ScreenWidthInRawPixels,
                                          displayInformation.ScreenHeightInRawPixels);
                var minheight = screenSize.Height * 0.7;
                var minwidth = screenSize.Width * 0.7;
                if (e.Size.Height < minheight || e.Size.Width < minwidth)
                {
                    ApplicationView.GetForCurrentView().TryResizeView(new Size(minwidth, minheight));
                }
            }
    else{
    //150 and above resolution
var screenSize = new Size(displayInformation.ScreenWidthInRawPixels,
                           displayInformation.ScreenHeightInRawPixels);
            var minwidth = screenSize.Width / 1.5;
                 if (e.Size.Width < minwidth)
                {
                    var size = new Size(1100, 640);
                    ApplicationView.GetForCurrentView().SetPreferredMinSize(size);
                    Debug.WriteLine("resize staus: " + ApplicationView.GetForCurrentView().TryResizeView(size));
                }

           
        }

The above code is working fine in 100 and 125 resolution. But in 150(recommended) resolution user is able to minimize the application to lower size. Any idea how to handle the screen size on 150 resolution.


Solution

  • Please check this document.

    The smallest allowed minimum size is 192 x 48 effective pixels. The largest allowed minimum size is 500 x 500 effective pixels. If you set a value outside of these bounds, it is coerced to be within the allowed bounds. (To learn about effective pixels, see Introduction to Windows app design.)

    So, please edit your largest width 150 to 192.

    private void Button_Click(object sender, RoutedEventArgs e)
    {
       
    var size = new Size(192, 150);
    ApplicationView.GetForCurrentView().SetPreferredMinSize(size);
    ApplicationView.GetForCurrentView().TryResizeView(size);
    
    }
    

    If you want to set current windows size as 150*150, please set the ApplicationViewMode as CompactOverlay the give it CustomSize like the following

    var preferences = ViewModePreferences.CreateDefault(ApplicationViewMode.CompactOverlay);
    preferences.CustomSize = new Windows.Foundation.Size(150, 150);
    await ApplicationView.GetForCurrentView().TryEnterViewModeAsync(ApplicationViewMode.CompactOverlay, preferences);
    

    Update

    please check this remark part

    The requested size is larger than the available work area (no effect);

    1920*1080 Scale150Percent, available work area is 1280 * (720 - taskbarheight). when you set 600 * 800, TryResizeView has no effect. Because the app's window has larger than current work are. Please try to set it as 600 * 600.

    Update 1

    now its not allowing the user to shrink or restore down below 600,600. But the issue now is, its not allowing to maximize or expand more than 600,600 as well.

    Please add the maximize window width trigger condition like the following.

    var displayInformation = DisplayInformation.GetForCurrentView();
    if (displayInformation.ResolutionScale == ResolutionScale.Scale125Percent || displayInformation.ResolutionScale == ResolutionScale.Scale100Percent)
    {
        var screenSize = new Size(displayInformation.ScreenWidthInRawPixels,
                                  displayInformation.ScreenHeightInRawPixels);
        var minheight = screenSize.Height * 0.7;
        var minwidth = screenSize.Width * 0.7;
        if (e.Size.Height < minheight || e.Size.Width < minwidth)
        {
            ApplicationView.GetForCurrentView().TryResizeView(new Size(minwidth, minheight));
        }
    }
    else
    {
        var screenSize = new Size(displayInformation.ScreenWidthInRawPixels,
                                    displayInformation.ScreenHeightInRawPixels);
        var minwidth = screenSize.Width / 1.5;
        if (e.Size.Width < minwidth)
        {
            var size = new Size(600, 640);         
            ApplicationView.GetForCurrentView().TryResizeView(size);
        }
        //150 resolution
    }