首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Xamarin窗体-获取AbsoluteLayout大小

Xamarin窗体-获取AbsoluteLayout大小
EN

Stack Overflow用户
提问于 2016-10-10 01:08:05
回答 1查看 827关注 0票数 1

我找不到获得WidthHeight of a Xamarin.Forms.View的方法。

我有一个目标:

代码语言:javascript
复制
public class SquareLayout : View
{
    public static readonly BindableProperty ScalingBaseProperty =
        BindableProperty.Create(nameof(ScalingBase), typeof(ScalingBaseType), typeof(SquareLayout), ScalingBaseType.Average,
            propertyChanged: OnScalingBasePropertyChanged);

    public ScalingBaseType ScalingBase
    {
        get { return (ScalingBaseType)GetValue(ScalingBaseProperty); }
        set { SetValue(ScalingBaseProperty, value); }
    }

    public enum ScalingBaseType
    {
        Average,
        Height,
        Width
    }

    public static void OnScalingBasePropertyChanged(BindableObject bindable, object oldValue, object newValue)
    {
        SquareLayout sq = ((SquareLayout)bindable);

        Debug.WriteLine("|Width = {0}|Height = {1}|", sq.Bounds.Width, sq.Bounds.Height);

        switch (sq.ScalingBase)
        {
            case ScalingBaseType.Average:
                double size = (sq.Bounds.Width + sq.Bounds.Height) / 2;
                sq.WidthRequest = size;
                sq.HeightRequest = size;
                break;
            case ScalingBaseType.Height:
                sq.WidthRequest = sq.Bounds.Height;
                break;
            case ScalingBaseType.Width:
                sq.HeightRequest = sq.Bounds.Width;
                break;
        }

        Debug.WriteLine("|Width = {0}|Height = {1}|", sq.Bounds.Width, sq.Bounds.Height);
    }
}

基本上,您可以在这个SquareLayout的某个位置声明,然后,根据一个Enum,将布局调整为Square

那么,我就有了这个XAML部件

代码语言:javascript
复制
<ContentPage.Content>
  <AbsoluteLayout BackgroundColor="White">
  ...
    <AbsoluteLayout x:Name="ToolsLayout" BackgroundColor="Red"
                    AbsoluteLayout.LayoutBounds="0.5, 0.05, 0.9, 0.075"
                    AbsoluteLayout.LayoutFlags="All">
      <control:SquareLayout BackgroundColor="White" ScalingBase="Height"
                            AbsoluteLayout.LayoutBounds="0, 0.5, 0.1, 1"
                            AbsoluteLayout.LayoutFlags="All">

      </control:SquareLayout>
      <control:SquareLayout BackgroundColor="White" ScalingBase="Height"
                            AbsoluteLayout.LayoutBounds="1, 0.5, 0.1, 1"
                            AbsoluteLayout.LayoutFlags="All">

      </control:SquareLayout>
    </AbsoluteLayout>
  ...
  </AbsoluteLayout>
</ContentPage.Content>

这会给我一个有两个SquareSquare。但什么都没有!

我试着显示WidthHeight,但什么也没有。我试过了Bounds,就像你在这个例子中看到的一样,但是再也没有了。

我得到的唯一值是-1

有人能帮我吗?提前感谢!

EN

回答 1

Stack Overflow用户

发布于 2017-08-24 05:09:27

Xamarin对于新手的文档非常少,花了一些时间来解决这个问题,基本上在设计期间宽度和高度属性的默认值为-1,这表示自动,因此在设计期间无法知道这些值是什么,因此您需要使用“events”来获得宽度和高度属性,在这种情况下,在运行时设置布局界限时,需要使用SizeChanged事件。

在xaml代码背后,使用此事件获取布局的宽度和高度,我使用了lambda函数,这可以通过多种方式实现--我觉得这个方法很简单。

代码语言:javascript
复制
yourlayout.SizeChanged+=(sender,e)=>
{
//Add your child objects here and set its values using yourlayout.Height etc
};

与在此函数中添加子函数不同,您可以使用它返回值并使用该值。

注意:我不确定这是否是MVVM的最佳实践。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39950086

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档