【问题标题】:Density of screen in iOS and Universal WIndows AppiOS 和 Universal WINdows App 中的屏幕密度
【发布时间】:2017-05-20 06:17:01
【问题描述】:

您好,我需要知道 Windows 10/8/8.1 和 iOS 中的屏幕密度。 我使用 DisplayMetrics 在 Android 中获得了屏幕密度,但我发现 UWP 和 iOS 中没有这样的选项/属性。 那么是否有任何属性可以让我在 UWP 和 IOS 中获得屏幕密度。

【问题讨论】:

标签: xamarin.ios uwp xamarin.forms portable-class-library


【解决方案1】:

更新 - 2018 年 6 月 8 日

Xamarin 已发布 Xamarin.Essentials!

Xamarin.Essentials 为开发人员的移动应用程序提供跨平台 API。

Android、iOS 和 UWP 提供独特的操作系统和平台 API,开发人员可以利用 Xamarin 在 C# 中访问所有这些 API。 Xamarin.Essentials 提供了一个单一的跨平台 API,它适用于任何 Xamarin.Forms、Android、iOS 或 UWP 应用程序,无论用户界面是如何创建的,都可以从共享代码访问这些应用程序。 https://github.com/xamarin/Essentials


您现在可以查询设备的显示信息,而无需管理所有单独的平台代码片段。

设备显示信息 - https://docs.microsoft.com/en-us/xamarin/essentials/device-display

操作方法:

在您的类中添加对 Xamarin.Essentials 的引用:

using Xamarin.Essentials;

以下信息通过 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;

有用的链接:









原始帖子(在 Xamarin.Essentials 存在之前)

下面,我将特定操作系统的屏幕详细信息存储在位于 Xamarin Forms App 类中的 static 变量中

这些似乎都适用于我的测试。

(从我的 github 页面复制/粘贴 - https://github.com/mattregul/Xamarin_Screensize

Android (MainActivity.cs) - Jump to Github Page

// 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) - Jump to Github Page

// 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) - 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) - Jump to Github Page

// 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;

// 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 8.1 (App.xaml.cs) - Jump to Github Page

// Size of App's view
screensize.App.DisplayScreenHeight = Window.Current.Bounds.Height;
screensize.App.DisplayScreenWidth = Window.Current.Bounds.Width;
screensize.App.DisplayScaleFactor = 1; // No scaling here?  If you find a scaling for Windows 8.1, please let me know :)

Xamarin 表单页面 (App.cs) - Jump to Github Page

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);
        }

    }
}

【讨论】:

    【解决方案2】:

    此列表列出了 ios 设备 dpi 信息。你也可以使用GBDeviceInfo第三方工具来获取IOS设备的屏幕密度,比如[GBDeviceInfo deviceInfo].displayInfo.pixelsPerInch;

    IOS:

       iPhones
    iPhone 1/3G/3GS           320 x  480    163 ppi
    iPhone 4/4S               640 x  940    326 ppi
    iPhone 5/5C/5S            640 x 1136    326 ppi
    iPhone 6/6S               750 x 1334    326 ppi
    iPhone 6 Plus/6S Plus    1080 x 1920    401 ppi
      Without downsampling:  1242 x 2208    461 ppi
    
    Except for the 6th generation (= 5th) iPod touch,
    all the models are equal to their iPhone counterparts
    
    iPads
    iPad 1/2                  768 x 1024    132 ppi
    iPad 3/4/Air/Air 2       1536 x 2048    264 ppi
    iPad Mini 1               768 x 1024    163 ppi
    iPad Mini 2/3/4          1536 x 2048    326 ppi
    iPad Pro                 2737 x 2048    264 ppi
    
    Apple Watches
    Apple Watch 38 mm         272 x  340    326 ppi
    Apple Watch 42 mm         312 x  390    326 ppi
    

    我在 UWP 中找到了API 以检索设备 dpi。 Windows.Graphics.Display.DisplayInformation。你可以试试看。

    【讨论】:

    • 我使用了(double)UIScreen.MainScreen.Scale;,它总是返回一个。为什么??
    • 如何在 ios 中获取 xdpi 和 ydpi?
    • 默认情况下,MainScreen.Scale 的值始终为 1,因为您设备的原始显示设置。
    • 好的,那么我如何在 ios 中获取 xdpi 和 ydpi? @Nico Zhu - MSFT
    • 请尝试在您的ios项目中使用[GBDeviceInfo deviceInfo].displayInfo.pixelsPerInch api。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-22
    相关资源
    最近更新 更多