[UWP] Propagate CollectionView BindingContext to EmptyView (#10574) fixes #9833

* Added repro sample

* Updated repro sample

* Updated EmptyView logic in UWP to propagate the BC
This commit is contained in:
Javier Suárez 2020-06-01 11:15:49 +02:00 коммит произвёл GitHub
Родитель 830b0bfc06
Коммит 47e55a852e
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 141 добавлений и 16 удалений

Просмотреть файл

@ -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<string> _items;
public Issue9833ViewModel()
{
LoadData();
}
public ObservableCollection<string> Items
{
get { return _items; }
set
{
_items = value;
OnPropertyChanged();
}
}
void LoadData()
{
Items = new ObservableCollection<string>();
}
}
}

Просмотреть файл

@ -196,6 +196,7 @@
<SubType>Code</SubType>
<DependentUpon>Issue9771.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Issue9833.cs" />
<Compile Include="$(MSBuildThisFileDirectory)RefreshViewTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue7338.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ScrollToGroup.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;
}
}