{"id":45803,"date":"2019-11-20T14:02:19","date_gmt":"2019-11-20T22:02:19","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/xamarin\/?p=45803"},"modified":"2020-04-28T02:02:42","modified_gmt":"2020-04-28T09:02:42","slug":"refreshview-xamarin-forms","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/refreshview-xamarin-forms\/","title":{"rendered":"Pull-to-refresh with Xamarin.Forms RefreshView"},"content":{"rendered":"<p>There are tons of great new features packed into <a href=\"https:\/\/docs.microsoft.com\/xamarin\/whats-new\/2019-10?WT.mc_id=refreshview-xamarinblog-jamont#xamarinforms-43\">Xamarin.Forms 4.3<\/a>. A new favorite feature is <a href=\"https:\/\/docs.microsoft.com\/xamarin\/xamarin-forms\/user-interface\/collectionview\/?WT.mc_id=refreshview-xamarinblog-jamont\">CollectionView<\/a>. A highly performant and flexible way of displaying a list of data in a variety of layouts. One frequently-asked question with CollectionView, or other scrolling layouts, is how to add Pull-to-Refresh functionality similar to ListView. The answer: wrap it in the new <a href=\"https:\/\/docs.microsoft.com\/xamarin\/xamarin-forms\/user-interface\/refreshview?WT.mc_id=refreshview-xamarinblog-jamont\" rel=\"noopener noreferrer\" target=\"_blank\">RefreshView<\/a>. <\/p>\n<h2>Xamarin.Forms RefreshView<\/h2>\n<p>This control was introduced alongside CollectionView in Xamarin.Forms 4.3. Which gives you complete control over adding pull-to-refresh to any scrollable control. Let&#8217;s take a look at how easy it is to add pull-to-refresh to your app with RefreshView.<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2019\/11\/default-progress-circle-large.png\" alt=\"Screenshot of a RefreshView refreshing data, on iOS and Android\" width=\"840\" height=\"756\" class=\"aligncenter size-full wp-image-45804\" srcset=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2019\/11\/default-progress-circle-large.png 840w, https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2019\/11\/default-progress-circle-large-300x270.png 300w, https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2019\/11\/default-progress-circle-large-768x691.png 768w\" sizes=\"(max-width: 840px) 100vw, 840px\" \/><\/p>\n<p>To setup the code we will need a <strong>CollectionView<\/strong>:<\/p>\n<pre class=\"prettyprint\">&#60;CollectionView ItemsSource=\"{Binding Items}\"&#62;\r\n    &#60;CollectionView.ItemsLayout&#62;\r\n        &#60;LinearItemsLayout Orientation=\"Vertical\"\/&#62;\r\n    &#60;\/CollectionView.ItemsLayout&#62;\r\n    &#60;!-- Add ItemTemplate Here --&#62;\r\n&#60;\/CollectionView&#62;<\/pre>\n<p>In our ViewModel, or code behind, setup an <b>ICommand<\/b> to be executed when the refresh happens. This acts as the binding property called <b>IsRefreshing<\/b> that is a boolean to control when the <strong>RefreshView<\/strong> is active.<\/p>\n<pre class=\"prettyprint\"><code>\r\npublic ICommand RefreshCommand { get; }\r\npublic ItemsViewModel()\r\n{\r\n    Title = \"Browse\";\r\n    Items = new ObservableCollection<Item>();\r\n    RefreshCommand = new Command(ExecuteRefreshCommand);\r\n}\r\n\r\nbool isRefreshing;\r\npublic bool IsRefreshing\r\n{\r\n    get => isRefreshing;\r\n    set\r\n    {\r\n        isRefreshing = value;\r\n        OnPropertyChanged(nameof(IsRefreshing));\r\n    }\r\n}\r\n\r\nvoid ExecuteRefreshCommand()\r\n{\r\n    Items.Clear();\r\n    Items.Add(new Item { Text = \"Refreshed Data\", Description = \"Whoa!\" });\r\n    \r\n    \/\/ Stop refreshing\r\n    IsRefreshing = false;\r\n}\r\n<\/code><\/pre>\n<p>Now, all we need to do is wrap the <strong>CollectionView<\/strong> inside of a <strong>RefreshView<\/strong>. Then bind the <b>IsRefreshing<\/b> and <b>Command<\/b> properties to our properties in our ViewModel.<\/p>\n<pre class=\"prettyprint\">&#60;RefreshView IsRefreshing=\"{Binding IsRefreshing}\"\r\n             Command=\"{Binding RefreshCommand}\"&#62;\r\n    &#60;CollectionView ItemsSource=\"{Binding Items}\"&#62;\r\n        &#60;CollectionView.ItemsLayout&#62;\r\n            &#60;LinearItemsLayout Orientation=\"Vertical\"\/&#62;\r\n        &#60;\/CollectionView.ItemsLayout&#62;\r\n        &#60;!-- Add ItemTemplate Here --&#62;\r\n    &#60;\/CollectionView&#62;\r\n&#60;\/RefreshView&#62;<\/pre>\n<p>When running the application, we can now perform a pull-to-refresh on our CollectionView.<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2019\/11\/Pull-To-Refresh-1.gif\" alt=\"Xamarin.Forms RefreshView Android pull-to-refresh enabled.\" width=\"544\" height=\"236\" class=\"aligncenter size-full wp-image-45806\" \/><\/p>\n<h2>Controlling IsRefreshing<\/h2>\n<p>In the example above, the <b>RefreshView<\/b> will set <strong>IsRefreshing<\/strong> to &#8220;True&#8221; when the user pulls down. Then manually set the property to false to stop the <b>RefreshView<\/b> from showing. Use the <b>IsRefreshing<\/b> property as a gate to control if the data should be refreshed to include a <a href=\"https:\/\/docs.microsoft.com\/xamarin\/xamarin-forms\/app-fundamentals\/data-binding\/binding-mode?WT.mc_id=refreshview-xamarinblog-jamont\">binding mode<\/a>. This is so that only the code behind updates the property.<\/p>\n<pre class=\"prettyprint\">&#60;RefreshView IsRefreshing=\"{Binding IsRefreshing, Mode=OneWay}\"\r\n             Command=\"{Binding RefreshCommand}\"&#62;\r\n   &#60;!-- CollectionView --&#62;\r\n&#60;\/RefreshView&#62;<\/pre>\n<p>In the <b>ViewModel<\/b> we can now check against the property and set it both ways:<\/p>\n<pre class=\"lang:default decode:true\"><code>\r\nvoid ExecuteRefreshCommand()\r\n{\r\n    if (IsRefreshing)\r\n        return;\r\n\r\n    IsRefreshing = true;\r\n    Items.Clear();\r\n    Items.Add(new Item { Text = \"Refreshed Data\", Description = \"Whoa!\" });\r\n    IsRefreshing = false;\r\n}\r\n<\/code><\/pre>\n<h2>Learn More<\/h2>\n<p>Learn more about <a href=\"https:\/\/docs.microsoft.com\/xamarin\/xamarin-forms\/user-interface\/refreshview?WT.mc_id=refreshview-xamarinblog-jamont\" rel=\"noopener noreferrer\" target=\"_blank\">RefreshView<\/a> from our amazing documentation. With details such as setting the refresh color, disabling refresh, and even controlling the <a href=\"https:\/\/docs.microsoft.com\/xamarin\/xamarin-forms\/platform\/windows\/refreshview-pulldirection?WT.mc_id=refreshview-xamarinblog-jamont\" rel=\"noopener noreferrer\" target=\"_blank\">pull direction on UWP<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The RefreshView control was introduced alongside CollectionView in Xamarin.Forms 4.3 to give you complete control over adding pull-to-refresh to any scrollable control. <\/p>\n","protected":false},"author":544,"featured_media":45806,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2,1,367],"tags":[751,7485,7484,7483],"class_list":["post-45803","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","category-xamarin","category-xamarin-forms","tag-collectionview","tag-controls","tag-refreshview","tag-xamarin-forms-4-3"],"acf":[],"blog_post_summary":"<p>The RefreshView control was introduced alongside CollectionView in Xamarin.Forms 4.3 to give you complete control over adding pull-to-refresh to any scrollable control. <\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/45803","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=45803"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/45803\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media\/45806"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=45803"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=45803"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=45803"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}