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

标签 xamarin xamarin.forms

我不知道如何在输入框中置顶占位符文本。
我有一个很大的输入框,想把占位符文本放在顶部。

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

最佳答案

您需要为每个平台创建一个自定义渲染器以对齐占位符,如下所示:

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 - Xamarin 表单中的占位符文本对齐方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32778337/

相关文章:

java - 将多个 ViewHolder 与 RecyclerView 一起使用

xamarin.forms - 如何在需要时隐藏编辑器上的键盘

c# - 对所有项目同时执行 foreach block ?

iOS 应用程序启动时间测量

xamarin - 无法添加 Xamarin Google Play 服务

c# - 在 Xamarin Forms 中将字符串本地保存在设备上

ios - 无法创建Xamarin Forms Picker自定义渲染器

c# - ListView 的单元格重用问题和自定义渲染器的 View 单元格

c# - 如何测试包含 Application.Current.Properties.ContainsKey ("token"的方法

c# - Xamarin Entry 底线颜色不变