一般来说,改变一个windows app窗口大小有两种方法:
ApplicationView.PreferredLaunchViewSize = new Size(Width, Height);
ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;
或者:
ApplicationView.GetForCurrentView().TryResizeView(new Size{Width = xxx, Height = xxx });
前者用于在APP启动时,修改APP窗口的大小,后者可以在APP运行时,改变窗口大小。
那么,当你在APP运行时生成第二个窗口时,应该用第二种方法。但是需要注意的是,这行代码放在在生成第二个窗口的代码,或者放在新窗口的Page的InitializeComponent()中时,都会同时改变第一个窗口的大小,比较好的做法是,将这段代码放在新Page的Loaded方法里。
默认窗口通过Button事件,生成第二个窗口:
private async void OnClick(object sender, RoutedEventArgs e) { var newCoreAppView = CoreApplication.CreateNewView(); var appView = ApplicationView.GetForCurrentView(); await newCoreAppView.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Low, async () => { var window = Window.Current; var newAppView = ApplicationView.GetForCurrentView(); var frame = new Frame(); window.Content = frame; frame.Navigate(typeof(BlankPage)); window.Activate(); await ApplicationViewSwitcher.TryShowAsStandaloneAsync(newAppView.Id, ViewSizePreference.Default, appView.Id, ViewSizePreference.Default); }); }
第二个窗口:public BlankPage() { this.InitializeComponent(); this.Loaded += Page_Loaded; } private void Page_Loaded(object sender, RoutedEventArgs e) { var s = ApplicationView.GetForCurrentView(); s.TryResizeView(new Size { Width = 600, Height = 320 }); }