Xamarin表单中的占位符文本对齐方式

时间:2015-09-25 08:51:24

标签: xamarin xamarin.forms

我不知道如何在输入框中填充占位符文字。 我有一个非常大的输入框,并希望将占位符文本放在顶部。

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

2 个答案:

答案 0 :(得分:3)

您需要为每个平台创建一个自定义渲染器,以便对占位符进行如下操作:

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;
        }
    }
}

答案 1 :(得分:1)

无需更多自定义,只需在XAML中使用:HorizontalTextAlignment="Center"