Xamarin 表单中的占位符文本对齐方式 [英] Placeholder text alignment in Xamarin forms

查看:15
本文介绍了Xamarin 表单中的占位符文本对齐方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何在输入框中放置占位符文本的顶部.我有一个非常大的输入框,想将占位符文本放在顶部.

I don't know how to top the placeholder text in a entry box. I have a entry box that is very big and want to put the placeholder text in the top.

<Entry Placeholder="Enter notes about the item" 
       Keyboard="Text" VerticalOptions="Start" HeightRequest="80" />

推荐答案

您将需要为每个平台创建一个自定义渲染器来调整占位符,如下所示:

You will need to create a custom renderer for each platform to aling the placeholder something like this:

public class PlaceholderEditor : Editor
{
    public static readonly BindableProperty PlaceholderProperty =
        BindableProperty.Create<PlaceholderEditor, string>(view => view.Placeholder, String.Empty);

    public PlaceholderEditor()
    {
    }

    public string Placeholder
    {
        get
        {
            return (string)GetValue(PlaceholderProperty);
        }

        set
        {
            SetValue(PlaceholderProperty, value);
        }
    }
}

public class PlaceholderEditorRenderer : EditorRenderer
{
    public PlaceholderEditorRenderer()
    {
    }

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

        if (e.NewElement != null)
        {
            var element = e.NewElement as PlaceholderEditor;
            this.Control.Hint = element.Placeholder;
        }
    }

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

        if (e.PropertyName == PlaceholderEditor.PlaceholderProperty.PropertyName)
        {
            var element = this.Element as PlaceholderEditor;
            this.Control.Hint = element.Placeholder;
        }
    }
}

这篇关于Xamarin 表单中的占位符文本对齐方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆