diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue9833.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue9833.cs new file mode 100644 index 000000000..c0f693a55 --- /dev/null +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue9833.cs @@ -0,0 +1,103 @@ +using System.Collections.ObjectModel; +using Xamarin.Forms.CustomAttributes; +using Xamarin.Forms.Internals; + +#if UITEST +using NUnit.Framework; +using Xamarin.Forms.Core.UITests; +#endif + +namespace Xamarin.Forms.Controls.Issues +{ + [Preserve(AllMembers = true)] + [Issue(IssueTracker.Github, 9833, "[Bug] [UWP] Propagate CollectionView BindingContext to EmptyView", + PlatformAffected.UWP)] +#if UITEST + [Category(UITestCategories.CollectionView)] +#endif + public class Issue9833 : TestContentPage + { + readonly CollectionView _collectionView; + readonly Label _emptyLabel; + + public Issue9833() + { + Title = "Issue 9833"; + + BindingContext = new Issue9833ViewModel(); + + var layout = new StackLayout + { + Padding = 0 + }; + + var instructions = new Label + { + Text = "If the EmptyView BindingContext is not null, the test has passed.", + BackgroundColor = Color.Black, + TextColor = Color.White + }; + + _collectionView = new CollectionView(); + _collectionView.SetBinding(ItemsView.ItemsSourceProperty, "Items"); + + var emptyView = new StackLayout + { + BackgroundColor = Color.LightGray + }; + + _emptyLabel = new Label + { + Text = "This is the EmptyView. " + }; + + emptyView.Children.Add(_emptyLabel); + + _collectionView.EmptyView = emptyView; + + layout.Children.Add(instructions); + layout.Children.Add(_collectionView); + + Content = layout; + } + + protected override void Init() + { + + } + + protected override void OnAppearing() + { + base.OnAppearing(); + + var emptyView = (View)_collectionView.EmptyView; + _emptyLabel.Text += $"BindingContext = {emptyView.BindingContext}"; + } + } + + [Preserve(AllMembers = true)] + public class Issue9833ViewModel : BindableObject + { + ObservableCollection _items; + + public Issue9833ViewModel() + { + LoadData(); + } + + public ObservableCollection Items + { + get { return _items; } + set + { + _items = value; + OnPropertyChanged(); + } + } + + void LoadData() + { + Items = new ObservableCollection(); + } + } +} diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems index 120e7b2ee..2b57f3bfa 100644 --- a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems @@ -196,6 +196,7 @@ Code Issue9771.xaml + diff --git a/Xamarin.Forms.Platform.UAP/CollectionView/ItemsViewRenderer.cs b/Xamarin.Forms.Platform.UAP/CollectionView/ItemsViewRenderer.cs index 213f7eaac..e84371184 100644 --- a/Xamarin.Forms.Platform.UAP/CollectionView/ItemsViewRenderer.cs +++ b/Xamarin.Forms.Platform.UAP/CollectionView/ItemsViewRenderer.cs @@ -1,11 +1,10 @@ -using System.Collections.Specialized; +using System; +using System.Collections.Specialized; using System.ComponentModel; using System.Threading.Tasks; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Data; -using Windows.UI.Xaml.Controls.Primitives; -using Windows.Foundation; using Xamarin.Forms.Internals; using UwpScrollBarVisibility = Windows.UI.Xaml.Controls.ScrollBarVisibility; using UWPApp = Windows.UI.Xaml.Application; @@ -21,7 +20,8 @@ namespace Xamarin.Forms.Platform.UWP UwpScrollBarVisibility? _defaultHorizontalScrollVisibility; UwpScrollBarVisibility? _defaultVerticalScrollVisibility; FrameworkElement _emptyView; - View _formsEmptyView; + View _formsEmptyView; + bool _emptyViewDisplayed; ScrollViewer _scrollViewer; internal double _previousHorizontalOffset; internal double _previousVerticalOffset; @@ -388,7 +388,7 @@ namespace Xamarin.Forms.Platform.UWP } (ListViewBase as IEmptyView)?.SetEmptyView(_emptyView, _formsEmptyView); - + UpdateEmptyViewVisibility(); } @@ -413,25 +413,46 @@ namespace Xamarin.Forms.Platform.UWP FrameworkElement RealizeEmptyView(View view) { - _formsEmptyView = view; + _formsEmptyView = view ?? throw new ArgumentNullException(nameof(view)); return view.GetOrCreateRenderer().ContainerElement; } protected virtual void UpdateEmptyViewVisibility() { - if (_emptyView != null && ListViewBase is IEmptyView emptyView) - { - emptyView.EmptyViewVisibility = (CollectionViewSource?.View?.Count ?? 0) == 0 - ? Visibility.Visible - : Visibility.Collapsed; + bool isEmpty = (CollectionViewSource?.View?.Count ?? 0) == 0; - if (emptyView.EmptyViewVisibility == Visibility.Visible) + if (isEmpty) + { + if (_formsEmptyView != null) { - if (ActualWidth >= 0 && ActualHeight >= 0) - { - _formsEmptyView?.Layout(new Rectangle(0, 0, ActualWidth, ActualHeight)); - } + if (_emptyViewDisplayed) + ItemsView.RemoveLogicalChild(_formsEmptyView); + + if (ItemsView.EmptyViewTemplate == null) + ItemsView.AddLogicalChild(_formsEmptyView); } + + if (_emptyView != null && ListViewBase is IEmptyView emptyView) + { + emptyView.EmptyViewVisibility = Visibility.Visible; + + if (ActualWidth >= 0 && ActualHeight >= 0) + _formsEmptyView?.Layout(new Rectangle(0, 0, ActualWidth, ActualHeight)); + } + + _emptyViewDisplayed = true; + } + else + { + if (_emptyViewDisplayed) + { + if (_emptyView != null && ListViewBase is IEmptyView emptyView) + emptyView.EmptyViewVisibility = Visibility.Collapsed; + + ItemsView.RemoveLogicalChild(_formsEmptyView); + } + + _emptyViewDisplayed = false; } }