最大化/调整大小以显示 Windows-10 C# UWP 应用程序的分辨率?
Maximize / Resize to Display Resolution a Windows-10 C# UWP App?
在以下版本的 App.xaml.cs 中,使用 Visual Studio 2019 编写,与我的 Windows C# UWP 解决方案/项目相关联 Example_Application、class应用程序的构造函数成功调整了启动应用程序时出现的蓝色应用程序 window 的大小。我的问题:假设分辨率比例为 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();
}
}
}
我尝试过的事情:
- 将 "PreferredLaunchViewSize" 更改为 "Maximized" 不会最大化我显示器上的蓝色 window。将 "PreferredLaunchViewSize" 更改为 "FullScreen" 确实会使我的应用程序占据全屏,但这不是我想要的,因为我希望能够看到我的应用程序标题栏和我的 Windows-10任务栏。
- 我只能在 OnLaunched 的最后写
Windows.Foundation.Rect visibleBounds = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().VisibleBounds;
,边界的宽度和高度属性 return 显示应用程序宽度和高度,而不是 Windows-10 显示分辨率。
- 只能在OnLaunched的最后写
uint screenWidthInRawPixels = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().ScreenWidthInRawPixels;
,screenWidthInRawPixels是当前应用宽度,不是Windows-10显示宽度。
要在 UWP 应用程序中获取屏幕分辨率,您可以尝试使用 DisplayInformation.ScreenHeightInRawPixels Property and DisplayInformation.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。
最终,我选择在我的工作区中最大化我的 UWP 应用程序(用 C# 编写),即我 Windows 任务栏上方的屏幕区域。我想提供最少的说明来创建您知道有效的最大化应用程序。
我在 Visual Studio 社区 2019 中使用 C# 创建了一个新的默认空白应用程序(通用 Windows),名为 "Draw Bounding Boxes"。我在此处添加了空格,以便我可以从“开始”菜单中使用空格访问 "Draw Bounding Boxes"。
我用下面的代码块替换了 "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
我在 "MainPage.xaml" 中的 <Page>
标签中添加了 属性 x:Name="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;
}
}
}
在以下版本的 App.xaml.cs 中,使用 Visual Studio 2019 编写,与我的 Windows C# UWP 解决方案/项目相关联 Example_Application、class应用程序的构造函数成功调整了启动应用程序时出现的蓝色应用程序 window 的大小。我的问题:假设分辨率比例为 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();
}
}
}
我尝试过的事情:
- 将 "PreferredLaunchViewSize" 更改为 "Maximized" 不会最大化我显示器上的蓝色 window。将 "PreferredLaunchViewSize" 更改为 "FullScreen" 确实会使我的应用程序占据全屏,但这不是我想要的,因为我希望能够看到我的应用程序标题栏和我的 Windows-10任务栏。
- 我只能在 OnLaunched 的最后写
Windows.Foundation.Rect visibleBounds = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().VisibleBounds;
,边界的宽度和高度属性 return 显示应用程序宽度和高度,而不是 Windows-10 显示分辨率。 - 只能在OnLaunched的最后写
uint screenWidthInRawPixels = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().ScreenWidthInRawPixels;
,screenWidthInRawPixels是当前应用宽度,不是Windows-10显示宽度。
要在 UWP 应用程序中获取屏幕分辨率,您可以尝试使用 DisplayInformation.ScreenHeightInRawPixels Property and DisplayInformation.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。
最终,我选择在我的工作区中最大化我的 UWP 应用程序(用 C# 编写),即我 Windows 任务栏上方的屏幕区域。我想提供最少的说明来创建您知道有效的最大化应用程序。
我在 Visual Studio 社区 2019 中使用 C# 创建了一个新的默认空白应用程序(通用 Windows),名为 "Draw Bounding Boxes"。我在此处添加了空格,以便我可以从“开始”菜单中使用空格访问 "Draw Bounding Boxes"。
我用下面的代码块替换了 "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
我在 "MainPage.xaml" 中的 <Page>
标签中添加了 属性 x:Name="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;
}
}
}