question

njsokalski avatar image
0 Votes"
njsokalski asked ·

Resize & Center a UWP App On Launch

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.



windows-uwp
· 3
10 |1000 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.

Hello, Have you solved the problem you encountered? If there are new questions, please let me know

0 Votes 0 ·

Hello, does the following answers solve your problem?

0 Votes 0 ·

hello, If you have any other questions, please feel free to contact us

0 Votes 0 ·
RichardZhang-MSFT avatar image
0 Votes"
RichardZhang-MSFT answered ·

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.

·
10 |1000 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.

jtorjo avatar image
0 Votes"
jtorjo answered ·

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





·
10 |1000 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.