不调用MvxExpandableListAdapter SetItemsSource

时间:2016-09-21 04:28:47

标签: xamarin.android mvvmcross

我有使用MvxExpandableListView

的代码段
public class ExpandView : MvxActivity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.ExpandView);
        MvxExpandableListView list = FindViewById<MvxExpandableListView>(Resource.Id.TheExpandView);
        list.SetAdapter(new MyCustomAdaptor(this, (IMvxAndroidBindingContext)BindingContext));
    }

    private class MyCustomAdaptor : MvxExpandableListAdapter
    {
        private IList _itemsSource;
        public MyCustomAdaptor(Context context, IMvxAndroidBindingContext bindingContext)
            : base(context, bindingContext)
        {
        }

        protected override void SetItemsSource(IEnumerable value)
        {
            Mvx.Trace("Setting itemssource");
            if (_itemsSource == value)
                return;
            var existingObservable = _itemsSource as INotifyCollectionChanged;
            if (existingObservable != null)
                existingObservable.CollectionChanged -= OnItemsSourceCollectionChanged;

            _itemsSource = value as IList;

            var newObservable = _itemsSource as INotifyCollectionChanged;
            if (newObservable != null)
                newObservable.CollectionChanged += OnItemsSourceCollectionChanged;

            if (value != null)
            {

            }
            else
                base.SetItemsSource(null);
        }

    }
}

加载视图时,不会调用覆盖方法 SetItemsSource ,但是当我尝试离开页面 SetItemsSource 时,会调用null值。

1 个答案:

答案 0 :(得分:0)

您需要设置属性ItemsSource。查看MvxAdapter的来源:

[MvxSetToNullAfterBinding]
public virtual IEnumerable ItemsSource
{
    get { return _itemsSource; }
    set { SetItemsSource(value); }
}

ItemsSource的设置者调用方法SetItemsSource

MvxExpandableListAdapter继承MvxAdapter

<强>来源: http://repo.typesafe.com/typesafe/releases/ https://github.com/MvvmCross/MvvmCross/blob/4.0/MvvmCross/Binding/Droid/Views/MvxExpandableListAdapter.cs