{"id":26122,"date":"2016-06-15T11:15:53","date_gmt":"2016-06-15T18:15:53","guid":{"rendered":"https:\/\/blog.xamarin.com\/?p=26122"},"modified":"2016-06-15T11:15:53","modified_gmt":"2016-06-15T18:15:53","slug":"flip-through-items-with-xamarin-forms-carouselview","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/flip-through-items-with-xamarin-forms-carouselview\/","title":{"rendered":"Flip through items with Xamarin.Forms CarouselView"},"content":{"rendered":"<p>\t\t\t\tThe<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/Carousel.png\" alt=\"Carousel\" width=\"75\" class=\"alignright size-full wp-image-26125\" \/> latest preview release of Xamarin.Forms (2.3.0) includes some great new features, including Themes, <a href=\"https:\/\/blog.xamarin.com\/scaffolding-xamarin-forms-pages-with-datapages\/\">Data Pages<\/a>, <a href=\"https:\/\/blog.xamarin.com\/deep-link-content-with-xamarin-forms-url-navigation\/\">URL Navigation<\/a>, and a brand new control, the <strong>CarouselView<\/strong>. You can think of the CarouselView as the successor to the CarouselPage, which had the restriction of a page that couldn&#8217;t be embedded easily. In contrast, the new CarouselView is highly optimized on each platform, allowing you to flip through many items with a simple, familiar API with an <strong>ItemsSource<\/strong> and a <strong>DataTemplate<\/strong>.<\/p>\n<h2>Get the CarouselView<\/h2>\n<p>Ensure that your projects are on the latest version of Xamarin.Forms and then install the <a href=\"https:\/\/www.nuget.org\/packages\/Xamarin.Forms.CarouselView\">CarouselView NuGet<\/a> in your iOS, Android, Windows, and Portable Class Library (if applicable).<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/Carousel_NuGets.png\" alt=\"Carousel_NuGets\" width=\"650\" class=\"aligncenter size-full wp-image-26123\" \/><\/p>\n<p>There are many use cases for the CarouselView; the top one that I hear is having a Carousel on the top of a page to show off specific items, as seen in the app stores. For this example, we&#8217;re going to take an existing application that includes a list of monkeys and add a CarouselView to the top that highlights zoos where you can see them.<\/p>\n<p>A zoo is represented by an image and name:<\/p>\n<pre class=\"lang:csharp decode:true\">\npublic class Zoo\n{\n    public string ImageUrl { get; set; }\n    public string Name { get; set; }\n}\n<\/pre>\n<p>In our View Model that will be set to the <em>BindingContext<\/em> of the page, we have an <em>ObservableCollection<\/em> of Zoo items:<\/p>\n<pre class=\"lang:csharp decode:true\">\npublic class MonkeysViewModel\n{\n    public ObservableCollection Monkeys { get; set; }\n    public ObservableCollection&lt;Grouping&gt; MonkeysGrouped { get; set; }\n\n    public ObservableCollection Zoos { get; set; }\n\n    public MonkeysViewModel()\n    {\n\n        Monkeys = MonkeyHelper.Monkeys;\n        MonkeysGrouped = MonkeyHelper.MonkeysGrouped;\n        Zoos = new ObservableCollection\n        {\n            new Zoo\n            {\n                ImageUrl = \"http:\/\/content.screencast.com\/users\/JamesMontemagno\/folders\/Jing\/media\/23c1dd13-333a-459e-9e23-c3784e7cb434\/2016-06-02_1049.png\",\n                Name = \"Woodland Park Zoo\"\n            },\n                new Zoo\n            {\n                ImageUrl =    \"http:\/\/content.screencast.com\/users\/JamesMontemagno\/folders\/Jing\/media\/6b60d27e-c1ec-4fe6-bebe-7386d545bb62\/2016-06-02_1051.png\",\n                Name = \"Cleveland Zoo\"\n                },\n            new Zoo\n            {\n                ImageUrl = \"http:\/\/content.screencast.com\/users\/JamesMontemagno\/folders\/Jing\/media\/e8179889-8189-4acb-bac5-812611199a03\/2016-06-02_1053.png\",\n                Name = \"Phoenix Zoo\"\n            }\n        };\n    }\n}\n<\/pre>\n<h2>Adding the CarouselView<\/h2>\n<p>Since the CarouselView is in a separate assembly, we must bring in the CarouselView&#8217;s namespace in root of our page:<\/p>\n<pre class=\"lang:xml decode:true\">\nxmlns:cv=\"clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.CarouselView\"\n<\/pre>\n<p>Then we can simply add the CarouselView at the top of our page:<\/p>\n<pre class=\"lang:xml decode:true\">\n\n  \n    \n    \n  \n  \n    \n      \n        \n          \n            \n            \n          \n          \n          \n            <Label \/>\n          \n        \n      \n    \n  \n  <!--List of Monkeys below-->\n\n<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/ezgif.com-video-to-gif.gif\" alt=\"CarouselView In Action\" width=\"466\" height=\"480\" class=\"aligncenter size-full wp-image-26126\" \/><\/p>\n<p>As you can see, the CarouselView has an <em>ItemsSource<\/em> with a binding to the ObservableCollection  that we created. Each zoo then has its own <em>DataTemplate<\/em> to display the its logo and name.<\/p>\n<h3>Windows Setup<\/h3>\n<p>If you are building applications for Windows, a DataTemplate needs to be added to the Application Resources in the App.xaml of the application. <\/p>\n<p>First add a new namespace in the root node:<\/p>\n<pre class=\"lang:csharp decode:true\">\nxmlns:uwp=\"using:Xamarin.Forms.Platform\"\n<\/pre>\n<p>Then add the following to the Application.Resources node:<\/p>\n<pre class=\"lang:csharp decode:true\">\n\n    \n\n<\/pre>\n<p>It should look something like this:<\/p>\n<pre class=\"lang:csharp decode:true\">\n\n    \n        \n            \n        \n    \n\n<\/pre>\n<h2>CarouselView Events<\/h2>\n<p>Since the CarouselView is a control, you have access to a wide variety of animation and life cycle events that you would expect, but you can also subscribe to the <em>ItemSelected<\/em> event to see when users flip through the view:<\/p>\n<pre class=\"lang:csharp decode:true\">\nCarouselZoos.ItemSelected += (sender, args) =&gt;\n{\n  var zoo = args.SelectedItem as Zoo;\n  if (zoo == null)\n    return;\n\n  Title = zoo.Name;\n};\n<\/pre>\n<h2>Embed Anywhere<\/h2>\n<p>Since the CarouselView is a view, you can put it anywhere on the page or even inside of the ListView&#8217;s header so it scrolls with the ListView:<\/p>\n<pre class=\"lang:xml decode:true\">\n\n  \n    \n      \n        \n          \n            \n              \n              \n            \n            \n            \n              <Label \/>\n            \n          \n        \n      \n    \n  \n\n<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/ezgif.com-video-to-gif-1.gif\" alt=\"CarouselViewHeader\" width=\"456\" height=\"480\" class=\"aligncenter size-full wp-image-26127\" \/><\/p>\n<h2>Release<\/h2>\n<p>To ensure that the CarouselView does not get &#8220;Linked-out&#8221; when compiling in release mode ensure that you are using Compiled XAML by adding the following code into the project&#8217;s AssemblyInfo.cs where your XAMLexists:<\/p>\n<pre class=\"lang:csharp decode:true\">\nusing Xamarin.Forms.Xaml;\n[assembly: XamlCompilation(XamlCompilationOptions.Compile)]\n<\/pre>\n<p>Or add the following code after the Forms.Init(); call in each project:<\/p>\n<pre class=\"lang:csharp decode:true\">\nvar cv = typeof (Xamarin.Forms.CarouselView);\nvar assembly = Assembly.Load(cv.FullName);\n<\/pre>\n<h2>Learn More<\/h2>\n<p>To learn more, be sure to read through all of our <a href=\"http:\/\/forums.xamarin.com\/discussion\/66431\/xamarin-forms-2-2-2-3-docs\">updated documentation for Xamarin.Forms 2.3.0<\/a> and of course browse the full source code for this sample on <a href=\"https:\/\/github.com\/jamesmontemagno\/Xamarin.Forms-Monkeys\">GitHub<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The latest preview release of Xamarin.Forms (2.3.0) includes some great new features, including Themes, Data Pages, URL Navigation, and a brand new control, the CarouselView. You can think of the CarouselView as the successor to the CarouselPage, which had the restriction of a page that couldn&#8217;t be embedded easily. In contrast, the new CarouselView is [&hellip;]<\/p>\n","protected":false},"author":544,"featured_media":26125,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[4,16],"class_list":["post-26122","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","tag-xamarin-platform","tag-xamarin-forms"],"acf":[],"blog_post_summary":"<p>The latest preview release of Xamarin.Forms (2.3.0) includes some great new features, including Themes, Data Pages, URL Navigation, and a brand new control, the CarouselView. You can think of the CarouselView as the successor to the CarouselPage, which had the restriction of a page that couldn&#8217;t be embedded easily. In contrast, the new CarouselView is [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/26122","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\/544"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=26122"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/26122\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=26122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=26122"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=26122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}