dmc*_*dmc 18 android xamarin.ios ios xamarin xamarin.forms
我正在尝试重写我为iOS编写的应用程序.我打算写一个Android版本,但认为最好让这个机会使用Xamarin.Forms.一次做一页,现在我被困在一个我需要获得屏幕宽度和高度的页面上.有没有人知道Xamarin.Forms中iOS的View.Frame.Width相当于什么?
kao*_*ick 31
有一种简单的方法可以获取屏幕的宽度和高度,Xamarin.Forms
并从应用程序的任何位置全局访问它.我会做这样的事情:
1.在App.cs中创建两个公共成员:
var width = DeviceDisplay.MainDisplayInfo.Width;
var height = DeviceDisplay.MainDisplayInfo.Height;
Run Code Online (Sandbox Code Playgroud)
2.设置 MainActivity.cs
(Android)或 AppDelegate.cs
(iOS)中的值:
安卓:
public static class App
{
public static int ScreenWidth;
public static int ScreenHeight;
...
}
Run Code Online (Sandbox Code Playgroud)
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
...
}
Run Code Online (Sandbox Code Playgroud)
如果您使用的是xamarin表单,那么您可以在便携式类库(pcl)中找到当前屏幕的宽度和高度,如下所示.
对于宽度,你在pcl中使用它,
Application.Current.MainPage.Width
Run Code Online (Sandbox Code Playgroud)
对于身高,你可以在pcl中做到这一点,
Application.Current.MainPage.Height
Run Code Online (Sandbox Code Playgroud)
目前 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
Run Code Online (Sandbox Code Playgroud)
无论显示属性如何,在所有平台上创建精确的逐英寸框架:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
要获取设备信息,请设置 DI 解析器或使用静态容器。所有 3 个平台都有一个带有静态 CurrentDevice 属性的单例设备调用:
resolverContainer.Register<IDevice>(t => WindowsPhoneDevice.CurrentDevice)
resolverContainer.Register<IDevice>(t => AppleDevice.CurrentDevice)
resolverContainer.Register<IDevice>(t => AndroidDevice.CurrentDevice)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
24388 次 |
最近记录: |