如何在编辑器Xamarin表单中设置占位符和占位符颜色
如何在Xamarin表单的如何在编辑器Xamarin表单中设置占位符和占位符颜色,xamarin,xamarin.forms,Xamarin,Xamarin.forms,如何在Xamarin表单的编辑器中设置占位符文本和占位符颜色 它没有默认功能或属性如何自定义它 参考文档:您需要一个自定义渲染器(这里是Android自定义渲染器)您需要另一个iOS渲染器: public class PlaceholderEditor : Editor { public static readonly BindableProperty PlaceholderProperty = BindableProperty.Create<Placeholder
编辑器中设置占位符文本和占位符颜色
它没有默认功能或属性如何自定义它
参考文档:您需要一个自定义渲染器(这里是Android自定义渲染器)您需要另一个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;
}
}
}
在Xamarin论坛上已经有了一个主题:
有关自定义渲染器的详细信息,请参见以下内容:
在Xaml中将占位符字符串设置为编辑器的文本
然后在代码隐藏文件中:
InitializeComponent();
var placeholder = myEditor.Text;
myEditor.Focused += (sender, e) =>
{
// Set the editor's text empty on focus, only if the place
// holder is present
if (myEditor.Text.Equals(placeholder))
{
myEditor.Text = string.Empty;
// Here You can change the text color of editor as well
// to active text color
}
};
myEditor.Unfocused += (sender, e) =>
{
// Set the editor's text to place holder on unfocus, only if
// there is no data entered in editor
if (string.IsNullOrEmpty(myEditor.Text.Trim()))
{
myEditor.Text = placeholder;
// Here You can change the text color of editor as well
// to dim text color (Text Hint Color)
}
};
对于本机Ios?
InitializeComponent();
var placeholder = myEditor.Text;
myEditor.Focused += (sender, e) =>
{
// Set the editor's text empty on focus, only if the place
// holder is present
if (myEditor.Text.Equals(placeholder))
{
myEditor.Text = string.Empty;
// Here You can change the text color of editor as well
// to active text color
}
};
myEditor.Unfocused += (sender, e) =>
{
// Set the editor's text to place holder on unfocus, only if
// there is no data entered in editor
if (string.IsNullOrEmpty(myEditor.Text.Trim()))
{
myEditor.Text = placeholder;
// Here You can change the text color of editor as well
// to dim text color (Text Hint Color)
}
};