iOS和Universal WIndows应用程序中的屏幕密度 [英] Density of screen in iOS and Universal WIndows App
问题描述
我需要知道Windows 10/8/8.1和iOS中的屏幕密度. 我使用DisplayMetrics在Android中获得了屏幕密度,但是在UWP和iOS中找不到这样的选项/属性. 那么,是否有任何属性可以用来获取UWP和IOS中的屏幕密度.
Hi I need to know the density of screen in windows 10/8/8.1 and iOS. I got screen density in Android using DisplayMetrics but I find no such option/property available in UWP and iOS. So Is there any property through which I can get screen density in UWP and IOS.
推荐答案
更新-2018年6月8日
UPDATE - June 8th 2018
Xamarin.Essentials为开发人员的移动应用程序提供了跨平台API.
Xamarin.Essentials provides developers with cross-platform APIs for their mobile applications.
Android,iOS和UWP提供了独特的操作系统和平台API,开发人员可以利用Xamarin在C#中访问所有API. Xamarin.Essentials提供了一个跨平台API,该API可与任何Xamarin.Forms,Android,iOS或UWP应用程序一起使用,无论用户界面如何创建,都可以从共享代码中对其进行访问. https://github.com/xamarin/Essentials
Android, iOS, and UWP offer unique operating system and platform APIs that developers have access to all in C# leveraging Xamarin. Xamarin.Essentials provides a single cross-platform API that works with any Xamarin.Forms, Android, iOS, or UWP application that can be accessed from shared code no matter how the user interface is created. https://github.com/xamarin/Essentials
您现在可以查询设备的显示信息,而不必管理所有单独的平台代码段.
You can now query the device's display information without having to manage all of the individual platform code pieces.
设备显示信息- https://docs.microsoft.com/en-us/xamarin/essentials/device-display
在您的课程中添加对Xamarin.Essentials的引用:
Add a reference to Xamarin.Essentials in your class:
using Xamarin.Essentials;
以下信息通过API公开:
The following information is exposed through the API:
// Get Metrics
var mainDisplayInfo = DeviceDisplay.MainDisplayInfo;
// Orientation (Landscape, Portrait, Square, Unknown)
var orientation = mainDisplayInfo.Orientation;
// Rotation (0, 90, 180, 270)
var rotation = mainDisplayInfo.Rotation;
// Width (in pixels)
var width = mainDisplayInfo.Width;
// Height (in pixels)
var height = mainDisplayInfo.Height;
// Screen density
var density = mainDisplayInfo.Density;
有用链接:
-
(Github) Xamarin.Essentials - https://github.com /xamarin/Essentials
Useful Links:
(Github) Xamarin.Essentials - https://github.com/xamarin/Essentials
(博客) Xamarin.Essentials - https://blog.xamarin.com/xamarin-essentials-cross-platform-apis-mobile-apps/
(文档) Essentials入门- https://docs.microsoft.com/zh-cn/xamarin/essentials/get-started
下面,我将特定操作系统的屏幕详细信息存储在Xamarin Forms
App
类中的static
vars中Below, I'm storing the specific OS's screen details in
static
vars, located in the Xamarin FormsApp
class这些似乎都可以在我的测试中正常工作.
These all appear to work in my testing.
(从我的github页面复制/粘贴- https://github.com/mattregul/Xamarin_Screensize )
// Store off the device sizes, so we can access them within Xamarin Forms // Screen Width = WidthPixels / Density // Screen Height = HeightPixels / Density App.DisplayScreenWidth = (double)Resources.DisplayMetrics.WidthPixels / (double)Resources.DisplayMetrics.Density; App.DisplayScreenHeight = (double)Resources.DisplayMetrics.HeightPixels / (double)Resources.DisplayMetrics.Density; App.DisplayScaleFactor = (double)Resources.DisplayMetrics.Density;
iOS(AppDelegate.cs)-跳转到Github页面
// Store off the device sizes, so we can access them within Xamarin Forms App.DisplayScreenWidth = (double)UIScreen.MainScreen.Bounds.Width; App.DisplayScreenHeight = (double)UIScreen.MainScreen.Bounds.Height; App.DisplayScaleFactor = (double)UIScreen.MainScreen.Scale;
注意在所有Windows示例中,
screensize
是根项目名称空间UWP(App.xaml.cs)-
Note In all Windows examples
screensize
is the root project namespaceUWP (App.xaml.cs) - Jump to Github Page
// You decided which is best for you... // Do you want Size of the App's View // or // Do you want the Display's resolution // ###################################### // Size of App's view screensize.App.DisplayScreenHeight = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().VisibleBounds.Height; screensize.App.DisplayScreenWidth = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().VisibleBounds.Width; // Size of screen's resolution //screensize.App.DisplayScreenWidth = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().ScreenHeightInRawPixels; //screensize.App.DisplayScreenHeight = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().ScreenWidthInRawPixels; // Pixels per View Pixel // - https://msdn.microsoft.com/en-us/windows/uwp/layout/design-and-ui-intro#effective-pixels-and-scaling // - https://msdn.microsoft.com/en-us/windows/uwp/layout/screen-sizes-and-breakpoints-for-responsive-design screensize.App.DisplayScaleFactor = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
Windows Phone 8.1(App.xaml.cs)-
Windows 8.1(App.xaml.cs)-
Xamarin表单页面(App.cs)-跳转到Github页面
namespace screensize { public class App : Application { public static double DisplayScreenWidth = 0f; public static double DisplayScreenHeight = 0f; public static double DisplayScaleFactor = 0f; public App() { string ScreenDetails = Device.OS.ToString() + " Device Screen Size:\n" + $"Width: {DisplayScreenWidth}\n" + $"Height: {DisplayScreenHeight}\n" + $"Scale Factor: {DisplayScaleFactor}"; // The root page of your application var content = new ContentPage { Title = "Xamarin_GetDeviceScreensize", Content = new StackLayout { VerticalOptions = LayoutOptions.Center, Children = { new Label { HorizontalTextAlignment = TextAlignment.Center, FontSize = Device.GetNamedSize (NamedSize.Large, typeof(Label)), Text = ScreenDetails } } } }; MainPage = new NavigationPage(content); } } }
这篇关于iOS和Universal WIndows应用程序中的屏幕密度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!