【问题标题】:Xamarin Create square layout using screen sizeXamarin 使用屏幕大小创建方形布局
【发布时间】:2019-11-24 15:54:43
【问题描述】:

我有问题。我想创建一个具有屏幕尺寸宽度和高度的 StackLayout,因此它将是一个正方形。我已经试过了:

public Page1()
{
    InitializeComponent();

    imageLayout.HeightRequest = Application.Current.MainPage.Width;
    imageLayout.WidthRequest = Application.Current.MainPage.Width;
}

但这给了我Application.Current.MainPage 为空的错误。

我该如何解决这个问题?

【问题讨论】:

    标签: c# xamarin xamarin.forms xamarin.android xamarin.ios


    【解决方案1】:

    我想我会使用您页面中的 SizeChanged 事件:

    public Page1()
    {
        InitializeComponent();
    
        SizeChanged += (s,a) =>
        {
            imageLayout.HeightRequest = this.Width;
            imageLayout.WidthRequest = this.Width;
        };
    
    }
    

    【讨论】:

    【解决方案2】:

    出于这些目的,您应该覆盖 OnSizeAllocated

    protected override void OnSizeAllocated(double width, double height)
        {
            base.OnSizeAllocated(width, height);
            imageLayout.WidthRequest= width;
            imageLayout.HeightRequest = width;
        }
    

    更清洁的方式

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-06
      • 2017-06-29
      • 1970-01-01
      • 2016-12-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多