最大化/调整大小以显示Windows-10 C#UWP应用的分辨率?

在以下版本的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”更改为“ Maximized”不会使我的显示器上的蓝色窗口最大化。将“ PreferredLaunchViewSize”更改为“ FullScreen”确实会使我的应用程序占据全屏,但这不是我想要的,因为我希望能够看到我的应用程序标题栏和Windows-10任务栏。
  2. 我只能在OnLaunched的最后写Windows.Foundation.Rect visibleBounds = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().VisibleBounds;,并且bounds的Width和Height属性返回当前应用程序的宽度和高度,而不是Windows-10的显示分辨率。
  3. 我只能在OnLaunched的最后写uint screenWidthInRawPixels = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().ScreenWidthInRawPixels;,而screenWidthInRawPixels是当前应用程序的宽度,而不是Windows-10的显示宽度。
lflxyz 回答:最大化/调整大小以显示Windows-10 C#UWP应用的分辨率?

要在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。

,

最终,我选择在我的工作区(Windows任务栏上方的屏幕区域)中最大化我的UWP App(用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,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

我在{MainPage.xaml“的x:Name="page"标签中添加了属性<Page>

我从MainPage.xaml中删除了<Grid> </Grid>环境。

我用以下代码块替换了“ 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;
        }
    }
}
本文链接:https://www.f2er.com/3056911.html

大家都在问