{"id":38397,"date":"2018-12-07T14:00:30","date_gmt":"2018-12-07T19:00:30","guid":{"rendered":"https:\/\/blog.xamarin.com\/?p=38397"},"modified":"2019-03-25T13:38:37","modified_gmt":"2019-03-25T21:38:37","slug":"xamarin-forms-4-0-feature-preview-an-entirely-new-point-of-collectionview","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/xamarin-forms-4-0-feature-preview-an-entirely-new-point-of-collectionview\/","title":{"rendered":"Xamarin.Forms 4.0 Feature Preview: An Entirely New Point of (Collection)View"},"content":{"rendered":"<p>As part of the upcoming Xamarin.Forms 4.0 release, we are implementing the all new CollectionView\u00a0control. The CollectionView\u00a0is intended to be a successor to the ListView, improving upon its design by reducing technical complexity and allowing for more flexibility of layout and function. But we&#8217;re not stopping there! Along with this also comes the long-awaited CarouselView.<\/p>\n<p><strong>Technical note:<\/strong>\u00a0Enable the <span class=\"lang:c# decode:true crayon-inline\">CollectionView<\/span>\u00a0(which also enables the <span class=\"lang:c# decode:true crayon-inline\">CarouselView<\/span>) with a feature flag just before you initialize Xamarin.Forms in your MainActivity.cs and AppDelegate:<\/p>\n<pre class=\"lang:c# decode:true\">global::Xamarin.Forms.Forms.SetFlags(\"CollectionView_Experimental\");\r\n<\/pre>\n<p><strong>Follow along:\u00a0<\/strong>if you wish to follow along with this blog post you can clone and run the sample from <a href=\"https:\/\/github.com\/pauldipietro\/CollectionViewSample\">GitHub<\/a>. Alternatively, if you want to update an existing project to the <a href=\"https:\/\/www.nuget.org\/packages\/Xamarin.Forms\/4.0.0.8055-pre1\">Xamarin.Forms 4.0-pre NuGet<\/a> (available via your favorite NuGet package manager using the pre-release option).<\/p>\n<h2>A Basic Layout<\/h2>\n<p>One of the biggest changes between <span class=\"lang:c# decode:true crayon-inline \">ListView<\/span>\u00a0and\u00a0<span class=\"lang:c# decode:true crayon-inline\">CollectionView<\/span>\u00a0is the removal of wrapping content in a\u00a0<span class=\"lang:c# decode:true crayon-inline\">ViewCell<\/span>. This allows for significant gains to performance, especially on Android, while remaining familiar to what you\u2019ve done before when using the <span class=\"lang:c# decode:true crayon-inline\">ListView<\/span>.<\/p>\n<p>Below is an example layout. For simplicity\u2019s sake, create a <span class=\"lang:c# decode:true crayon-inline\">List&lt;string&gt;<\/span>\u00a0of people&#8217;s names in your binding context and then bind to it in XAML:<\/p>\n<pre class=\"marking:true lang:xhtml mark:3 decode:true\">&lt;StackLayout&gt;\r\n    &lt;CollectionView x:Name=\"CV\" \r\n                    ItemsSource=\"{Binding People}\" \r\n                    Margin=\"10,0,0,0\"&gt;\r\n        &lt;CollectionView.ItemTemplate&gt;\r\n            &lt;DataTemplate&gt;\r\n                &lt;StackLayout HeightRequest=\"50\" WidthRequest=\"200\" Orientation=\"Horizontal\" Padding=\"0,5,0,5\"&gt;\r\n                    &lt;Image Source=\"person\"\/&gt;\r\n                    &lt;Label Text=\"{Binding}\" VerticalOptions=\"Center\"\/&gt;\r\n                &lt;\/StackLayout&gt;\r\n            &lt;\/DataTemplate&gt;\r\n        &lt;\/CollectionView.ItemTemplate&gt;\r\n    &lt;\/CollectionView&gt;\r\n&lt;\/StackLayout&gt;<\/pre>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/basic.png\"><img decoding=\"async\" class=\"aligncenter wp-image-38403\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/basic.png\" alt=\"A Basic CollectionView\" width=\"392\" height=\"700\" \/><\/a><\/p>\n<p>You now have a basic list of items. Notice that you no longer use a <span class=\"lang:c# decode:true crayon-inline\">ViewCell<\/span>, but a\u00a0<span class=\"lang:c# decode:true crayon-inline\">DataTemplate<\/span><span style=\"background-color: #ffffff;font-family: Georgia,'Times New Roman','Bitstream Charter',Times,serif;font-size: medium\">!<\/span> Hopefully, so far this new\u00a0<span class=\"lang:c# decode:true crayon-inline\">CollectionView<\/span>\u00a0is familiar to the <span class=\"lang:c# decode:true crayon-inline\">ListView<\/span>\u00a0you&#8217;ve used in the past. Let&#8217;s look now at what else it can do.<\/p>\n<h2>Making A Grid Layout<\/h2>\n<p>By default, <span class=\"lang:c# decode:true crayon-inline\">CollectionView<\/span>\u00a0assumes a linear layout. That means you can not only do vertical lists, but horizontal as well! This is accomplished by setting the\u00a0<span class=\"lang:c# decode:true crayon-inline\">ItemsLayout<\/span>\u00a0property to a new instance of\u00a0<span class=\"lang:c# decode:true crayon-inline\">ListItemsLayout<\/span>\u00a0which takes an\u00a0<span class=\"lang:c# decode:true crayon-inline\">ItemsLayoutOrientation<\/span>\u00a0parameter (Vertical|Horizontal).<\/p>\n<p>There&#8217;s also another option available: the <span class=\"lang:c# decode:true crayon-inline\">GridItemsLayout<\/span>.<\/p>\n<p>Going back to the previous example above, you can use this option to make the\u00a0<span class=\"lang:c# decode:true crayon-inline \">CollectionView<\/span> look a little fancier. Using a <span class=\"lang:c# decode:true crayon-inline\">GridItemsLayout<\/span>, you still get a scrollable list, but now with two items in each row:<\/p>\n<pre class=\"marking:true lang:c# mark:4 decode:true\">  &lt;StackLayout&gt;\r\n      &lt;CollectionView x:Name=\"CV\" ItemsSource=\"{Binding People}\" VerticalOptions=\"Center\" HorizontalOptions=\"Center\" Margin=\"10,0,10,0\"&gt;\r\n          &lt;CollectionView.ItemsLayout&gt;\r\n              &lt;GridItemsLayout Orientation=\"Horizontal\" Span=\"2\"\/&gt;\r\n          &lt;\/CollectionView.ItemsLayout&gt;\r\n          &lt;CollectionView.ItemTemplate&gt;\r\n              &lt;DataTemplate&gt;\r\n                  &lt;Frame BorderColor=\"LightGray\" CornerRadius=\"3\" HasShadow=\"False\"&gt;\r\n                      &lt;Grid&gt;\r\n                          &lt;Grid.ColumnDefinitions&gt;\r\n                              &lt;ColumnDefinition Width=\"Auto\"\/&gt;\r\n                              &lt;ColumnDefinition Width=\"100\" \/&gt;\r\n                          &lt;\/Grid.ColumnDefinitions&gt;\r\n                          &lt;Grid.RowDefinitions&gt;\r\n                              &lt;RowDefinition Height=\"Auto\"\/&gt;\r\n                          &lt;\/Grid.RowDefinitions&gt;\r\n                          &lt;Image Grid.Column=\"0\" Source=\"person\" Aspect=\"Fill\"\/&gt;\r\n                          &lt;StackLayout Grid.Column=\"1\"&gt;\r\n                              &lt;Label Text=\"{Binding}\" HorizontalOptions=\"EndAndExpand\" VerticalOptions=\"CenterAndExpand\"\/&gt;\r\n                          &lt;\/StackLayout&gt;\r\n                      &lt;\/Grid&gt;\r\n                  &lt;\/Frame&gt;\r\n              &lt;\/DataTemplate&gt;\r\n          &lt;\/CollectionView.ItemTemplate&gt;\r\n      &lt;\/CollectionView&gt;\r\n  &lt;\/StackLayout&gt;<\/pre>\n<p>Note the <span class=\"lang:c# decode:true crayon-inline \">ItemsLayout<\/span>\u00a0in the above XAML. Setting the <span class=\"lang:c# decode:true crayon-inline \">Orientation<\/span>\u00a0to <span class=\"lang:c# decode:true crayon-inline \">Vertical<\/span>\u00a0indicates the direction in which the <span class=\"lang:c# decode:true crayon-inline\">CollectionView<\/span>\u00a0expands, and the <span class=\"lang:c# decode:true crayon-inline\">Span<\/span>\u00a0property sets the number of items per row. In this instance, when you choose to display two you get this result:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/grid.png\"><img decoding=\"async\" class=\"aligncenter wp-image-38407\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/grid.png\" alt=\"A Grid-style CollectionView\" width=\"392\" height=\"700\" \/><\/a><\/p>\n<h2>CarouselView is Here<\/h2>\n<p>The previous example shows a scrollable grid of people, but perhaps you want to show one person at a time in a card format. You can use\u00a0<span class=\"lang:c# decode:true crayon-inline\">CarouselView<span style=\"background-color: #ffffff;font-family: Georgia,'Times New Roman','Bitstream Charter',Times,serif;font-size: medium\">,<\/span><\/span>\u00a0a separate control which uses\u00a0<span class=\"lang:c# decode:true crayon-inline \">CollectionView<\/span>\u00a0as its basis, can be used to adjust your layout, and add in some business card-like information:<\/p>\n<pre class=\"lang:xhtml decode:true\">&lt;CarouselView   x:Name=\"CV\" \r\n                ItemsSource=\"{Binding People}\" \r\n                HeightRequest=\"200\" \r\n                HorizontalOptions=\"Center\" \r\n                VerticalOptions=\"CenterAndExpand\" \r\n                Margin=\"10\"&gt;\r\n    &lt;CarouselView.ItemsLayout&gt;\r\n        &lt;GridItemsLayout Orientation=\"Horizontal\"\/&gt;\r\n    &lt;\/CarouselView.ItemsLayout&gt;\r\n    &lt;CarouselView.ItemTemplate&gt;\r\n        &lt;DataTemplate&gt;\r\n            &lt;Frame BorderColor=\"LightGray\" CornerRadius=\"3\" HasShadow=\"False\"&gt;\r\n                &lt;Grid&gt;\r\n                    &lt;Grid.ColumnDefinitions&gt;\r\n                        &lt;ColumnDefinition Width=\"75\"\/&gt;\r\n                        &lt;ColumnDefinition Width=\"*\" \/&gt;\r\n                    &lt;\/Grid.ColumnDefinitions&gt;\r\n                    &lt;Grid.RowDefinitions&gt;\r\n                        &lt;RowDefinition Height=\"*\"\/&gt;\r\n                        &lt;RowDefinition Height=\"*\"\/&gt;\r\n                    &lt;\/Grid.RowDefinitions&gt;\r\n                    &lt;Image Grid.Column=\"0\" Grid.Row=\"0\" Source=\"person\" VerticalOptions=\"Start\"\/&gt;\r\n                    &lt;StackLayout Grid.Column=\"1\" Grid.Row=\"1\" HorizontalOptions=\"EndAndExpand\" VerticalOptions=\"EndAndExpand\"&gt;\r\n                        &lt;Label Text=\"{Binding}\" FontSize=\"24\" HorizontalOptions=\"EndAndExpand\"\/&gt;\r\n                        &lt;Label Text=\"Company Address\" HorizontalOptions=\"EndAndExpand\"\/&gt;\r\n                        &lt;Label Text=\"City, State\" HorizontalOptions=\"EndAndExpand\"\/&gt;\r\n                    &lt;\/StackLayout&gt;\r\n                &lt;\/Grid&gt;\r\n            &lt;\/Frame&gt;\r\n        &lt;\/DataTemplate&gt;\r\n    &lt;\/CarouselView.ItemTemplate&gt;\r\n&lt;\/CarouselView&gt;\r\n<\/pre>\n<h2><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/carousel.gif\"><img decoding=\"async\" class=\"aligncenter wp-image-38416\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/carousel.gif\" alt=\"A page using a CarouselView\" width=\"388\" height=\"700\" \/><\/a><\/h2>\n<h2>It\u2019s a Snap to Use<\/h2>\n<p>One of the great features provided by\u00a0<span class=\"lang:c# decode:true crayon-inline \">CollectionView<\/span>\u00a0is the ability to add snap points. For the page you just designed, it allows for the view to stop part-way between cards. What if you instead want only one card to appear centered at a time? With the <span class=\"lang:c# decode:true crayon-inline \">SnapPointsAlignment<\/span>\u00a0and <span class=\"lang:c# decode:true crayon-inline \">SnapPointsType<\/span>\u00a0properties on the <span class=\"lang:c# decode:true crayon-inline\">ItemsLayout<\/span>, you can configure this behavior in a snap (pun intended)! Add the following to your XAML for enabling this behavior:<\/p>\n<pre class=\"marking:true lang:c# mark:4-5 decode:true\">&lt;CarouselView.ItemsLayout&gt;\r\n    &lt;GridItemsLayout \r\n        Orientation=\"Horizontal\" \r\n        SnapPointsAlignment=\"Center\" \r\n        SnapPointsType=\"Mandatory\"\/&gt;\r\n&lt;\/CarouselView.ItemsLayout&gt;<\/pre>\n<h2><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/snap.gif\"><img decoding=\"async\" class=\"aligncenter wp-image-38417\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/snap.gif\" alt=\"A CarouselView using a SnapPoint\" width=\"388\" height=\"700\" \/><\/a><\/h2>\n<h2>It\u2019s so Empty in Here<\/h2>\n<p>Lastly, a common scenario many developers may face is what to display when the\u00a0<span class=\"lang:c# decode:true crayon-inline \">ItemsSource<\/span> is empty. With the <span class=\"lang:c# decode:true crayon-inline\">CollectionView<\/span>\u00a0you can now set any content to the <span class=\"lang:c# decode:true crayon-inline \">EmptyView<\/span>\u00a0 of your list for just this purpose. With the XAML below, the string &#8220;No items currently exist!&#8221; will be shown inside of the <span class=\"lang:c# decode:true crayon-inline \">CollectionView<\/span>\u00a0when the list of people is empty:<\/p>\n<pre class=\"lang:c# decode:true\">&lt;CollectionView.EmptyView&gt;\r\n    &lt;Label Text=\"No items currently exist!\"\/&gt;\r\n&lt;\/CollectionView.EmptyView&gt;\r\n<\/pre>\n<h2><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/empty.png\"><img decoding=\"async\" class=\"aligncenter wp-image-38418\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/empty.png\" alt=\"An EmptyView\" width=\"394\" height=\"700\" \/><\/a><\/h2>\n<p>You can customize this view however you like, including interactive content and supporting bindings.<\/p>\n<h2>Try it today!<\/h2>\n<p>With the early preview release of Xamarin.Forms 4.0, the <span class=\"lang:c# decode:true crayon-inline \">CollectionView<\/span>\u00a0is also in a early preview state. As such, it is missing some features, and feature parity between Android and iOS is not complete. You can track the development progress of this control <a href=\"https:\/\/gist.github.com\/hartez\/7d0edd4182dbc7de65cebc6c67f72e14\">on GitHub<\/a>.<\/p>\n<p>New functionality will be available quickly on <a href=\"https:\/\/github.com\/xamarin\/\/wiki\/Nightly-Builds\">nightly releases<\/a>, and ship with future pre-releases. Have feedback? Take <a href=\"https:\/\/aka.ms\/xf-4-feedback\">this survey<\/a> covering <a href=\"https:\/\/blog.xamarin.com\/xamarin-forms-4-0-preview\/\">all the new Xamarin.Forms 4.0 features<\/a> we are now previewing.<\/p>\n<p>We invite and encourage you to <a href=\"https:\/\/github.com\/xamarin\/\/issues\/3172\">join the conversation<\/a> on the Xamarin.Forms GitHub repo where the <span class=\"lang:c# decode:true crayon-inline \">CollectionView<\/span>\u00a0and <span class=\"lang:c# decode:true crayon-inline \">CarouselView<\/span>\u00a0are being discussed. If you&#8217;d like to try out this sample, grab the code on <a href=\"https:\/\/github.com\/pauldipietro\/CollectionViewSample\">GitHub<\/a>.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As part of the upcoming Xamarin.Forms 4.0 release, we are implementing the all new CollectionView\u00a0control. The CollectionView\u00a0is intended to be a successor to the ListView, improving upon its design by reducing technical complexity and allowing for more flexibility of layout and function.<\/p>\n","protected":false},"author":1958,"featured_media":40919,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2,367],"tags":[750,751,409],"class_list":["post-38397","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","category-xamarin-forms","tag-carouselview","tag-collectionview","tag-releases"],"acf":[],"blog_post_summary":"<p>As part of the upcoming Xamarin.Forms 4.0 release, we are implementing the all new CollectionView\u00a0control. The CollectionView\u00a0is intended to be a successor to the ListView, improving upon its design by reducing technical complexity and allowing for more flexibility of layout and function.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/38397","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/1958"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=38397"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/38397\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media\/40919"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=38397"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=38397"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=38397"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}