处理Xamarin中的页面方向更改

时间:2017-06-27 09:50:49

标签: xamarin screen-orientation

使用Xamarin时,处理方向变化的最佳或最常用方法是什么?

我有一个旋转木马页面,它旋转了5个内容页面(同一页面,只是不同的文本/图像),在内容页面中我有一个6行网格,每行包含图像,文本或按钮。 / p>

从纵向更改为横向时,我会在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);
                }
            }
        }

1 个答案:

答案 0 :(得分:0)

通过对我所设置的内容进行一些调整,我的工作得很好,仍然会对其他人的看法感兴趣。