Xamarin.ios Xamarin表单向iOS编辑器添加占位符
如何在iOS的Xamarin表单中向编辑器添加占位符 我尝试通过自定义渲染器添加控件.Layer,但找不到与其相关的属性 请提供帮助。请尝试以下代码: PCL:Xamarin.ios Xamarin表单向iOS编辑器添加占位符,xamarin.ios,xamarin.forms,Xamarin.ios,Xamarin.forms,如何在iOS的Xamarin表单中向编辑器添加占位符 我尝试通过自定义渲染器添加控件.Layer,但找不到与其相关的属性 请提供帮助。请尝试以下代码: PCL: using System; using Xamarin.Forms; namespace ABC.CustomViews { public class PlaceholderEditor : Editor { public static readonly BindableProperty Placeho
using System;
using Xamarin.Forms;
namespace ABC.CustomViews
{
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);
}
}
}
}
using UIKit;
using ABC.CustomViews;
using ABC.iOS.CustomRenderer;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(PlaceholderEditor), typeof(PlaceholderEditorRenderer))]
namespace ABC.iOS.CustomRenderer
{
public class PlaceholderEditorRenderer : EditorRenderer
{
private string Placeholder { get; set; }
protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
{
base.OnElementChanged(e);
var element = this.Element as PlaceholderEditor;
if (Control != null && element != null)
{
Placeholder = element.Placeholder;
Control.TextColor = UIColor.LightGray;
Control.Text = Placeholder;
Control.ShouldBeginEditing += (UITextView textView) =>
{
if (textView.Text == Placeholder)
{
textView.Text = "";
textView.TextColor = UIColor.Black; // Text Color
}
return true;
};
Control.ShouldEndEditing += (UITextView textView) =>
{
if (textView.Text == "")
{
textView.Text = Placeholder;
textView.TextColor = UIColor.LightGray; // Placeholder Color
}
return true;
};
}
}
}
}
下面是android的代码
using System;
using MyApp;
using MyApp.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(CustomEditor), typeof(CustomEditorRenderer))]
namespace MyApp.Droid
{
public class CustomEditorRenderer : EditorRenderer
{
public CustomEditorRenderer()
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
var element = e.NewElement as CustomEditor;
this.Control.Hint = element.Placeholder;
Control.Gravity = Android.Views.GravityFlags.Start;
Control.SetBackgroundColor(global::Android.Graphics.Color.White);
Control.SetPadding(15,15,15,15);
}
}
}
使用系统;
使用MyApp;
使用MyApp.Droid;
使用Xamarin.Forms;
使用Xamarin.Forms.Platform.Android;
[assembly:ExportRenderer(typeof(CustomEditor)、typeof(CustomEditorRenderer))]
名称空间MyApp.Droid
{
公共类CustomEditorRenderer:EditorRenderer
{
公共CustomEditorRenderer()
{
}
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(例如NewElement!=null)
{
var element=e.NewElement作为CustomEditor;
this.Control.Hint=element.Placeholder;
Control.Gravity=Android.Views.GravityFlags.Start;
Control.SetBackgroundColor(全局::Android.Graphics.Color.White);
对照组:设置填充(15,15,15,15);
}
}
}
这里有一个Xamarin forms版本,它允许在编辑器的初始值设定项中包含placehodler,并且如果在代码中设置了Text属性(即如果Editor.Text=”“,则它将以浅灰色显示占位符),它还将处理一致的行为
using System;
using Xamarin.Forms;
namespace CrowdWisdom.Controls
{
public class EditorPlaceHolder : Editor
{
String placeHolderText = "";
public EditorPlaceHolder(String placeholder)
{
Text = placeholder;
TextColor = Color.LightGray;
this.Focused += EditorPlaceHolder_Focused;
this.Unfocused += EditorPlaceHolder_Unfocused;
this.placeHolderText = placeholder;
}
private void EditorPlaceHolder_Focused(object sender, FocusEventArgs e) //triggered when the user taps on the Editor to interact with it
{
if (Empty())
{
base.Text = "";
this.TextColor = Color.Black;
}
}
private void EditorPlaceHolder_Unfocused(object sender, FocusEventArgs e) //triggered when the user taps "Done" or outside of the Editor to finish the editing
{
if (Empty()) //if there is text there, leave it, if the user erased everything, put the placeholder Text back and set the TextColor to gray
{
this.Text = placeHolderText;
this.TextColor = Color.LightGray;
}
}
public String PlaceHolderText
{
get
{
return this.placeHolderText;
}
set
{
if (this.Empty())
{
this.Text = value;
this.TextColor = Color.LightGray;
}
this.placeHolderText = value;
}
}
public bool Empty()
{
return (this.Text.Equals("") || this.Text.Equals(this.placeHolderText));
}
public virtual new string Text
{
get
{
return base.Text;
}
set
{
base.Text = value;
if (Empty())
{
this.TextColor = Color.LightGray;
base.Text = this.placeHolderText;
}
else
{
this.TextColor = Color.Black;
}
}
}
}
}
Xamarin表单为Android编辑器添加占位符
using System;
using System.Collections.Generic;
using System.Text;
using Xamarin.Forms;
namespace MyCare.Renderers
{
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);
}
}
}
}
//Renderer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Xamarin.Forms;
using MyCare.Renderers;
using MyCare.Droid.Renderers;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(PlaceholderEditor), typeof(PlaceholderEditorRenderer))]
namespace MyCare.Droid.Renderers
{
class PlaceholderEditorRenderer : EditorRenderer
{
private string Placeholder { get; set; }
protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
{
base.OnElementChanged(e);
var element = this.Element as PlaceholderEditor;
if (Control != null && element != null)
{
Placeholder = element.Placeholder;
Control.SetTextColor(Android.Graphics.Color.Black);
Control.SetHintTextColor(Android.Graphics.Color.LightGray);
Control.Hint = element.Placeholder;
Control.SetBackgroundColor(Android.Graphics.Color.Transparent);
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统文本;
使用Xamarin.Forms;
命名空间MyCare.Renderers
{
类占位符编辑器:编辑器
{
public static readonly BindableProperty PlaceholderProperty=BindableProperty.Create(view=>view.Placeholder,String.Empty);
公共占位符编辑器()
{
}
公共字符串占位符
{
得到
{
返回(字符串)GetValue(占位符属性);
}
设置
{
SetValue(占位符属性、值);
}
}
}
}
//渲染器
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用Android.App;
使用Android.Content;
使用Android.OS;
使用Android.Runtime;
使用Android.Views;
使用Android.Widget;
使用Xamarin.Forms;
使用MyCare.renders;
使用MyCare.Droid.renders;
使用Xamarin.Forms.Platform.Android;
[程序集:导出渲染器(类型化(占位符编辑器)、类型化(占位符编辑器或渲染器))]
命名空间MyCare.Droid.Renderers
{
类占位符EditorRenderer:EditorRenderer
{
私有字符串占位符{get;set;}
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
var element=此.element作为占位符编辑器;
if(Control!=null&&element!=null)
{
占位符=元素。占位符;
Control.SetTextColor(Android.Graphics.Color.Black);
Control.SetHintTextColor(Android.Graphics.Color.LightGray);
Control.Hint=element.Placeholder;
SetBackgroundColor(Android.Graphics.Color.Transparent);
}
}
}
}
继承Jay的解决方案。下面是XAML的用法
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:cstCtrl="clr-namespace:TeamCollaXform.Views"
x:Class="TeamCollaXform.Views.MailComposePage">
请阅读如何制定一个问题,以增加您获得答案的机会。目前,由于问题太广泛,有太多不同的好答案。您可以通过包括您自己尝试过的内容来缩小范围。您是否有Android和UWP的等价物?@PrakashChennupat我已经添加了android的代码。请check@AbdulBasit非常感谢。
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:cstCtrl="clr-namespace:TeamCollaXform.Views"
x:Class="TeamCollaXform.Views.MailComposePage">
<cstCtrl:PlaceholderEditor Grid.Row="2" x:Name="txtContent" Text="" HeightRequest="750" Placeholder="Compose..."/>