【问题标题】:How to detect device is big or small in xamarin forms如何以 xamarin 形式检测设备的大小
【发布时间】:2020-10-28 06:13:51
【问题描述】:

如何检测设备是小还是大? 我不需要检测是否是平板电脑。

这个概念取自https://devblogs.microsoft.com/xamarin/styling-for-multiple-device-resolutions/

我们使用下面的方法并相应地加载适当的样式,但是由于我没有考虑密度,因此并不准确。

如何改进或重写此方法,以便它给我更好的结果并更准确地检测设备的大小。

当前

const int smallWightResolution = 768;
const int smallHeightResolution = 1280;

public static bool IsASmallDevice()
{
        // Get Metrics
        var mainDisplayInfo = Xamarin.Essentials.DeviceDisplay.MainDisplayInfo;

        // Width (in pixels)
        var width = mainDisplayInfo.Width;

        // Height (in pixels)
        var height = mainDisplayInfo.Height;
        return (width <= smallWightResolution && height <= smallHeightResolution);
  }

尝试使用密度但不知道公式是什么

public static bool IsSmallDevice()
{ 
    //we don't support tablet so tablet don't apply.

    int smallWidthResolution = 768;
    int smallHeightResolution = 1280;
    double screenWidth;
    double screenHeight;
    bool isSmallDevice;
    
    var metrics = Xamarin.Essentials.DeviceDisplay.MainDisplayInfo;
    
    switch (Xamarin.Forms.Device.RuntimePlatform)
    {
        case Xamarin.Forms.Device.Android:
            //Android not sure how to sort of correctly detect if is a small device
            screenWidth = (metrics.Width - 0.5f) / metrics.Density;
            screenHeight = (metrics.Height - 0.5f) / metrics.Density;
            isSmallDevice = "???????";
            break;
        case Xamarin.Forms.Device.iOS:
            //ios no changes 
            isSmallDevice = metrics.Width <= smallWidthResolution
                            && metrics.Height <= smallHeightResolution;
            break;
    }

    return isSmallDevice;
}

更新

Huawei P9 Android 7.0
    Density=2.5
    Width=1080
    Height=2160
    ScreenHeight(Calculated)=864
    ScreenWidth(Calculated)=432


    Samsung A20 Android (new phone)
    Density=2
    Width=720
    Height=1560
    ScreenHeight(Calculated)=780
    ScreenWidth(Calculated)=360

有什么建议吗?

【问题讨论】:

标签: xamarin.forms xamarin.essentials


【解决方案1】:

回答

我建议使用Xamarin.Essentials NuGet Package 来检索屏幕的宽度、高度和密度,然后使用以下公式:

  • 屏幕宽度 = 宽度/密度
  • ScreenHeight = 高度/密度

代码

using Xamarin.Essentials;

// **** Example Screen Sizes ****
// iPhone SE 2nd Gen (aka iPhone 8), Width 750, Density 2, Width/Density 375
// iPhone 11, Width 828, Density 2, Width/Density 414
// Pixel 3, Width 1080, Density 2.75, Width/Density 392.7
// Galaxy S5, Width 1080, Density 3, Width/Density 360
// Galaxy Nexus, Width 720, Density 2, Width/Density 360
public static double ScreenWidth { get; } = DeviceDisplay.MainDisplayInfo.Width / DeviceDisplay.MainDisplayInfo.Density;
public static double ScreenHeight { get; } = DeviceDisplay.MainDisplayInfo.Height / DeviceDisplay.MainDisplayInfo.Density;

public static bool IsSmallScreen { get; } = ScreenWidth <= 360;

示例应用

这是我在GitTrends 应用程序中使用上述代码的方式: https://github.com/brminnick/GitTrends/blob/aa0c0a2d53dab7306514287533330b4a3bfbf609/GitTrends/Services/XamarinFormsService.cs#L11-L18

【讨论】:

  • 您好,感谢您的回复,不确定是不是我,但在我的测试中,我没有得到预期的结果。我有这款新的 Android 手机三星 A20 和旧的华为,它将旧手机检测为“大”,新手机检测为小但是如果我“强制”检测为大并加载不同的样式,新手机就可以了一它看起来不对!请参阅更新下的问题中的详细信息。查看数据,我得到的新手机更小,但处理得更好。希望我没有混淆,但我很困惑,因为如果您愿意,我需要一种可靠的方法来检测小型和大型手机。
  • 屏幕的物理尺寸(6" vs 4")与其屏幕密度(也就是每英寸的像素数)不同。作为移动应用程序开发人员,我们只需要担心屏幕密度,因为这就是我们的 Xamarin.Forms UI 在屏幕上呈现的方式。
  • 您也可以随意使用IsSmallScreen 并将其调整为最适合您的应用程序。我的设置为ScreenWidth &lt;= 360,但你可以将你的设置为ScreenWidth &lt;= 375
【解决方案2】:

您可以使用 Xamarin Forms 内置的 Idiom 来检测设备类型:

  1. 桌面

  2. 电话

  3. 平板电脑

  4. 电视

  5. 观看

    if (Device.Idiom == TargetIdiom.Phone) { // 你的代码在这里 }

【讨论】:

  • 感谢您的回复我只关心手机,不关心其他成语,如果手机大小我需要能够检测到它,以上对我不起作用
猜你喜欢
  • 2018-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-31
  • 2016-11-03
  • 1970-01-01
相关资源
最近更新 更多