Xamarin.Forms:RelativeLayout中约束的单位

时间:2015-06-15 17:48:31

标签: xamarin relativelayout xamarin.forms

有没有人知道Constraints.Constant(double value)的“值”应该是什么类型的单位?

我正在尝试使用RelativeLayouts创建我的Xamarin.Forms页面,但是当我尝试使用像素作为值时,元素大于应有的值。

此外,我使用Contraints.RelativeToParent(),但是某些元素(特别是图像)看起来是倾斜的,好像“父”不占用屏幕的完整大小。 RelativeLayout有预定的界限吗?

(我正在使用返回parent.Width * 0.3906这样的计算,它应该返回293,而是返回124.992)这是基于iPhone 6模拟器。

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

没有平台无关的方法来进行您想要实现的计算。您需要构建一个本机实现来调用特定平台以获得可以使用的大小。

您需要遵循DependencyService模式:

public interface IDeviceSize
{
    double DeviceHeight { get; }

    double DeviceWidth { get; }
}


public class DeviceSize_Android : IDeviceSize
{
    Context _context;

    public DeviceSize()
    {
        _context = Xamarin.Forms.Forms.Context;
    }

    #region IDeviceSize implementation

    public double DeviceHeight
    {
        get
        {
            return Convert.ToDouble(_context.Resources.DisplayMetrics.HeightPixels / _context.Resources.DisplayMetrics.ScaledDensity);
        }
    }

    public double DeviceWidth
    {
        get
        {

            return Convert.ToDouble(_context.Resources.DisplayMetrics.WidthPixels / _context.Resources.DisplayMetrics.ScaledDensity);
        }
    }

    #endregion
}

public class DeviceSize_iOS : IDeviceSize
{
    public DeviceSize()
    {
    }

    #region IDeviceSize implementation

    public double DeviceHeight
    {
        get
        {
            CGRect screenRect = UIScreen.MainScreen.Bounds;
            return screenRect.Height;
        }
    }

    public double DeviceWidth
    {
        get
        {
            CGRect screenRect = UIScreen.MainScreen.Bounds;
            return screenRect.Width;
        }
    }
    #endregion
}

您需要发布一些代码,以便进一步评估您的要求,因为可能不需要像您那样包含特定尺寸。