【问题标题】:Maximize / Resize to Display Resolution a Windows-10 C# UWP App?最大化/调整大小以显示 Windows-10 C# UWP 应用程序的分辨率?
【发布时间】:2020-03-17 13:59:30
【问题描述】:

在以下版本的 App.xaml.cs 中,使用 Visual Studio 2019 编写,与我的 Windows C# UWP 解决方案/名为 Example_Application 的项目相关联,类 App 的构造函数成功调整了启动应用程序时出现的蓝色应用程序窗口的大小。我的问题:假设分辨率比例为 1,只是为了让事情更容易,我如何将 1920 和 1080 更改为构成我的 Windows-10 显示分辨率的两个数字?

namespace Example_Application
{
    sealed partial class App : Windows.UI.Xaml.Application
    {
        public App()
        {
            Windows.UI.ViewManagement.ApplicationView.PreferredLaunchViewSize = new Windows.Foundation.Size(1920, 1080);
            Windows.UI.ViewManagement.ApplicationView.PreferredLaunchWindowingMode = Windows.UI.ViewManagement.ApplicationViewWindowingMode.PreferredLaunchViewSize;
        }

        protected override void OnLaunched(Windows.ApplicationModel.Activation.LaunchActivatedEventArgs e)
        {
            Windows.UI.Xaml.Controls.Frame rootFrame = new Windows.UI.Xaml.Controls.Frame();
            Windows.UI.Xaml.Window.Current.Content = rootFrame;
            rootFrame.Navigate(typeof(MainPage), e.Arguments);
            Windows.UI.Xaml.Window.Current.Activate();
        }
    }
}

我尝试过的事情:

  1. 将“PreferredLaunchViewSize”更改为“最大化”不会最大化我的显示器上的蓝色窗口。将“PreferredLaunchViewSize”更改为“FullScreen”确实会使我的应用程序占据全屏,但这不是我想要的,因为我希望能够看到我的应用程序标题栏和 Windows-10 任务栏。
  2. 我只能在 OnLaunched 的最后写 Windows.Foundation.Rect visibleBounds = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().VisibleBounds;,并且边界的 Width 和 Height 属性返回当前应用程序的宽度和高度,而不是 Windows-10 显示分辨率。
  3. 我只能在 OnLaunched 的最后写 uint screenWidthInRawPixels = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().ScreenWidthInRawPixels;,而 screenWidthInRawPixels 是当前应用程序的宽度,而不是 Windows-10 的显示宽度。

【问题讨论】:

    标签: c# uwp resize display maximize


    【解决方案1】:

    要在 UWP 应用中获取屏幕分辨率,您可以尝试使用 DisplayInformation.ScreenHeightInRawPixels PropertyDisplayInformation.ScreenWidthInRawPixels Property

    如下代码:

      protected override void OnLaunched(LaunchActivatedEventArgs e)
        {
            Frame rootFrame = Window.Current.Content as Frame;
               ........
                Window.Current.Activate();
            }
    
            //screen resolution
            string heightsize = DisplayInformation.GetForCurrentView().ScreenHeightInRawPixels.ToString();
            string widthsize = DisplayInformation.GetForCurrentView().ScreenWidthInRawPixels.ToString();
            Size mysize = new Size(Convert.ToDouble(widthsize), Convert.ToDouble(heightsize));
    
    
            ApplicationView.PreferredLaunchViewSize = mysize;
            ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;
        }
    

    我的分辨率是 1920*1080。在我的测试中,它可以正确地让我的屏幕分辨率为 1920*1080。

    【讨论】:

    • 罗伊,谢谢你的建议。我相信我在“我尝试过的事情”下的第三项中提到了尝试这个。虽然也许这些定义确实检索了显示分辨率数字,但我相信这些定义只有在将 MainPage 分配给应用程序窗口后才能实现,这不允许调整应用程序窗口的大小。如果我没有在正确的地方实施你的建议,你会推荐一个插入点吗?如果您愿意,您是否可以使用 HelloWorld 类型的应用程序尝试您的建议,如果您让窗口调整大小以填满屏幕,请告诉我?
    • @TomLever,我编辑了我的答案。我在 OnLaunched 事件结束时使用这些代码。是的,这是一个简单的空白应用程序。但请注意,PreferredLaunchViewSize 将在您第二次启动应用程序时起作用。你能接受吗?
    • 罗伊,你让我重回正轨。在我构建解决方案并开始两次之后,您的解决方案对我有用。谢谢。知道为什么需要两次启动吗?
    • @TomLever PreferredLaunchViewSize PreferredLaunchViewSize 模式的代码可以在应用程序的构造方法中首次启动应用程序时工作。这种行为的原因是当我们调用DisplayInformation 类来获取屏幕分辨率时,我们需要确保ApplicationView(当前线程的CoreApplicationView 的单个实例)被激活。这意味着这个DisplayInformation 类必须在Window.Current.Activate() 之后调用。
    • @TomLever 所以我们只能在 OnLaunched 方法结束时获得正确的分辨率,并在窗口显示后将其设置为首选大小。然后它将第二次工作,因为这次设置了首选大小。
    【解决方案2】:

    最终,我选择在我的工作区(Windows 任务栏上方的屏幕区域)中最大化我的 UWP 应用程序(用 C# 编写)。我想提供一些关于创建您知道可以运行的最大化应用程序的最少说明。

    我在 Visual Studio Community 2019 中使用 C# 创建了一个新的默认空白应用程序(通用 Windows),称为“绘制边界框”。我在此处添加了空格,以便我可以从“开始”菜单中访问带有空格的“绘制边界框”。

    我用以下代码块替换了“App.xaml.cs”的内容。

    namespace Draw_Bounding_Boxes
    {
        /// <summary>
        /// Provides application-specific behavior to supplement the default Application class.
        /// </summary>
        sealed partial class App : Windows.UI.Xaml.Application
        {
            /// <summary>
            /// Invoked when the application is launched normally by the end user.  Other entry points
            /// will be used such as when the application is launched to open a specific file.
            /// </summary>
            /// <param name="e">Details about the launch request and process.</param>
            protected override void OnLaunched(Windows.ApplicationModel.Activation.LaunchActivatedEventArgs e)
            {
                // Resize app.
                uint screenWidthInRawPixels = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().ScreenWidthInRawPixels;
                uint screenHeightInRawPixels = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().ScreenHeightInRawPixels;
                double rawPixelsPerViewPixel = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
                double screenWidthInViewPixels = System.Convert.ToDouble(screenWidthInRawPixels) / rawPixelsPerViewPixel;
                double screenHeightInViewPixels = System.Convert.ToDouble(screenHeightInRawPixels) / rawPixelsPerViewPixel;
    
                // If offsetToScreenWidthInViewPixels is less than 15,
                // on first load app will be of default size, and on second load app will be full screen.
                // A loaded image will have height equal to full screen height minus app title bar height minus app toolbar height minus 5 view pixels of padding.
                // Part of a loaded image with aspect ratio less than one will be behind Windows taskbar.
                // This is all very complicated and undesirable.
                // If offsetToScreenHeightInViewPixels is less than 40,
                // on first load app will be of default size, and on second load app will be full screen.
                // A loaded image will have height equal to full screen height minus app title bar height minus app toolbar height minus 5 view pixels of padding.
                // Part of a loaded image with aspect ratio less than one will be behind Windows taskbar.
                // This is all very complicated and undesirable.
                // If offsetToScreenWidthInViewPixels is greater than or equal to 15 and offsetToScreenHeightInViewPixels is greater than or equal to 40,
                // on first load app will be of PreferredLaunchViewSize, and a loaded image with aspect ratio less than one will have height exactly equal to height of app minus app title bar height minus app toolbar height.
                // If PreferredLaunchViewSize.Height is only screenHeightInViewPixels - offsetToScreenHeightInViewPixels,
                // part of app and a loaded image with aspect ratio less than one will be behind taskbar.
                // If taskbarHeight is taken off of screenHeightInViewPixels - offsetToScreenHeightInViewPixels,
                // bottom of app and coincident bottom of loaded image will be slightly above taskbar.
                // I consider this ideal.
                double offsetToScreenWidthInViewPixels = 15;
                double offsetToScreenHeightInViewPixels = 40;
                double taskbarHeight = 40;
                Windows.UI.ViewManagement.ApplicationView.PreferredLaunchViewSize = new Windows.Foundation.Size(screenWidthInViewPixels - offsetToScreenWidthInViewPixels, screenHeightInViewPixels - offsetToScreenHeightInViewPixels - taskbarHeight);
                Windows.UI.ViewManagement.ApplicationView.PreferredLaunchWindowingMode = Windows.UI.ViewManagement.ApplicationViewWindowingMode.PreferredLaunchViewSize;
    
                // Set the app window to a new Frame.
                Windows.UI.Xaml.Controls.Frame rootFrame = new Windows.UI.Xaml.Controls.Frame();
                Windows.UI.Xaml.Window.Current.Content = rootFrame;
    
                // Navigate the frame to the initial default page.
                rootFrame.Navigate(typeof(MainPage), e.Arguments);
    
                // Attempts to activate the application window by bringing it to the foreground and setting the input focus to it.
                Windows.UI.Xaml.Window.Current.Activate();
    
            } // protected override void OnLaunched
        } // sealed partial class App
    } // namespace Draw_Bounding_Boxes
    

    我将属性x:Name="page" 添加到“MainPage.xaml”中的&lt;Page&gt; 标记中。

    我从 MainPage.xaml 中删除了 &lt;Grid&gt; &lt;/Grid&gt; 环境。

    我用以下代码块替换了“MainPage.xaml.cs”的内容。

    // Create namespace Draw_Bounding_Boxes to contain all classes associated with our app.
    namespace Draw_Bounding_Boxes
    {
        // Create class MainPage that inherits fields and methods from Windows.UI.Xaml.Controls.Page and
        // is used to declare and define user-interface elements and functionality.
        public sealed partial class MainPage : Windows.UI.Xaml.Controls.Page
        {
            // Create constructor public MainPage.
            public MainPage()
            {
                // Necessary to instantiate this Page, add a stackPanel to this Page, et cetera.
                this.InitializeComponent();
    
                // Find width of app in view pixels and height between bottom of app and bottom of title bar in view pixels.
                double widthOfAppInViewPixels = Windows.UI.ViewManagement.ApplicationView.PreferredLaunchViewSize.Width;
                double heightBetweenBottomOfAppAndBottomOfTitleBarInViewPixels = Windows.UI.ViewManagement.ApplicationView.PreferredLaunchViewSize.Height;
    
                // Create a stackPanel.
                Windows.UI.Xaml.Controls.StackPanel stackPanel = new Windows.UI.Xaml.Controls.StackPanel();
    
                // Create a toolbar with width equal to the width of the app, height equal to 50 view pixels, and background color of light blue that has one row and four columns.
                Windows.UI.Xaml.Controls.Grid toolbar = new Windows.UI.Xaml.Controls.Grid();
                toolbar.Width = widthOfAppInViewPixels;
                toolbar.Height = 50;
                toolbar.Background = new Windows.UI.Xaml.Media.SolidColorBrush(Windows.UI.Colors.AliceBlue);
                Windows.UI.Xaml.Controls.RowDefinition row = new Windows.UI.Xaml.Controls.RowDefinition();
                toolbar.RowDefinitions.Add(row);
                Windows.UI.Xaml.Controls.ColumnDefinition column = new Windows.UI.Xaml.Controls.ColumnDefinition();
                column.Width = new Windows.UI.Xaml.GridLength(widthOfAppInViewPixels);
                toolbar.ColumnDefinitions.Add(column);
    
                stackPanel.Children.Add(toolbar);
    
                page.Content = stackPanel;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 2014-06-27
      • 1970-01-01
      • 2022-11-13
      相关资源
      最近更新 更多