【问题标题】:Setting window size on desktop for a Windows 10 UWP app在桌面上为 Windows 10 UWP 应用设置窗口大小
【发布时间】:2015-10-31 09:15:01
【问题描述】:

我刚刚开始使用 Visual Studio 2015 Community Edition 在 Windows 10 Pro 上学习 UWP 应用开发。我尝试通过在 MainPage.xaml 中设置 Page 标记的 WidthHeight 属性来修改 C# version of the official "Hello, World!" sample

有趣的是,当我启动应用程序时,它的大小会有所不同。此外,如果我调整它的窗口大小然后重新启动它,应用程序似乎会记住它之前的窗口大小。

是否可以强制 UWP 应用具有预定义的窗口大小,至少在台式电脑上是这样?

【问题讨论】:

    标签: c# window win-universal-app windows-10 uwp


    【解决方案1】:

    尝试在您的MainPage构造函数 中设置PreferredLaunchViewSize,如下所示:

    public MainPage()
    {
        this.InitializeComponent();
    
        ApplicationView.PreferredLaunchViewSize = new Size(480, 800);
        ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;
    }
    

    正如@kol 还指出的那样,如果您想要任何小于默认500x320 的尺寸,则需要手动重置它:

    ApplicationView.GetForCurrentView().SetPreferredMinSize(new Size(200, 100));
    

    【讨论】:

    • 很有趣,谢谢。是否可以在 XAML 中设置?
    • 想一想,不是在主页上设置大小,而是在主页上设置'parent's parent,也就是Frame's parent。我认为没有直接的 xaml 访问权限。
    • 这工作:ApplicationView.GetForCurrentView().SetPreferredMinSize(new Size(200, 100));
    • 如果您要使用 ApplicationView 类,您需要在命名空间声明上方包含“使用 Windows.UI.ViewManagement”。
    • 为什么不把它放在 App.xaml.cs 中的OnLaunched(LaunchActivatedEventArgs e) { } 中,因为它更多地与应用程序本身有关,而不是与主页面有关? (如果我错了,请纠正我)
    【解决方案2】:

    您无法真正控制窗口大小,即使您尝试重新调整它的大小,它也可能会失败。我在 MSDN 论坛上问过同样的问题,并在这里得到了答案:

    Windows 10 universal DirectX application

    顺便说一句,这是您的事件处理程序“OnLaunched”或事件处理程序“OnActivated”中的解决方案:

    Window.Current.Activate();
    

    并将其替换为:

    float DPI = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().LogicalDpi;
    
    Windows.UI.ViewManagement.ApplicationView.PreferredLaunchWindowingMode = Windows.UI.ViewManagement.ApplicationViewWindowingMode.PreferredLaunchViewSize;
    
    var desiredSize = new Windows.Foundation.Size(((float)800 * 96.0f / DPI), ((float)600 * 96.0f / DPI));
    
    Windows.UI.ViewManagement.ApplicationView.PreferredLaunchViewSize = desiredSize;
    
    Window.Current.Activate();
    
    bool result = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().TryResizeView(desiredSize);
    

    最好将此代码放入“OnActivated()”事件处理程序中,因为它会在应用启动时设置您定义的大小,并在任何中断后变为活动状态。

    在“desiredSize”计算中,800 是宽度,600 是高度。这个计算是必须的,因为大小是以 DPI 为单位的,所以你必须将它从像素转换为 DPI。

    另外请记住,尺寸不能小于“320x200”。

    【讨论】:

    • 仅链接的答案不是 SO 的方式。链接可能有一天会过时。在你的答案中加入重要信息!
    【解决方案3】:

    对于第一个应用程序启动,ApplicationView.PreferredLaunchWindowingMode 设置为 ApplicationViewWindowingMode.Auto,无论您在代码中设置什么。

    但是,从this question on MSDN 开始,可能有办法克服这个问题。其中一个答案提供了一种设置第一个启动大小的方法(之后恢复为Auto)。

    如果您的目标是只在 PreferredLaunchViewSize 上启动一次,您可以使用这个粗鲁的解决方案(由您自己决定以您的编码风格更好地实现!:P)

    public MainPage()
    {
        this.InitializeComponent();
    
        var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
            if (localSettings.Values["launchedWithPrefSize"] == null)
            {
                // first app launch only!!
                ApplicationView.PreferredLaunchViewSize = new Size(100, 100);
                ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;
                localSettings.Values["launchedWithPrefSize"] = true;
            }
            // resetting the auto-resizing -> next launch the system will control the PreferredLaunchViewSize
            ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.Auto;
        }
    }
    

    附:我没有测试过。

    【讨论】:

    • 该方法不起作用,因为“第一个”页面将在进入此构造函数之前启动
    【解决方案4】:

    在 StackOverflow 的另一个链接中,还有另一种方法:https://stackoverflow.com/a/68583688/5993426。这段代码是插入到 App.xaml 中的:

        protected override void OnWindowCreated(WindowCreatedEventArgs args)
        {
            SetWindowMinSize(new Size(args.Window.Bounds.Width, args.Window.Bounds.Height));
            args.Window.CoreWindow.SizeChanged += CoreWindow_SizeChanged;
            base.OnWindowCreated(args);
        }
    
        private void CoreWindow_SizeChanged(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.WindowSizeChangedEventArgs args)
        {
            if (SetWindowMinSize(args.Size))
            {
                sender.ReleasePointerCapture();
            }            
        }
    
        private bool SetWindowMinSize(Size size)
        {
            if (size.Width < minWidth || size.Height < minHeight)
            {
                if (size.Width < minWidth) size.Width = minWidth + 10;
                if (size.Height < minHeight) size.Height = minHeight + 10;
                return ApplicationView.GetForCurrentView().TryResizeView(size);
            }
            return false;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-12
      • 1970-01-01
      • 2016-01-10
      • 1970-01-01
      • 2015-09-05
      • 2020-10-14
      • 2013-10-02
      相关资源
      最近更新 更多