简体   繁体   中英

Passing data from custom renderer to custom control

I have the following Xamarin.Forms control in my project:

public partial class AutoCompleteView : Xamarin.Forms.View
{
    public event EventHandler<TextChangedEventArgs> TextChanged;

    public static readonly BindableProperty TextProperty = BindableProperty.Create(
        propertyName: "Text",
        returnType: typeof(string),
        declaringType: typeof(string),
        defaultBindingMode: BindingMode.TwoWay);

    public static readonly BindableProperty SuggestionsProperty = BindableProperty.Create(
        propertyName: "Suggestions",
        returnType: typeof(IEnumerable<string>),
        declaringType: typeof(ObservableCollection<string>),
        defaultBindingMode: BindingMode.OneWay);

    public IEnumerable<string> Suggestions
    {
        get => (IEnumerable<string>)GetValue(SuggestionsProperty);
        set => SetValue(SuggestionsProperty, value);
    }

    public string Text
    {
        get => (string)GetValue(TextProperty);
        set => SetValue(TextProperty, value);
    }

    public AutoCompleteView () : base()
    {
        InitializeComponent ();
    }

    private void OnTextChanged(TextChangedEventArgs e)
    {
        TextChanged?.Invoke(this, e);
    }
}

And the following custom Android renderer:

 public class AutoCompleteViewRenderer : Xamarin.Forms.Platform.Android.ViewRenderer<AutoCompleteView, AutoCompleteTextView>
{
    public AutoCompleteViewRenderer(Context context) : base(context) { }

    protected override void OnElementChanged(ElementChangedEventArgs<AutoCompleteView> e)
    {
        base.OnElementChanged(e);

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

        var autoComplete = new AutoCompleteTextView(this.Context);
        SetNativeControl(autoComplete);
        this.Control.SetSingleLine();


        this.Control.SetTextColor(Android.Graphics.Color.Black);
    }

    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);

        if (this.Element == null || this.Control == null)
            return;
        var autoComplete = (AutoCompleteView)sender;
        this.Control.Text = autoComplete.Text;
        if (autoComplete.Suggestions != null)
        {
            var suggestions = autoComplete.Suggestions.ToArray();
            this.Control.Adapter = new ArrayAdapter<string>(Context, Resource.Layout.autoCompleteCell, suggestions);
        }

    }
}

The problem is that control does not receive text from renderer (ie neither Text setter nor binded model's setter invoked). How can I solve this problem?

Thank mjwills, I found a logic mistake here. this.Control.Text = autoComplete.Text should obviously be inverted (ie autoComplete.Text = this.Control.Text ), since this.Control is a native control, which should pass text changes to custom AutoCompleteView .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM