|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Collections.ObjectModel; |
| 4 | +using System.Collections.Specialized; |
| 5 | +using System.ComponentModel; |
| 6 | +using System.Globalization; |
| 7 | +using System.Text; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Xamarin.Forms.CustomAttributes; |
| 10 | +using Xamarin.Forms.Internals; |
| 11 | + |
| 12 | +#if UITEST |
| 13 | +using Xamarin.UITest; |
| 14 | +using NUnit.Framework; |
| 15 | +using Xamarin.Forms.Core.UITests; |
| 16 | +#endif |
| 17 | + |
| 18 | +namespace Xamarin.Forms.Controls.Issues |
| 19 | +{ |
| 20 | + [Issue(IssueTracker.Github, 13126, "[Bug] Regression: 5.0.0-pre5 often fails to draw dynamically loaded collection view content", PlatformAffected.iOS)] |
| 21 | +#if UITEST |
| 22 | + [NUnit.Framework.Category(UITestCategories.CollectionView)] |
| 23 | +#endif |
| 24 | + public class Issue13126 : TestContentPage |
| 25 | + { |
| 26 | + _13126VM _vm; |
| 27 | + const string Success = "Success"; |
| 28 | + |
| 29 | + protected override void Init() |
| 30 | + { |
| 31 | + var collectionView = BindingWithConverter(); |
| 32 | + |
| 33 | + var grid = new Grid |
| 34 | + { |
| 35 | + RowDefinitions = new RowDefinitionCollection |
| 36 | + { |
| 37 | + new RowDefinition() { Height = GridLength.Star }, |
| 38 | + } |
| 39 | + }; |
| 40 | + |
| 41 | + grid.Children.Add(collectionView); |
| 42 | + |
| 43 | + Content = grid; |
| 44 | + |
| 45 | + _vm = new _13126VM(); |
| 46 | + BindingContext = _vm; |
| 47 | + } |
| 48 | + |
| 49 | + protected async override void OnParentSet() |
| 50 | + { |
| 51 | + base.OnParentSet(); |
| 52 | + _vm.IsBusy = true; |
| 53 | + |
| 54 | + await Task.Delay(1000); |
| 55 | + |
| 56 | + _vm.Data.Add(Success); |
| 57 | + |
| 58 | + _vm.IsBusy = false; |
| 59 | + } |
| 60 | + |
| 61 | + internal static CollectionView BindingWithConverter() |
| 62 | + { |
| 63 | + var cv = new CollectionView |
| 64 | + { |
| 65 | + IsVisible = true, |
| 66 | + |
| 67 | + ItemTemplate = new DataTemplate(() => |
| 68 | + { |
| 69 | + var label = new Label(); |
| 70 | + label.SetBinding(Label.TextProperty, new Binding(".")); |
| 71 | + return label; |
| 72 | + }) |
| 73 | + }; |
| 74 | + |
| 75 | + cv.EmptyView = new Label { Text = "Should not see me" }; |
| 76 | + |
| 77 | + cv.SetBinding(CollectionView.ItemsSourceProperty, new Binding("Data")); |
| 78 | + cv.SetBinding(VisualElement.IsVisibleProperty, new Binding("IsBusy", converter: new BoolInverter())); |
| 79 | + |
| 80 | + return cv; |
| 81 | + } |
| 82 | + |
| 83 | + class BoolInverter : IValueConverter |
| 84 | + { |
| 85 | + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) |
| 86 | + { |
| 87 | + return !((bool)value); |
| 88 | + } |
| 89 | + |
| 90 | + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) |
| 91 | + { |
| 92 | + throw new NotImplementedException(); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + internal class _13126VM : INotifyPropertyChanged |
| 97 | + { |
| 98 | + private bool _isBusy; |
| 99 | + |
| 100 | + public bool IsBusy |
| 101 | + { |
| 102 | + get |
| 103 | + { |
| 104 | + return _isBusy; |
| 105 | + } |
| 106 | + |
| 107 | + set |
| 108 | + { |
| 109 | + _isBusy = value; |
| 110 | + OnPropertyChanged(nameof(IsBusy)); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + public OptimizedObservableCollection<string> Data { get; } = new OptimizedObservableCollection<string>(); |
| 115 | + |
| 116 | + public event PropertyChangedEventHandler PropertyChanged; |
| 117 | + |
| 118 | + void OnPropertyChanged(string name) |
| 119 | + { |
| 120 | + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + internal class OptimizedObservableCollection<T> : ObservableCollection<T> |
| 125 | + { |
| 126 | + bool _shouldRaiseNotifications = true; |
| 127 | + |
| 128 | + public OptimizedObservableCollection() |
| 129 | + { |
| 130 | + } |
| 131 | + |
| 132 | + public OptimizedObservableCollection(IEnumerable<T> collection) |
| 133 | + : base(collection) |
| 134 | + { |
| 135 | + } |
| 136 | + |
| 137 | + public IDisposable BeginMassUpdate() |
| 138 | + { |
| 139 | + return new MassUpdater(this); |
| 140 | + } |
| 141 | + |
| 142 | + protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e) |
| 143 | + { |
| 144 | + if (_shouldRaiseNotifications) |
| 145 | + base.OnCollectionChanged(e); |
| 146 | + } |
| 147 | + |
| 148 | + protected override void OnPropertyChanged(PropertyChangedEventArgs e) |
| 149 | + { |
| 150 | + if (_shouldRaiseNotifications) |
| 151 | + base.OnPropertyChanged(e); |
| 152 | + } |
| 153 | + |
| 154 | + class MassUpdater : IDisposable |
| 155 | + { |
| 156 | + readonly OptimizedObservableCollection<T> parent; |
| 157 | + public MassUpdater(OptimizedObservableCollection<T> parent) |
| 158 | + { |
| 159 | + this.parent = parent; |
| 160 | + parent._shouldRaiseNotifications = false; |
| 161 | + } |
| 162 | + |
| 163 | + public void Dispose() |
| 164 | + { |
| 165 | + parent._shouldRaiseNotifications = true; |
| 166 | + parent.OnPropertyChanged(new PropertyChangedEventArgs("Count")); |
| 167 | + parent.OnPropertyChanged(new PropertyChangedEventArgs("Item[]")); |
| 168 | + parent.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | +#if UITEST |
| 174 | + [Test] |
| 175 | + public void CollectionViewShouldSourceShouldUpdateWhileInvisible() |
| 176 | + { |
| 177 | + RunningApp.WaitForElement(Success); |
| 178 | + } |
| 179 | +#endif |
| 180 | + } |
| 181 | +} |
0 commit comments