[英]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.