Resize & Center a UWP App On Launch

Nathan Sokalski 4,116 Reputation points
2020-02-15T04:30:02.187+00:00

I have a UWP app which, when launched, I want to assign a fixed width, horizontally center, and vertically stretch to the size of the screen (excluding the Task Bar). I would also like to keep it this size & position, although I realize that this may not be allowed. I have tried the following in App.xaml.vb immediately after Window.Current.Activate() in the OnLaunched method:

ApplicationView.GetForCurrentView().TryResizeView(New Size(560, ApplicationView.GetForCurrentView().VisibleBounds.Height))

This works fine for the width, but the height (which is returning 900 on my machine) is not the full height (even without the Task Bar). If I add a value to this height, it increases the height, but the maximum that it allows me to add is 20, which still does not fill the full height.

Here is the basic list of things I need to do (that I have not figured out):

  1. Stretch the app to the full height of the screen (without the Task Bar)
  2. Horizontally center the app
  3. Prevent the app from being resized (if possible, otherwise, I would like to come up with an easy way for the user to automatically resize & center it, such as a Button)

Is there any way to do these things? Thanks.

Universal Windows Platform (UWP)
{count} votes

2 answers

Sort by: Most helpful
  1. Richard Zhang-MSFT 6,936 Reputation points
    2020-02-17T01:38:28.12+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    In UWP, users have the right to resize the window arbitrarily. As a developer, we should not and cannot restrict the right to resize the window.

    In my tests, through ApplicationView.GetForCurrentView().TryResizeView method, the height can only be set to 999 at most. It can be seen that there are also restrictions in this regard.

    At the same time, there is currently no API to control the display position of applications on the desktop.

    Thanks.

    0 comments No comments

  2. John Torjo 861 Reputation points
    2020-02-21T08:12:57.307+00:00

    Welcome to UWP, Microsoft's pride and joy, where even the most mundane tasks feel like brain surgery.

    I have a workaround for this - I found it somwhere on github:

    // https://github.com/stammen/WindowSizing  
    public class try_resize_main_window {  
        private readonly Page page_;  
        private ThreadPoolTimer m_timer;  
    
        private bool resized_already_ = false;  
    
        public try_resize_main_window(Page page) {  
            page_ = page;  
            page.Loaded += on_loaded;  
        }  
    
        public void OnWindowSizeChanged(object sender, WindowSizeChangedEventArgs e)  
        {  
            if (m_timer != null)  
            {  
                m_timer.Cancel();  
                m_timer = null;  
            }  
    
            TimeSpan period = TimeSpan.FromSeconds(1.0);  
            m_timer = ThreadPoolTimer.CreateTimer(async (source) => {  
                if (!resized_already_)  
                    await page_.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => {  
                        if (!resized_already_) {  
                            resized_already_ = true;  
                            ApplicationView.GetForCurrentView().TryResizeView(ui_util.preferred_launch_size());  
                            m_timer.Cancel();  
                        }  
                    });  
                else  
                    m_timer.Cancel();  
    
            }, period);  
        }  
    
        private void on_loaded(object sender, RoutedEventArgs e)  
        {  
            var result = ApplicationView.GetForCurrentView().TryResizeView(ui_util.preferred_launch_size());  
            Debug.WriteLine("OnLoaded TryResizeView: " + result);  
            page_.Loaded -= on_loaded;  
        }  
    }  
    

    I would use it like this: on my main page class's constructor, I would do this:

    public partial class main_ui_window:Page {  
        private try_resize_main_window try_resize_;  
        public main_ui_window() {  
            InitializeComponent();  
    		try_resize_ = new try_resize_main_window(this);  
    	}  
        protected override void OnNavigatedTo(NavigationEventArgs e)  
        {  
            base.OnNavigatedTo(e);  
    		Window.Current.SizeChanged += try_resize_.OnWindowSizeChanged;  
        }  
    
        protected override void OnNavigatedFrom(NavigationEventArgs e)  
        {  
            base.OnNavigatedFrom(e);  
    		Window.Current.SizeChanged -= try_resize_.OnWindowSizeChanged;  
        }  
    	...  
    

    Hope this helps.

    Best,
    John

    0 comments No comments