在 Xamarin 中处理页面方向变化
handling page orientation change in Xamarin
使用 Xamarin 时,处理方向变化的最佳或最可接受的方法是什么?
我有一个旋转木马页面,它轮流显示 5 个内容页面(同一页面,只是不同 text/images),在内容页面中我有一个 6 行网格,每行包含一个图像、文本或一个按钮。
从纵向更改为横向时,我在 OnSizeAllocated 中重置了它们的大小和填充。这似乎对 5 个内容页面中的 3 个随机工作,其他 2 个中的文本将错位,而且也不总是相同的 3 个工作。
我想有比手动调整大小更好的方法吗?
protected override void OnSizeAllocated(double width, double height)
{
base.OnSizeAllocated(width, height); //must be called
if (this.width != width || this.height != height)
{
this.width = width;
this.height = height;
//reconfigure layout
if (width > height)
{
img.HeightRequest = 62.5;
img.WidthRequest = 62.5;
logo.HeightRequest = 52.5;
logo.WidthRequest = 142.27;
headerStack.Padding = new Thickness(0, -60, 0, 0);
txtStack.Padding = new Thickness(20, -60, 20, 0);
sIndicator.Padding = new Thickness(20, 0, 20, 100);
sButton.Padding = new Thickness(0, -40, 0, 0);
}
else
{
img.WidthRequest = 250;
img.HeightRequest = 250;
logo.HeightRequest = 70;
logo.WidthRequest = 189.7;
headerStack.Padding = new Thickness(20, 40, 20, 0);
txtStack.Padding = new Thickness(20, 0, 20, 0);
sIndicator.Padding = new Thickness(20, 25, 20, 0);
}
}
}
对我的设置进行一些调整后,我的工作就很好了,我仍然会对其他人对他们如何做的看法感兴趣。
使用 Xamarin 时,处理方向变化的最佳或最可接受的方法是什么?
我有一个旋转木马页面,它轮流显示 5 个内容页面(同一页面,只是不同 text/images),在内容页面中我有一个 6 行网格,每行包含一个图像、文本或一个按钮。
从纵向更改为横向时,我在 OnSizeAllocated 中重置了它们的大小和填充。这似乎对 5 个内容页面中的 3 个随机工作,其他 2 个中的文本将错位,而且也不总是相同的 3 个工作。
我想有比手动调整大小更好的方法吗?
protected override void OnSizeAllocated(double width, double height)
{
base.OnSizeAllocated(width, height); //must be called
if (this.width != width || this.height != height)
{
this.width = width;
this.height = height;
//reconfigure layout
if (width > height)
{
img.HeightRequest = 62.5;
img.WidthRequest = 62.5;
logo.HeightRequest = 52.5;
logo.WidthRequest = 142.27;
headerStack.Padding = new Thickness(0, -60, 0, 0);
txtStack.Padding = new Thickness(20, -60, 20, 0);
sIndicator.Padding = new Thickness(20, 0, 20, 100);
sButton.Padding = new Thickness(0, -40, 0, 0);
}
else
{
img.WidthRequest = 250;
img.HeightRequest = 250;
logo.HeightRequest = 70;
logo.WidthRequest = 189.7;
headerStack.Padding = new Thickness(20, 40, 20, 0);
txtStack.Padding = new Thickness(20, 0, 20, 0);
sIndicator.Padding = new Thickness(20, 25, 20, 0);
}
}
}
对我的设置进行一些调整后,我的工作就很好了,我仍然会对其他人对他们如何做的看法感兴趣。