Xamarin.Forms 具有相对大小的滚动网格
Xamarin.Forms Scrolling Grid With Relative Sizes
我设法在使用 8 行行高 250(绝对)的 ScrollView 中创建了一个 GridView。这很好,直到我意识到它不能在不同分辨率的屏幕上工作。如何创建一个有 8 行的网格视图,但每行占据屏幕的一半,因此您必须向下滚动才能看到其余部分?使用 8 行高度“*”只是将 8 行放在视图上而无需滚动。我正在使用 xaml,但如果需要可以使用 c#。
您可以访问设备的屏幕高度,因此计算 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
。
重要提示:如果您在页面上允许设备旋转纵向和横向模式,您可能需要根据需要更新绑定。您可以通过相同的方式访问屏幕的宽度。
我设法在使用 8 行行高 250(绝对)的 ScrollView 中创建了一个 GridView。这很好,直到我意识到它不能在不同分辨率的屏幕上工作。如何创建一个有 8 行的网格视图,但每行占据屏幕的一半,因此您必须向下滚动才能看到其余部分?使用 8 行高度“*”只是将 8 行放在视图上而无需滚动。我正在使用 xaml,但如果需要可以使用 c#。
您可以访问设备的屏幕高度,因此计算 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
。
重要提示:如果您在页面上允许设备旋转纵向和横向模式,您可能需要根据需要更新绑定。您可以通过相同的方式访问屏幕的宽度。