{"id":19910,"date":"2015-07-15T14:37:31","date_gmt":"2015-07-15T18:37:31","guid":{"rendered":"https:\/\/blog.xamarin.com\/?p=19910"},"modified":"2015-07-15T14:37:31","modified_gmt":"2015-07-15T18:37:31","slug":"share-even-more-code-with-plugins-for-xamarin","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/share-even-more-code-with-plugins-for-xamarin\/","title":{"rendered":"Share Even More Code with Plugins for Xamarin"},"content":{"rendered":"<p>\t\t\t\t<img decoding=\"async\" class=\" size-full wp-image-19911 alignright\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/plugin_icon_nuget.png\" alt=\"Plugins for Xamarin NuGet Icon\" width=\"128\" height=\"128\" \/>One of my top priorities for Moments, <a href=\"https:\/\/blog.xamarin.com\/build-your-own-snapchat-clone-with-xamarin.forms-and-azure\/\">a Snapchat clone built with Xamarin.Forms and Microsoft Azure<\/a>, was to maximize the amount of code shared between my target platforms. I was already achieving a significant amount of shared code with Xamarin.Forms, but I noticed that many things, such as checking for device connectivity or showing a dialog, had to be done on a per-platform basis. Plugins for Xamarin helped fill in those gaps and allowed me to share even more code.<\/p>\n<p>&nbsp;<\/p>\n<p>A <a href=\"https:\/\/github.com\/xamarin\/plugins\">Plugin for Xamarin<\/a> is a special kind of NuGet that adds cross-platform functionality or abstracts platform-specific functionality to a common API. These are extremely lightweight and completely cross-platform, so they can be accessed via a Portable Class Library (PCL) or Shared Project. Below, I\u2019ve shared a few of my favorite Plugins for Xamarin that I used to build Moments.<\/p>\n<h2>Connectivity<\/h2>\n<p>In today&#8217;s world, it&#8217;s vital that apps maintain connectivity when interacting with web services and other network-dependent logic. With the <a href=\"https:\/\/www.nuget.org\/packages\/Xam.Plugin.Connectivity\">Connectivity Plugin for Xamarin and Windows<\/a>, it\u2019s never been easier to check the connection status of a mobile device, gather connection types and bandwidths, and more.<\/p>\n<p>Testing network connectivity from shared code is literally as easy as writing one line of code:<\/p>\n<pre class=\"lang:csharp decode:true\">\nvar connected = CrossConnectivity.Current.IsConnected;\n<\/pre>\n<p>The Connectivity Plugin for Xamarin and Windows can also test the reachability of specific hosts using the <code>IsReachable<\/code> and <code>IsRemoteReachable<\/code> methods. Finally, you can subscribe to the ConnectivityChanged event to ensure that your app always knows if the device is connected to the internet:<\/p>\n<pre class=\"lang:csharp decode:true\">\nCrossConnectivity.Current.ConnectivityChanged += (sender, args) =&gt;\n{\n\/\/ Connectivity changed! Check args.IsConnected!\n};\n<\/pre>\n<h2>Settings<\/h2>\n<p>Another common use case among mobile apps is the ability to store user settings. Each platform exposes its own way of storing settings, such as\u00a0<code>NSUserDefaults<\/code> on iOS and <code>SharedPreferences<\/code> on Android. The <a href=\"https:\/\/github.com\/jamesmontemagno\/Xamarin.Plugins\/tree\/master\/Settings\">Settings Plugin for Xamarin and Windows<\/a> takes the ability to create and access settings cross-platform!<\/p>\n<p>Rather than dealing with the various platform-specific implementations, the Settings Plugin for Xamarin and Windows makes it easy to implement user settings, as seen below:<\/p>\n<pre class=\"lang:csharp decode:true\">\npublic class Settings\n{\npublic bool NotificationsEnabled\n{\nget { CrossSettings.Current.GetValueOrDefault&lt;bool&gt; (\"notificationsEnabled\"); }\nset { CrossSettings.Current.AddOrUpdateValue&lt;bool&gt; (\"notificationsEnabled\", value); }\n}\n}\n<\/pre>\n<p>Not only is the Settings Plugin for Xamarin and Windows great for managing settings from shared code, but it can also act as a simple cross-platform key-value store with support for many different data types, such as strings, integers, doubles, booleans, DateTimes, and more!<\/p>\n<h2>Dialogs<\/h2>\n<p>From alert popups to loading spinners, dialogs are a staple of mobile app development. The <a href=\"https:\/\/www.nuget.org\/packages\/Acr.UserDialogs\">User Dialogs Plugin for Xamarin and Windows<\/a> allows you to show almost any type of dialog you can think of from shared code, including loading spinners, alerts, confirmations, and error dialogs.<\/p>\n<p><strong>Loading Indicators<\/strong>\nFor performing potentially lengthy tasks, such as logging in a user, use the <code>ShowLoading<\/code> method:<\/p>\n<pre class=\"lang:csharp decode:true\">\nUserDialogs.Instance.ShowLoading (\"Loading\");\n<\/pre>\n<p>When the task ends, you can hide the dialog using the following method call:<\/p>\n<pre class=\"lang:csharp decode:true\">\nUserDialogs.Instance.HideLoading ();\n<\/pre>\n<p><strong>Success &amp; Failure<\/strong>\nIf a dialog needs to reflect\u00a0success or failure, such as the the result of a user log in, you can use the success and failure APIs available within the User Dialogs Plugin for Xamarin and Windows:<\/p>\n<pre class=\"lang:csharp decode:true\">\n\/\/ Success\nUserDialogs.Instance.ShowSuccess (\"Success\");\n\/\/ Failure\nUserDialogs.Instance.ShowError (\"Error\");\n<\/pre>\n<h2>Download a Plugin for Xamarin Today<\/h2>\n<p>All of the Plugins for Xamarin listed above helped me significantly increase productivity by achieving over 87% shared code in Moments. Check out <a href=\"https:\/\/github.com\/xamarin\/plugins\">Plugins for Xamarin on GitHub<\/a> to find a plugin for your next project, or find out how easy it is to <a href=\"https:\/\/github.com\/xamarin\/plugins#create-a-plugin-for-xamarin\">build your own Plugin for Xamarin<\/a>.\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of my top priorities for Moments, a Snapchat clone built with Xamarin.Forms and Microsoft Azure, was to maximize the amount of code shared between my target platforms. I was already achieving a significant amount of shared code with Xamarin.Forms, but I noticed that many things, such as checking for device connectivity or showing a [&hellip;]<\/p>\n","protected":false},"author":546,"featured_media":20887,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[4,16],"class_list":["post-19910","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","tag-xamarin-platform","tag-xamarin-forms"],"acf":[],"blog_post_summary":"<p>One of my top priorities for Moments, a Snapchat clone built with Xamarin.Forms and Microsoft Azure, was to maximize the amount of code shared between my target platforms. I was already achieving a significant amount of shared code with Xamarin.Forms, but I noticed that many things, such as checking for device connectivity or showing a [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/19910","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\/546"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=19910"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/19910\/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=19910"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=19910"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=19910"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}