iOS 和 Universal WINdows App 中的屏幕密度 [英] Density of screen in iOS and Universal WIndows App

查看:16
本文介绍了iOS 和 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,可与任何 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

    (博客)Xamarin.Essentials - https://blog.xamarin.com/xamarin-essentials-cross-platform-apis-mobile-apps/

    (文档)Essentials 入门 - https://docs.microsoft.com/en-us/xamarin/essentials/get-started

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

    Below, I'm storing the specific OS's screen details in static vars, located in the Xamarin Forms App 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) - 跳转到 Github 页面

    Note In all Windows examples screensize is the root project namespace

    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) - 跳转到 Github 页面

    // 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) - 跳转到 Github 页面

    // 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) - 跳转到 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:
    " +
                    $"Width: {DisplayScreenWidth}
    " +
                    $"Height: {DisplayScreenHeight}
    " +
                    $"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 App 中的屏幕密度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆