【问题标题】:Entry not vertically centered in Xamarin Forms MacOSXamarin Forms MacOS 中的条目未垂直居中
【发布时间】:2021-01-13 06:57:47
【问题描述】:

在尝试使用居中条目时偶然发现了这个错误 https://github.com/xamarin/Xamarin.Forms/issues/10789

尝试通过自定义渲染器在本地解决它,但在此过程中遇到了一些麻烦。这是我目前拥有的:

[assembly: Xamarin.Forms.ExportRenderer(typeof(Entry), typeof(CustomEntryRenderer))]
namespace Project.MacOS.Renderers
{
public class CustomEntryRenderer : EntryRenderer
{

    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);
        var element = e.NewElement as Entry;

        if (Control != null && element != null)
        {
            Control.BackgroundColor = NSColor.Clear;
            Control.Bordered = false;

            var stringHeight = Control.AttributedStringValue.Size.Height;


            var titleRect = Bounds;

            var oldOriginY = this.Bounds.Y;
            var res = titleRect.Y + (Bounds.Size.Height - stringHeight) / 2.0;
            titleRect.Y = (System.nfloat)res;


            var res2 = titleRect.Size.Height - (titleRect.Y - oldOriginY);

            titleRect.Size.Height = res2;
        }
    }

    protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);
    }
}

}

所以我跟着这个问题: Set text vertical center in NSTextField

但是有一些问题。第一个问题是我无法设置titleRect.Size.Height = res2;,因为它抱怨“无法修改‘CGRECT.Size’的返回值,因为它不是变量。第二个问题是我如何将这些新添加的值分配给控件自己,这样我就可以测试它,看看它是否有效。

【问题讨论】:

  • 如果没有记错的话,你不能只设置高度,你必须设置整个尺寸

标签: c# xamarin.forms xamarin.mac


【解决方案1】:

第一个问题是我无法设置 titleRect.Size.Height = res2;因为它抱怨“无法修改‘CGRECT.Size’的返回值,因为它不是变量。

关于这个,这意味着你不能直接分配Height

我们需要设置Size的总参数如下:

titleRect = new CoreGraphics.CGRect(titleRect.X, titleRect.Y, titleRect.Width, res2 );

第二个问题是我如何将这些新添加的值分配给控件本身,以便我可以对其进行测试并查看它是否有效。

通过共享讨论,我们还可以自定义一个NSTextFieldCell,然后设置为NSTextField

public class CustomEntryRenderer :EntryRenderer
{
    public CustomEntryRenderer()
    {
    }

    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);

        var element = e.NewElement as Entry;
        if (Control != null & element != null)
        {
            Control.BackgroundColor = NSColor.Clear;

            VerticallyCenteredTextfieldCell verticallyCenteredTextfieldCell = new VerticallyCenteredTextfieldCell(Control);
            Control.Cell = verticallyCenteredTextfieldCell;
            Control.StringValue = element.Text;
            Control.Bordered = true;
        }            
    }

}

public class VerticallyCenteredTextfieldCell : NSTextFieldCell
{
    private NSTextField control;

    public VerticallyCenteredTextfieldCell(NSTextField control)
    {
        this.control = control;
    }

    public override CoreGraphics.CGRect TitleRectForBounds(CoreGraphics.CGRect theRect)
    {
        var stringheight = control.AttributedStringValue.Size.Height;
        CoreGraphics.CGRect titleRect = base.TitleRectForBounds(theRect);
        var oldOriginY = control.Frame.Y;
        titleRect.Y = control.Frame.Y + (control.Frame.Size.Height - stringheight) / 2;
        var height = titleRect.Size.Height - (titleRect.Y - oldOriginY);
        titleRect = new CoreGraphics.CGRect(titleRect.X, titleRect.Y, titleRect.Width, height);

        return titleRect;
    }

    public override void DrawInteriorWithFrame(CoreGraphics.CGRect cellFrame, NSView inView)
    {
        base.DrawInteriorWithFrame(TitleRectForBounds(cellFrame), inView);
    }

}

效果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-01
    • 2013-12-15
    • 2020-08-11
    • 1970-01-01
    • 2014-03-02
    • 2015-10-29
    • 1970-01-01
    相关资源
    最近更新 更多