简体   繁体   English

获取Xamarin.Forms中Entry的宽度

[英]Get width of Entry in Xamarin.Forms

How do I get the width of an Entry view in Xamarin.Forms? 如何在Xamarin.Forms中获得Entry视图的宽度? I'm trying to make an entry in iOS look like the default entry in Android (the one with the line underneath). 我正在尝试使iOS中的条目看起来像Android中的默认条目(下面有一行)。

Custom Renderer: 自定义渲染器:

void SetBorderWidth(Controls.Entry control)
{
    Control.BorderStyle = UITextBorderStyle.None;
    var myBox = new UIView(new CGRect(0, 40, <INSERT WIDTH HERE>, 1));
    myBox.BackgroundColor = control.BorderColor.ToUIColor();
    Control.AddSubview(myBox);
}

Whenever I try to insert either control.Width, control.WidthRequest, control.MinimumWidthRequest nothing happens. 每当我尝试插入control.Width,control.WidthRequest,control.MinimumWidthRequest时,都不会发生任何事情。 But if I put a number the line underneath suddenly shows. 但是,如果我输入数字,下面的线会突然显示。

Additionally when I print out the width and widthrequest they have a value of -1. 另外,当我打印出width和widthrequest时,它们的值为-1。

In your iOS Entry renderer, override the Draw method and use the CGRect provided as the current Frame size of the UITextField , since this method can be called multiple times (screen rotations, resizes, etc... Save your UIView after its initial creation and just update its Frame . 在您的iOS项渲染器,重载Draw方法和使用CGRect作为当前提供的Frame的大小UITextField ,因为这种方法可以多次调用(屏幕旋转,调整大小,等...保存您UIView初始创建后只需更新其Frame

Something like (I do not have my personal code with me, but this should be "close"): 类似的东西(我没有我的个人密码,但这应该是“关闭”):

UIView myBox;
public override void Draw(CGRect rect)
{
    base.Draw(rect);
    switch (myBox)
    {
        case null:
            myBox = new UIView(new CGRect(0, 40, rect.Width, 1));
            Control.AddSubview(myBox);
            break;
        default:
            myBox.Frame = new CGRect(0, 40, rect.Width, 1);
            break;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM