以 xamarin 形式获取当前屏幕宽度

Get current screen width in xamarin forms

我正在创建一个 Xamarin Forms 项目,但我无法根据不同的设备查找当前屏幕宽度。

我知道如何在 android 和 ios 中完成,但需要一种方法来找到便携式的宽度。

你可以试试这个 Application.Current.MainPage.Width 在我的例子中这很有效,我能够在便携式项目本身中找到设备宽度。

您可以在 ContentPage 中使用宽度 属性。例如,

protected override void OnAppearing()
{
    base.OnAppearing();
    double w = this.Width;
}

请注意,这是在布局阶段设置的。所以,只能在页面初始化后才能使用,否则为-1。

https://developer.xamarin.com/api/property/Xamarin.Forms.VisualElement.Width/

常用代码(在App.xaml.cs)

public static int ScreenHeight {get; set;}
public static int ScreenWidth {get; set;}

Android部分(在MainActivity.cs,在OnCreate方法中)

App.ScreenHeight = (int) (Resources.DisplayMetrics.HeightPixels / Resources.DisplayMetrics.Density);
App.ScreenWidth = (int) (Resources.DisplayMetrics.WidthPixels / Resources.DisplayMetrics.Density);

iOS 部分(在 AppDelegate.cs 中,在 FinishedLaunching 方法中)

App.ScreenHeight = (int)UIScreen.MainScreen.Bounds.Height;
App.ScreenWidth = (int)UIScreen.MainScreen.Bounds.Width;

所以App.ScreenHeight和App.ScreenWidth会在应用启动时初始化,然后你就可以在公共代码的任何地方使用它们了。

我想在共享代码中创建一个静态结构,这样我就可以轻松访问屏幕 width/height 值(以及在 xAML 中进行绑定)。

app 命名空间中的结构(例如 App.xaml.cs):

using System;
using Xamarin.Forms;
namespace YourAppName {
    ...
    public struct Constant {
        public static double ScreenWidth = Application.Current.MainPage.Width;
        public static double ScreenHeight = Application.Current.MainPage.Height;
    }
}

在 C# 中检索屏幕宽度:

var labelWidth = Constant.ScreenHeight * 0.2;
// then do something with labelWidth

正在检索 XAML 中的屏幕宽度:

<Label Text="Text with insane font size" FontSize="{Binding Source={x:Static local:Constant.ScreenWidth}}"/>

另一种方法是为您的页面使用覆盖 "OnSizeAllocated",然后根据用户屏幕上的变化获取高度和宽度。您可以创建一个静态 class 来设置屏幕每次更改时的高度和宽度,然后在需要时从这个 class 访问它们。

设置和访问设备维度的静态class:

public static class ScreenDimensions
{
    public static double Height { get; set; }
    public static double Width { get; set; }
}

重写以获取屏幕尺寸:

protected override void OnSizeAllocated(double width, double height)
{
    base.OnSizeAllocated(width, height);
    ScreenDimensions.Height = height;
    ScreenDimensions.Width = width;
}

我知道这是一个旧话题,但值得一提的是最近 Xamarin.Essentials NuGet 包发布了,里面有一个有用的 class DeviceDisplay 可以为您处理这个任务.

可以找到文档 here

用法示例:

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

// Width (in xamarin.forms units)
var xamarinWidth = width / mainDisplayInfo.Density;

// Height (in pixels)
var height = mainDisplayInfo.Height;

// Screen density
var density = mainDisplayInfo.Density;

我想最好的方法是使用 xamarin.forms.Device 信息。

               Size size = Device.Info.PixelScreenSize;

            if (size.Width <= 720 && size.Height <= 1280)
            {
                stkLayoutTop.Margin = new Thickness(5, 30, 10, 0);
            }
            else
            {
                stkLayoutTop.Margin = new Thickness(5, 50, 10, 0);
            }