我正在尝试重写我为 iOS 编写的应用程序。我打算编写一个 android 版本,但认为最好让这个机会使用 Xamarin.Forms。一次做一页,现在我被困在需要获取屏幕宽度和高度的页面上。有谁知道 Xamarin.Forms 中相当于 iOS 的 View.Frame.Width?
6 回答
更新:您可以为此目的和许多其他目的使用Xamarin.Essentials nuget 包。
var width = DeviceDisplay.MainDisplayInfo.Width;
var height = DeviceDisplay.MainDisplayInfo.Height;
老答案:
有一种简单的方法可以获取屏幕的宽度和高度,Xamarin.Forms
并从应用程序的任何地方全局访问它。我会做这样的事情:
1. 在您的 App.cs 中创建两个公共成员:
public static class App
{
public static int ScreenWidth;
public static int ScreenHeight;
...
}
2. 在您的 MainActivity.cs
(Android) 或您的 AppDelegate.cs
(iOS) 中设置值:
安卓:
protected override void OnCreate(Bundle bundle)
{
...
App.ScreenWidth = (int)Resources.DisplayMetrics.WidthPixels; // real pixels
App.ScreenHeight = (int)Resources.DisplayMetrics.HeightPixels; // real pixels
// App.ScreenWidth = (int)(Resources.DisplayMetrics.WidthPixels / Resources.DisplayMetrics.Density); // device independent pixels
// App.ScreenHeight = (int)(Resources.DisplayMetrics.HeightPixels / Resources.DisplayMetrics.Density); // device independent pixels
...
}
IOS:
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
...
App.ScreenWidth = (int)UIScreen.MainScreen.Bounds.Width;
App.ScreenHeight = (int)UIScreen.MainScreen.Bounds.Height;
...
}
如果您使用的是 xamarin 表单,那么您可以在可移植类库(pcl)中找到当前屏幕的宽度和高度,如下所示。
对于宽度,您在 pcl 中使用它,
Application.Current.MainPage.Width
对于高度,您可以在 pcl 中执行此操作,
Application.Current.MainPage.Height
目前没有来自 Xamarin.Forms 本身的方法,但我们已将其实现为 Xamarin.Forms.Labs 中的 PCL 兼容接口,您可以从 NuGet 或 GitHub 的源代码获得。
https://github.com/XForms/Xamarin-Forms-Labs
IDevice 具有带有信息的 IDisplay 属性;X 和 Y 的高度、宽度、像素密度以及一些以英寸为单位计算尺寸的扩展方法。
从设备获取信息的示例页面:
#region Display information
var display = device.Display;
var displayFrame = new Frame();
if (display != null)
{
displayFrame.Content = new StackLayout()
{
Children =
{
new Label() { Text = display.ToString() },
new Label() { Text = string.Format("Screen width is\t {0:0.0} inches.", display.ScreenWidthInches()) },
new Label() { Text = string.Format("Screen height is\t {0:0.0} inches.", display.ScreenHeightInches()) },
new Label() { Text = string.Format("Screen diagonal size is\t {0:0.0} inches.", display.ScreenSizeInches()) }
}
};
}
else
{
displayFrame.Content = new Label() { TextColor = Color.Red, Text = "Device does not contain display information." };
}
stack.Children.Add(displayFrame);
#endregion
无论显示属性如何,在所有平台上创建精确的逐英寸帧:
public class AbsoluteLayoutWithDisplayInfoPage : ContentPage
{
public AbsoluteLayoutWithDisplayInfoPage(IDisplay display)
{
this.Title = "Absolute Layout With Display Info";
var abs = new AbsoluteLayout();
var inchX = display.WidthRequestInInches(1);
var inchY = display.HeightRequestInInches(1);
var originX = display.WidthRequestInInches(display.ScreenWidthInches() / 2);
var originY = display.HeightRequestInInches(display.ScreenHeightInches() / 2);
abs.Children.Add(new Label() { Text = "1\"x\"1\" blue frame" });
abs.Children.Add(new Frame()
{
BackgroundColor = Color.Navy,
},
new Rectangle(originX - inchX/2, originY - inchY/2, inchX, inchY));
abs.Children.Add(new Frame()
{
BackgroundColor = Color.White
},
new Rectangle(originX - inchX/16, originY - inchY/16, inchX/8, inchY/8));
this.Content = abs;
}
}
要获取设备信息,请设置您的 DI 解析器或使用静态容器。所有 3 个平台都有一个带有静态 CurrentDevice 属性的单例设备调用:
resolverContainer.Register<IDevice>(t => WindowsPhoneDevice.CurrentDevice)
resolverContainer.Register<IDevice>(t => AppleDevice.CurrentDevice)
resolverContainer.Register<IDevice>(t => AndroidDevice.CurrentDevice)
食谱
创建一个名为 ScreenSize 的新 Xamarin.Android 应用程序。编辑 Main.axml 使其包含两个 TextView:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:text="Screen Width:"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/screenWidthDp" />
<TextView
android:text="Screen Height:"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/screenHeightDp" />
</LinearLayout>
编辑Activity1.cs,将OnCreate中的代码改成如下:
![在此处输入图像描述][1] private int ConvertPixelsToDp(float pixelValue) { var dp = (int) ((pixelValue)/Resources.DisplayMetrics.Density); 返回DP;}
运行应用程序。根据设备,它将显示屏幕高度和宽度。以下屏幕截图来自 Galaxy Nexus:
为未来尝试执行此操作的开发人员更新,这些开发人员要么没有看过或错过它。Page 类继承自 VisualElement,它现在具有两个属性(恰好可以绑定以在 XAML 中使用):
Width - 获取此元素的当前渲染宽度。这是一个只读的可绑定属性。Height - 获取此元素的当前渲染高度。这是一个只读的可绑定属性。
在您的页面代码隐藏中,您只需执行以下操作:
var myWidth = this.Width;
var myHeight = this.Height;
如果您需要知道它是否发生变化,请使用 SizeChanged 事件:
public MyPage()
{
SizeChanged += OnSizeChanged;
}
private void OnSizeChanged(object sender, EventArgs e)
{
var myWidth = this.Width;
var myHeight = this.Height;
}
在您的 App 文件中定义一个公共静态变量
public static Size ScreenSize;
在调用 App 构造函数之前,您应该在 IOS 和 Android 中初始化变量。对于 IOS,在 FinishedLaunching 方法中
App.ScreenSize = new Size(UIScreen.MainScreen.Bounds.Width, UIScreen.MainScreen.Bounds.Height);
对于 Android,在 OnCreate 函数中
App.ScreenSize = new Xamarin.Forms.Size((int)(Resources.DisplayMetrics.WidthPixels / Resources.DisplayMetrics.Density), (int)(Resources.DisplayMetrics.HeightPixels / Resources.DisplayMetrics.Density));