【问题标题】:Xamarin.Forms - change size of control in code behindXamarin.Forms - 在后面的代码中更改控件的大小
【发布时间】:2015-08-26 06:36:15
【问题描述】:

我想在 Xamarin.Forms 中通过控件后面的代码中的一些事件来更改控件的大小。

特别是,我想在设备方向改变时改变 StackLayout 的大小。

目前,我已经这样做了(但它不起作用):

private void SettingsPage_OnSizeChanged(object sender, EventArgs e)
{
    var isPortrait = UIHelpers.IsPortrait(this);
    if (isPortrait)
        RemoteServerSL.WidthRequest = RemoteServerSL.ParentView.Width;
    else
    {
        RemoteServerSL.WidthRequest = RemoteServerSL.ParentView.Width/2;
    }

    this.UpdateChildrenLayout();
    this.ForceLayout();
    RemoteServerSL.ForceLayout();

}

如果我处于横向模式,目的是将我的 StackLayout 的宽度更改为 parentWidth。

我该如何处理?

顺便说一句。我现在正在使用 Android。

【问题讨论】:

    标签: c# xamarin.forms


    【解决方案1】:

    您应该使用 OnSizeAllocated 方法覆盖来检测方向变化;

    double previousWidth;
    double previousHeight;
    
        protected override void OnSizeAllocated(double width, double height)
        {
            base.OnSizeAllocated(width, height);
    
            if (previousWidth != width || previousHeight != height)
            {
                previousWidth = width;
                previousHeight = height;
    
                if (width > height)
                {
                    // landscape mode
                }
                else
                {
                    // portrait mode
                }   
            }
        }
    

    【讨论】:

    • 在页面布局上运行良好。您知道为什么它不起作用,例如自定义 StackLayout?
    • 你能详细说明什么是“自定义 StackLayout”吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-05
    • 2012-06-08
    • 1970-01-01
    相关资源
    最近更新 更多