[英]ObservableCollection.CollectionChanged not being called
好的,我进行了搜索,但我看到的任何内容都没有给我解决方案,所以我开始了。 我有一个public ObservableCollection<Photo> Items { get; set;}
public ObservableCollection<Photo> Items { get; set;}
不会更新 MonoDroid 中的 UI Monotouch 上的所有内容都可以正常工作。
进一步审查后:删除和添加都不会刷新集合。 我必须旋转设备才能看到变化。 (仅限安卓)
我的观点是这样的:
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
并且它没有被调用。 在这一刻我出主意了。
这有点猜测,可能是ContentView
的 Android 实现中存在错误。 您正在尝试将ObsevableCollection
绑定到声明为IEnumerable
的绑定。 这应该无关紧要,但可能存在一个错误,即 android 实现检测IEnumerable
但不测试其底层类型以查看它是否也是INotifyCollectionChanged
。 尝试将其从 IEnumerable 更改为具体类型,看看是否有效。 正如我所说,这是基于我在 WPF 世界中发现的类似问题的猜测。
我找到了答案,我创建了一个自定义渲染器并使用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
事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.