2016-06-13 60 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

相关问题