1
+ using System . Collections . ObjectModel ;
2
+ using System . Threading . Tasks ;
3
+ using System . Windows . Input ;
4
+ using Xamarin . Forms . CustomAttributes ;
5
+ using Xamarin . Forms . Internals ;
6
+
7
+ #if UITEST
8
+ using Xamarin . Forms . Core . UITests ;
9
+ using Xamarin . UITest ;
10
+ using NUnit . Framework ;
11
+ #endif
12
+
13
+ namespace Xamarin . Forms . Controls . Issues
14
+ {
15
+ [ Preserve ( AllMembers = true ) ]
16
+ [ Issue ( IssueTracker . Bugzilla , 8392 , "[Bug] CollectionView does not marshal ObservableCollection.CollectionChanged to MainThread" ) ]
17
+ public class Issue8392 : TestContentPage
18
+ {
19
+ public Issue8392 ( )
20
+ {
21
+ Title = "Issue 8392" ;
22
+
23
+ var collectionView = new CollectionView
24
+ {
25
+ ItemTemplate = new Issue8392DataTemplate ( )
26
+ } ;
27
+
28
+ collectionView . SetBinding ( ItemsView . ItemsSourceProperty , nameof ( Issue8392ViewModel . DataList ) ) ;
29
+
30
+ var refreshView = new RefreshView
31
+ {
32
+ Content = collectionView
33
+ } ;
34
+
35
+ refreshView . SetBinding ( RefreshView . IsRefreshingProperty , nameof ( Issue8392ViewModel . IsRefreshing ) ) ;
36
+ refreshView . SetBinding ( RefreshView . CommandProperty , nameof ( Issue8392ViewModel . PullToRefreshCommand ) ) ;
37
+
38
+ Content = refreshView ;
39
+
40
+ BindingContext = new Issue8392ViewModel ( ) ;
41
+ }
42
+
43
+ protected override void Init ( )
44
+ {
45
+
46
+ }
47
+
48
+ protected override void OnAppearing ( )
49
+ {
50
+ base . OnAppearing ( ) ;
51
+
52
+ if ( Content is RefreshView refreshView
53
+ && refreshView . Content is CollectionView )
54
+ {
55
+ refreshView . IsRefreshing = true ;
56
+ }
57
+ }
58
+ }
59
+
60
+ [ Preserve ( AllMembers = true ) ]
61
+ public class Issue8392DataTemplate : DataTemplate
62
+ {
63
+ public Issue8392DataTemplate ( ) : base ( CreateTemplate )
64
+ {
65
+ }
66
+
67
+ static Layout CreateTemplate ( )
68
+ {
69
+ var numberLabel = new Label
70
+ {
71
+ FontSize = 14 ,
72
+ HorizontalOptions = LayoutOptions . Start ,
73
+ VerticalOptions = LayoutOptions . Center ,
74
+ HorizontalTextAlignment = TextAlignment . Start ,
75
+ VerticalTextAlignment = TextAlignment . Center ,
76
+ } ;
77
+ numberLabel . SetBinding ( Label . TextProperty , nameof ( Issue8392Model . Number ) ) ;
78
+
79
+ var grid = new Grid
80
+ {
81
+ RowDefinitions =
82
+ {
83
+ new RowDefinition { Height = new GridLength ( 20 , GridUnitType . Absolute ) }
84
+ } ,
85
+ ColumnDefinitions =
86
+ {
87
+ new ColumnDefinition { Width = new GridLength ( 1 , GridUnitType . Star ) }
88
+ }
89
+ } ;
90
+
91
+ grid . Children . Add ( numberLabel , 0 , 0 ) ;
92
+
93
+ return grid ;
94
+ }
95
+ }
96
+
97
+ public class Issue8392Model
98
+ {
99
+ public Issue8392Model ( int number ) => Number = number ;
100
+
101
+ public int Number { get ; }
102
+ }
103
+
104
+ [ Preserve ( AllMembers = true ) ]
105
+ public class Issue8392ViewModel : BindableObject
106
+ {
107
+ bool _isRefreshing ;
108
+
109
+ public ICommand PullToRefreshCommand => new Command ( async ( ) => await ExecutePullToRefreshCommand ( ) ) ;
110
+
111
+ public bool IsRefreshing
112
+ {
113
+ get { return _isRefreshing ; }
114
+ set
115
+ {
116
+ _isRefreshing = value ;
117
+ OnPropertyChanged ( ) ;
118
+ }
119
+ }
120
+
121
+ public ObservableCollection < Issue8392Model > DataList { get ; } = new ObservableCollection < Issue8392Model > ( ) ;
122
+
123
+ async Task ExecutePullToRefreshCommand ( )
124
+ {
125
+ DataList . Clear ( ) ;
126
+
127
+ try
128
+ {
129
+ // If the items load without exception, the test has passed.
130
+ for ( int i = 0 ; i < 30 ; i ++ )
131
+ {
132
+ await Task . Delay ( 200 ) . ConfigureAwait ( false ) ;
133
+ DataList . Add ( new Issue8392Model ( i + 1 ) ) ;
134
+ }
135
+ }
136
+ finally
137
+ {
138
+ IsRefreshing = false ;
139
+ }
140
+ }
141
+ }
142
+ }
0 commit comments