{"id":3996,"date":"2022-10-11T20:51:51","date_gmt":"2022-10-11T20:51:51","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=3996"},"modified":"2022-10-11T20:51:51","modified_gmt":"2022-10-11T20:51:51","slug":"device-specific-code-in-xamarin","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/device-specific-code-in-xamarin\/","title":{"rendered":"Device specific code in Xamarin"},"content":{"rendered":"<p><em>Note: This post was written a while back but sat in draft. I&#8217;ve published this now, but I&#8217;m not sure it&#8217;s relevant to the latest versions etc. so please bear this in mind.<\/em><\/p>\n<p>Occasionally we might need to handle text or UI controls slightly differently on different devices. <\/p>\n<p>In a shared code project we&#8217;d probably look to write code using conditional compilation, but this wouldn&#8217;t work for PCL code. Also conditional compilation doesn&#8217;t work in XAML. So whilst conditional compilation is still a valid technique for shared code projects an alternative is runtime branching based upon the device the code is running on (in other words <em>if<\/em> or <em>switch<\/em> code). Xamarin already supplies us with a simple mechanism to achieve this using the OnPlatform method on the Device class.<\/p>\n<p><string>PCL\/Runtime device specific code<\/strong><\/p>\n<p>So, in code we can use the Device class like this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class MyPage : ContentPage\r\n{\r\n   public MyPage()\r\n   {\r\n      Padding = Device.OnPlatform(\r\n          new Thickness(0,20,0,0), \r\n          new Thickness(0),\r\n          new Thickness(0));\r\n   }\r\n}\r\n<\/pre>\n<p>Or from XAML we can use the OnPlatform element, for example<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;ContentPage&gt;\r\n   &lt;ContentPage.Padding&gt;\r\n      &lt;OnPlatform x:TypeArguments=&quot;Thickness&quot; iOS=&quot;0,20,0,0&quot; \/&gt;\r\n   &lt;\/ContentPage.Padding&gt;\r\n&lt;\/ContentPage&gt;\r\n<\/pre>\n<p><strong>Conditional Compilation directives<\/strong><\/p>\n<p>Just to complete the picture, let&#8217;s look at what compiler directives exist &#8211; these are not exclusive to shared code projects but obviously are available for any use<\/p>\n<ul>\n<li><strong>__IOS__<\/strong> for iOS code<\/li>\n<li><strong>__ANDROID__<\/strong> for Android code<\/li>\n<li><strong>__ANDROID_nn__<\/strong> for Android code, where nn is the Andoird API level supported<\/li>\n<li><strong>WINDOWS_UWP<\/strong> for Universal Windows Platform code<\/li>\n<li><strong>WINDOWS_APP<\/strong> for Windows 8.1 code<\/li>\n<li><strong>WINDOWS_PHONE_APP<\/strong> for Windows Phone 8.1 code<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Note: This post was written a while back but sat in draft. I&#8217;ve published this now, but I&#8217;m not sure it&#8217;s relevant to the latest versions etc. so please bear this in mind. Occasionally we might need to handle text or UI controls slightly differently on different devices. In a shared code project we&#8217;d probably [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[92,122],"tags":[],"class_list":["post-3996","post","type-post","status-publish","format-standard","hentry","category-xamarin","category-xamarin-forms"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/3996","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=3996"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/3996\/revisions"}],"predecessor-version":[{"id":9588,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/3996\/revisions\/9588"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=3996"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=3996"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=3996"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}