2016-09-19 60 views

回答

1

您將需要一個定製的渲染器(這裏是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; 
     } 
    } 
} 

而對於顏色,你可能需要像這樣(安卓) :

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

目前已經在Xamarin論壇這個線程: https://forums.xamarin.com/discussion/20616/placeholder-editor

,更多的是自定義渲染信息如下: https://developer.xamarin.com/guides/xamarin-forms/custom-renderer/

+0

對於本地IOS? –

1

設置佔位符字符串在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) 
    } 
}; 
+0

簡單和工作的答案! – ZeeProgrammer