问题描述
如何在 Xamarin表单的Editor
中设置占位符文本和占位符颜色.
它没有默认的功能或属性如何自定义它?
It has no default Functionality or Properties how to customize it?
参考文档: Xamarin表单编辑器
推荐答案
为此您将需要一个自定义渲染器(这里是Android Custom Renderer),您将需要另一个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编辑器表单中设置占位符和PlaceHolder颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!