1
- using System . ComponentModel ;
1
+ using System ;
2
+ using System . ComponentModel ;
2
3
using Windows . UI . Xaml . Controls ;
3
4
using UWPApp = Windows . UI . Xaml . Application ;
4
- using Xamarin . Forms . Platform . UAP ;
5
+ using WListView = Windows . UI . Xaml . Controls . ListView ;
5
6
using WScrollMode = Windows . UI . Xaml . Controls . ScrollMode ;
7
+ using WSetter = Windows . UI . Xaml . Setter ;
8
+ using WStyle = Windows . UI . Xaml . Style ;
9
+ using WThickness = Windows . UI . Xaml . Thickness ;
6
10
7
11
namespace Xamarin . Forms . Platform . UWP
8
12
{
@@ -47,13 +51,13 @@ protected override ListViewBase SelectListViewBase()
47
51
{
48
52
case GridItemsLayout gridItemsLayout :
49
53
return CreateGridView ( gridItemsLayout ) ;
50
- case LinearItemsLayout listItemsLayout
51
- when listItemsLayout . Orientation == ItemsLayoutOrientation . Horizontal :
52
- return CreateHorizontalListView ( ) ;
54
+ case LinearItemsLayout listItemsLayout when listItemsLayout . Orientation == ItemsLayoutOrientation . Vertical :
55
+ return CreateVerticalListView ( listItemsLayout ) ;
56
+ case LinearItemsLayout listItemsLayout when listItemsLayout . Orientation == ItemsLayoutOrientation . Horizontal :
57
+ return CreateHorizontalListView ( listItemsLayout ) ;
53
58
}
54
59
55
- // Default to a plain old vertical ListView
56
- return new FormsListView ( ) ;
60
+ throw new NotImplementedException ( "The layout is not implemented" ) ;
57
61
}
58
62
59
63
protected virtual void UpdateHeader ( )
@@ -163,26 +167,54 @@ protected override void HandleLayoutPropertyChanged(PropertyChangedEventArgs pro
163
167
formsGridView . Span = ( ( GridItemsLayout ) Layout ) . Span ;
164
168
}
165
169
}
170
+ else if ( property . Is ( GridItemsLayout . HorizontalItemSpacingProperty ) || property . Is ( GridItemsLayout . VerticalItemSpacingProperty ) )
171
+ {
172
+ if ( ListViewBase is FormsGridView formsGridView )
173
+ {
174
+ formsGridView . ItemContainerStyle = GetItemContainerStyle ( ( GridItemsLayout ) Layout ) ;
175
+ }
176
+ }
177
+ else if ( property . Is ( LinearItemsLayout . ItemSpacingProperty ) )
178
+ {
179
+ switch ( ListViewBase )
180
+ {
181
+ case FormsListView formsListView :
182
+ formsListView . ItemContainerStyle = GetVerticalItemContainerStyle ( ( LinearItemsLayout ) Layout ) ;
183
+ break ;
184
+ case WListView listView :
185
+ listView . ItemContainerStyle = GetHorizontalItemContainerStyle ( ( LinearItemsLayout ) Layout ) ;
186
+ break ;
187
+ }
188
+ }
166
189
}
167
190
168
191
static ListViewBase CreateGridView ( GridItemsLayout gridItemsLayout )
169
192
{
170
193
return new FormsGridView
171
194
{
172
195
Orientation = gridItemsLayout . Orientation == ItemsLayoutOrientation . Horizontal
173
- ? Orientation . Horizontal
174
- : Orientation . Vertical ,
196
+ ? Orientation . Horizontal
197
+ : Orientation . Vertical ,
175
198
176
- Span = gridItemsLayout . Span
199
+ Span = gridItemsLayout . Span ,
200
+ ItemContainerStyle = GetItemContainerStyle ( gridItemsLayout )
177
201
} ;
178
202
}
179
203
180
- static ListViewBase CreateHorizontalListView ( )
204
+ static ListViewBase CreateVerticalListView ( LinearItemsLayout listItemsLayout )
181
205
{
182
- var horizontalListView = new Windows . UI . Xaml . Controls . ListView ( )
206
+ return new FormsListView ( )
183
207
{
184
- ItemsPanel =
185
- ( ItemsPanelTemplate ) UWPApp . Current . Resources [ "HorizontalListItemsPanel" ]
208
+ ItemContainerStyle = GetVerticalItemContainerStyle ( listItemsLayout )
209
+ } ;
210
+ }
211
+
212
+ static ListViewBase CreateHorizontalListView ( LinearItemsLayout listItemsLayout )
213
+ {
214
+ var horizontalListView = new WListView ( )
215
+ {
216
+ ItemsPanel = ( ItemsPanelTemplate ) UWPApp . Current . Resources [ "HorizontalListItemsPanel" ] ,
217
+ ItemContainerStyle = GetHorizontalItemContainerStyle ( listItemsLayout )
186
218
} ;
187
219
188
220
ScrollViewer . SetHorizontalScrollMode ( horizontalListView , WScrollMode . Auto ) ;
@@ -191,5 +223,36 @@ static ListViewBase CreateHorizontalListView()
191
223
192
224
return horizontalListView ;
193
225
}
226
+
227
+ static WStyle GetItemContainerStyle ( GridItemsLayout layout )
228
+ {
229
+ var h = layout ? . HorizontalItemSpacing ?? 0 ;
230
+ var v = layout ? . VerticalItemSpacing ?? 0 ;
231
+ var margin = new WThickness ( h , v , h , v ) ;
232
+
233
+ var style = new WStyle ( typeof ( GridViewItem ) ) ;
234
+ style . Setters . Add ( new WSetter ( GridViewItem . MarginProperty , margin ) ) ;
235
+ return style ;
236
+ }
237
+
238
+ static WStyle GetVerticalItemContainerStyle ( LinearItemsLayout layout )
239
+ {
240
+ var v = layout ? . ItemSpacing ?? 0 ;
241
+ var margin = new WThickness ( 0 , v , 0 , v ) ;
242
+
243
+ var style = new WStyle ( typeof ( ListViewItem ) ) ;
244
+ style . Setters . Add ( new WSetter ( ListViewItem . MarginProperty , margin ) ) ;
245
+ return style ;
246
+ }
247
+
248
+ static WStyle GetHorizontalItemContainerStyle ( LinearItemsLayout layout )
249
+ {
250
+ var h = layout ? . ItemSpacing ?? 0 ;
251
+ var padding = new WThickness ( h , 0 , h , 0 ) ;
252
+
253
+ var style = new WStyle ( typeof ( ListViewItem ) ) ;
254
+ style . Setters . Add ( new WSetter ( ListViewItem . PaddingProperty , padding ) ) ;
255
+ return style ;
256
+ }
194
257
}
195
- }
258
+ }
0 commit comments