本文介绍了如何在编辑器 Xamarin Forms 中设置占位符和占位符颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Xamarin FormsEditor中设置Placeholder TextPlaceholder Color.

它没有默认的功能或属性,如何自定义?

It has no default Functionality or Properties how to customize it?

参考文档: Xamarin 表单编辑器

推荐答案

为此你需要一个自定义渲染器(这里是 Android 自定义渲染器)你需要另一个适用于 iOS 的渲染器:

You will need a custom renderer for that (Here is the Android Custom Renderer) you will need another renderer for iOS:

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

对于颜色,您可能需要这样的东西(Android):

And for the color you may need something like this (Android):

Control.SetHintTextColor(Android.Graphics.Color.White);

在 Xamarin 论坛中已经有一个线程:https://forums.xamarin.com/discussion/20616/placeholder-editor

There already a thread for this in the Xamarin Forums:https://forums.xamarin.com/discussion/20616/placeholder-editor

有关自定义渲染器的更多信息如下:https://developer.xamarin.com/guides/xamarin-forms/custom-渲染器/

And more about Custom Renderers information below:https://developer.xamarin.com/guides/xamarin-forms/custom-renderer/

这篇关于如何在编辑器 Xamarin Forms 中设置占位符和占位符颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 09:09