Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将数据从自定义渲染器传递到自定义控件_C#_.net_Xamarin.forms_Xamarin.android - Fatal编程技术网

C# 将数据从自定义渲染器传递到自定义控件

C# 将数据从自定义渲染器传递到自定义控件,c#,.net,xamarin.forms,xamarin.android,C#,.net,Xamarin.forms,Xamarin.android,我的项目中有以下Xamarin.Forms控件: public partial class AutoCompleteView : Xamarin.Forms.View { public event EventHandler<TextChangedEventArgs> TextChanged; public static readonly BindableProperty TextProperty = BindableProperty.Create(

我的项目中有以下
Xamarin.Forms
控件:

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);
    }
}
公共部分类自动完成视图:Xamarin.Forms.View
{
公共事件事件处理程序文本已更改;
公共静态只读BindableProperty TextProperty=BindableProperty.Create(
propertyName:“文本”,
returnType:typeof(字符串),
declaringType:typeof(字符串),
defaultBindingMode:BindingMode.TwoWay);
公共静态只读BindableProperty SuggestionProperty=BindableProperty.Create(
propertyName:“建议”,
returnType:typeof(IEnumerable),
declaringType:typeof(ObservableCollection),
defaultBindingMode:BindingMode.OneWay);
公众的无数建议
{
get=>(IEnumerable)GetValue(SuggestionProperty);
set=>SetValue(SuggestionProperty,value);
}
公共字符串文本
{
get=>(字符串)GetValue(TextProperty);
set=>SetValue(TextProperty,value);
}
公共自动完成视图():base()
{
初始化组件();
}
私有void OnTextChanged(textchangedventargs e)
{
TextChanged?调用(this,e);
}
}
以及以下自定义Android渲染器:

 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);
        }

    }
}
公共类自动完成ViewRenderer:Xamarin.Forms.Platform.Android.ViewRenderer
{
公共自动完成视图渲染器(上下文):基本(上下文){}
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(e.OldElement!=null | | this.Element==null)
返回;
var autoComplete=新的AutoCompleteTextView(this.Context);
SetNativeControl(自动完成);
this.Control.SetSingleLine();
this.Control.SetTextColor(Android.Graphics.Color.Black);
}
受保护的覆盖无效OnElementPropertyChanged(对象发送方,PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(发送方,e);
if(this.Element==null | | this.Control==null)
返回;
var autoComplete=(AutoCompleteView)发送方;
this.Control.Text=autoComplete.Text;
if(autoComplete.Suggestions!=null)
{
var-suggestions=autoComplete.suggestions.ToArray();
this.Control.Adapter=新的ArrayAdapter(上下文、Resource.Layout.autoCompleteCell、建议);
}
}
}

问题是控件不从呈现器接收文本(即既不调用
text
setter,也不调用绑定模型的setter)。如何解决这个问题?

谢谢mjwills,我在这里发现了一个逻辑错误
this.Control.Text=autoComplete.Text
显然应该反转(即
autoComplete.Text=this.Control.Text
),因为
this.Control
是本机控件,它应该将文本更改传递给自定义
AutoCompleteView

您是说
this.Control.Text=autoComplete.Text不执行?如果在该行上放置一个断点并进行调试,该断点会被命中吗?会的。它在创建元素和焦点丢失时触发。但毕竟,绑定属性(
Text
我的意思是)不会更新,而是保持为空。