如何在Xamarin.Forms上使用Android AutoCompleteTextView [英] How to use Android AutoCompleteTextView on Xamarin.Forms

查看:244
本文介绍了如何在Xamarin.Forms上使用Android AutoCompleteTextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理Xamarin.forms项目,但是我需要使用Android.Widget.AutoCompleteTextView如何应用它? 当我尝试将AutoCompleteTextView UserNameAutoComplete;添加到ContentPage时,出现以下错误:

I'm working on a Xamarin.forms project but i need to use Android.Widget.AutoCompleteTextView how can i apply that? When i am trying to add AutoCompleteTextView UserNameAutoComplete; to ContentPage i get the following error:

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

无法从"Android.Widget.AutoCompleteTextView"转换为 'Xamarin.Forms.View'

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

推荐答案

Android.Widget.AutoCompleteTextView是Android的View.

Android.Widget.AutoCompleteTextView is a View from Android.

PCL解决方案:

您不能在Xamarin Forms(PCL)ContentPage上使用特定于平台的View's.

You can't use platform specific View's on Xamarin Forms (PCL) ContentPage.

要使用特定于平台的View,您应该使用自定义渲染. 来自@JamesMontemagno的博客帖子展示了如何做所需的事情.

To use platform specific View you should use a custom render. There is a blog post from @JamesMontemagno that shows how to do what you need.

此代码为草稿示例,请按原样使用它.

This code is draft exemple please use it as such.

1-创建您自己的自定义Xamarin.Forms控件,该控件将在Android中呈现为AutoCompleteTextView:

1 - Create your own custom Xamarin.Forms control that will be renderered in Android as an AutoCompleteTextView:

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

2-在Android项目中为AutoCompleteView添加渲染器:

2 - In Android project add a renderer for 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.
        }
    }
}


共享项目的解决方案:

使用共享项目时,可以使用

When using Shared Project there is a possibility to use Native Embedding, like:

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

这篇关于如何在Xamarin.Forms上使用Android AutoCompleteTextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆