2016-06-13 64 views
5

我正在研究​​項目,但我需要使用Android.Widget.AutoCompleteTextView我該如何應用該項目? 當我試圖添加AutoCompleteTextView UserNameAutoComplete;ContentPage我得到以下錯誤:如何在Xamarin.Forms上使用Android AutoCompleteTextView

Content = new StackLayout 
{     
    VerticalOptions = LayoutOptions.Center, 
    Padding = new Thickness(25), 
    Children = 
    { 
     UserNameAutoComplete, 
     passwordEditor, 
    } 
}; 

cannot convert from 'Android.Widget.AutoCompleteTextView' to 'Xamarin.Forms.View'

回答

4

Android.Widget.AutoCompleteTextView是Android的一個View


解決方案PCL:

不能使用在Xamarin形式(PCL)ContentPage具體View's平臺。

要使用特定平臺View,您應該使用custom render。 @JamesMontemagno有blog post顯示如何做你所需要的。

此代碼是草稿例請按此使用。

1 - 創建將在Android中被呈示爲AutoCompleteTextView自己的定製Xamarin.Forms控制:

public class AutoCompleteView : View 
{ 
    // Place need properties here.  
} 

2 - 在Android項目添加渲染AutoCompleteView

[assembly: ExportRenderer(typeof(AutoCompleteView), typeof(AutoCompleteViewRenderer))] 
namespace App.Droid 
{ 
    public class AutoCompleteViewRenderer : ViewRenderer<AutoCompleteView, AutoCompleteTextView> 
    {  
     // Initialize the AutoCompleteTextView 
     protected override void OnElementChanged (ElementChangedEventArgs<AutoComplete> e) 
     { 
      base.OnElementChanged (e); 

      if (e.OldElement != null || this.Element == null) 
       return; 

      var autoComplete = new AutoCompleteTextView(Forms.Context); 
      SetNativeControl (autoComplete); 
     } 

     // Use the control here. 
     protected override void OnElementPropertyChanged (object sender, PropertyChangedEventArgs e) { 
      base.OnElementPropertyChanged (sender, e); 

      if (this.Element == null || this.Control == null) 
       return; 

      // variable this.Control is the AutoCompleteTextView, so you an manipulate it. 
     } 
    } 
} 

共享項目解決方案:

當使用共享項目存在使用Native Embedding,像一個可能性:

... 
    var textView = new TextView (Forms.Context) { Text = originalText }; 
    stackLayout.Children.Add (textView); 
    contentView.Content = textView.ToView(); 
    ... 
+0

能否請您給了一步做或添加一些代碼..thanks –

+0

感謝jzeferion ..如果你自定義的familare渲染,你可以請爲你的答案添加代碼包含步驟.. –

+0

我已經添加了一個簡單的例子來解釋如何渲染工作。欲瞭解更多詳情,請閱讀完整的博客文章。 – jzeferino

相關問題