您可以访问设备的屏幕高度,因此计算 0.5 * 高度并将其设置为行高:
在 app.xaml.cs 中创建静态变量
static public int ScreenHeight;
然后你需要为 iOS 和 Android 设置变量的值。
Android:MainActivity.cs
protected override void OnCreate(Bundle bundle)
{
base.OnCreate (bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
App MyApp = new App();
App.ScreenHeight = (int)(Resources.DisplayMetrics.HeightPixels / Resources.DisplayMetrics.Density);
}
iOS:AppDelegate.cs
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
App.ScreenHeight = (int)UIScreen.MainScreen.Bounds.Height;
global::Xamarin.Forms.Forms.Init();
LoadApplication(new App());
return base.FinishedLaunching(app, options);
}
现在,在视图的 ViewModel 中有一个属性,它可以访问 ScreenHeight 并根据您的需要计算它
public int HalfScreenHeight
{
get { return App.ScreenHeight / 2; }
}
最后,您可以将行高设为HalfScreenHeight。
重要提示:如果您在页面上允许设备旋转纵向和横向模式,您可能需要根据需要更新绑定。你可以用同样的方法访问屏幕的宽度。