Android.Widget.AutoCompleteTextView
is a View
来自安卓。
PCL的解决方案:
您不能使用特定于平台的View's
在 Xamarin 表单 (PCL) 上ContentPage
.
使用特定于平台的View
你应该使用自定义渲染 https://developer.xamarin.com/guides/xamarin-forms/custom-renderer/。
有一个博客文章 https://blog.xamarin.com/using-custom-controls-in-xamarin-forms-on-android/来自@JamesMontemagno,它展示了如何做你需要的事情。
这段代码是草稿范例请按原样使用它。
1 - 创建您自己的自定义 Xamarin.Forms 控件,该控件将在 Android 中呈现为AutoCompleteTextView
:
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.
}
}
}
共享项目的解决方案:
使用共享项目时可以使用原生嵌入 https://developer.xamarin.com/guides/xamarin-forms/user-interface/layouts/add-platform-controls/, like:
...
var textView = new TextView (Forms.Context) { Text = originalText };
stackLayout.Children.Add (textView);
contentView.Content = textView.ToView();
...