首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >最大化/调整大小以显示分辨率-10 C# UWP应用程序?

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

Stack Overflow用户
提问于 2019-11-21 15:23:07
回答 2查看 1.8K关注 0票数 0

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

代码语言:javascript
复制
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. 我只能在Windows.Foundation.Rect visibleBounds = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().VisibleBounds;的末尾编写OnLaunched,边界的宽度和高度属性返回当前应用程序的宽度和高度,而不是Windows-10显示分辨率。
  3. 我只能在uint screenWidthInRawPixels = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().ScreenWidthInRawPixels;的末尾编写OnLaunched,而screenWidthInRawPixels是当前的应用程序宽度,而不是Windows-10显示宽度。
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-05-13 16:48:37

最终,我选择在我的工作区中最大化我的UWP应用程序(用C#编写),我的屏幕上的那个区域在我的Windows任务栏之上。我想提供最低限度的指令,以创造一个最大化的应用程序,你知道是可行的。

我在VisualStudioCommunity2019中使用C#创建了一个新的默认空白应用程序(Universal ),名为“绘制边框”。我在这里添加了空格,这样我就可以从“开始”菜单中用空格访问“绘制边界框”。

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

代码语言:javascript
复制
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“中的<Page>标记中。

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

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

代码语言:javascript
复制
// 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;
        }
    }
}
票数 0
EN

Stack Overflow用户

发布于 2019-11-22 03:32:14

为了在UWP应用程序中获得屏幕分辨率,您可以尝试使用DisplayInformation.ScreenHeightInRawPixels性质DisplayInformation.ScreenWidthInRawPixels性质

类似于以下代码:

代码语言:javascript
复制
  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。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58978267

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档