没有调用ObservableCollection.CollectionChanged

时间:2016-03-23 17:53:17

标签: c# .net data-binding xamarin.forms

好的,我有搜索,没有什么我看到给我一个解决方案所以我走了。 我有一个public ObservableCollection<Photo> Items { get; set;}没有更新MonoDroid中的用户界面Monotouch上的所有内容都可以正常工作。

进一步审核:删除并添加两者都不会刷新集合。我必须旋转设备才能看到更改。 (仅限Android)

我的观点是这样绑定的:

this.BindingContext = _viewModel;

        var activityIndicator = new BrandActivityIndicator{
            VerticalOptions = LayoutOptions.Start,
            HorizontalOptions = LayoutOptions.Center
        };

        activityIndicator.SetBinding<PhotosViewModel>(BrandActivityIndicator.IsVisibleProperty, vm => vm.IsBusy);
        activityIndicator.SetBinding<PhotosViewModel>(BrandActivityIndicator.IsRunningProperty, vm => vm.IsBusy);

        this.Padding = new Thickness(10, 5, 10, 0);
        this.HorizontalOptions = LayoutOptions.FillAndExpand;
        this.VerticalOptions = LayoutOptions.FillAndExpand;

        _gridView = new GridView
        {      
            RowSpacing = 15,
            ColumnSpacing = 15,        
            ItemWidth = 85,        
            ItemHeight = 85,       
            HorizontalOptions = LayoutOptions.FillAndExpand,       
            VerticalOptions = LayoutOptions.FillAndExpand,     
            ItemTemplate = new DataTemplate(typeof(ImagesViewCell))
        };     

        _gridView.SetBinding<PhotosViewModel> (GridView.ItemsSourceProperty, vm => vm.Items); 
        _gridView.SetBinding<PhotosViewModel> (GridView.IsVisibleProperty, vm => vm.NotBusy);        

        var layout = new StackLayout
            {
                VerticalOptions = LayoutOptions.FillAndExpand,
                Children = {
                    activityIndicator,
                    _gridView
                }
            };

        this.Content = layout;

GridView只是一个ContentView

public class GridView : ContentView
{
    public GridView()
    {
        SelectionEnabled = true;

    }

    public static readonly BindableProperty ItemsSourceProperty = 
        BindableProperty.Create<GridView,IEnumerable>(p => p.ItemsSource, null);
}

viewModel是标准的

public class PhotosViewModel : BaseViewModel, IBadge
    {
        private readonly TaskScheduler _scheduler = TaskScheduler.FromCurrentSynchronizationContext();

        public ObservableCollection<Photo> Items { get; set;}
        public PhotosViewModel()
        {
            Items = new ObservableCollection<Photo>();

        }
}

我尝试附加Items.CollectionChanged += notify并且没有被调用。在这一刻,我没有想法。

2 个答案:

答案 0 :(得分:1)

这有点猜测,有可能在ContentView的Android实现中存在错误。您正在尝试将ObsevableCollection绑定到声明为IEnumerable的绑定。这不重要,但可能存在一个错误,即android实现检测到IEnumerable但没有测试其基础类型以查看它是否也是INotifyCollectionChanged。尝试将其从IEnumerable更改为具体类型,看看是否有效。 正如我所说,这是基于我在WPF世界中发现的类似问题的猜测。

答案 1 :(得分:0)

我找到了答案,我创建了一个自定义渲染器并使用InotifyCollectionChanged绑定了该属性。

if (newElement != null)
            {
                newElement.PropertyChanging += ElementPropertyChanging;
                newElement.PropertyChanged += ElementPropertyChanged;
                if (newElement.ItemsSource is INotifyCollectionChanged)
                    (newElement.ItemsSource as INotifyCollectionChanged).CollectionChanged += DataCollectionChanged;
            }


protected virtual void ElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "ItemsSource")
            {
                if (Element.ItemsSource is INotifyCollectionChanged)
                    (Element.ItemsSource as INotifyCollectionChanged).CollectionChanged += DataCollectionChanged;

                context.RunOnUiThread (() => UpdateFromCollectionChange ());
            }
        }

无论出于何种原因,ObservableCollection都没有使用Xamarin.forms在Android上触发collectionChanged事件而没有渲染器。