{"id":6357,"date":"2018-09-27T20:54:28","date_gmt":"2018-09-27T20:54:28","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=6357"},"modified":"2018-09-27T20:54:28","modified_gmt":"2018-09-27T20:54:28","slug":"onidiom-and-onplatform","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/onidiom-and-onplatform\/","title":{"rendered":"OnIdiom and OnPlatform"},"content":{"rendered":"<p>When developing a Xamarin Forms cross platform application, we&#8217;ll have a shared library where (hopefully) the bulk of our code will go, be it C# or XAML.<\/p>\n<p>However we may still need to make platform specific changes to our XAML, whether it&#8217;s images, margins, padding etc. Along with the possible differences per platform we also have the added complexity of the idiom, i.e. desktop, phone or tablet (for example) which might require differences, for example maybe we display more controls on a tablet compared to the phone.<\/p>\n<p><strong>OnPlatform<\/strong><\/p>\n<p>We&#8217;re still likely to need to handle different platforms within this shared code, for example in <a href=\"http:\/\/putridparrot.com\/blog\/xamarin-forms-tabbedpage\/\" rel=\"noopener\" target=\"_blank\">Xamarin Forms TabbedPage<\/a> we used OnPlatform like this<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;ContentPage.Icon&gt;\r\n   &lt;OnPlatform x:TypeArguments=&quot;FileImageSource&quot;&gt;\r\n      &lt;On Platform=&quot;iOS&quot; Value=&quot;history.png&quot;\/&gt;\r\n   &lt;\/OnPlatform&gt;\r\n&lt;\/ContentPage.Icon&gt;\r\n<\/pre>\n<p>The above simply declares that on iOS the ContentPage Icon is used and is located in the file <em>history.png<\/em>. <\/p>\n<p>We can handle more than just images, obviously we might look to handle different margins, fonts etc. depending upon the platform being used.<\/p>\n<p>We can declare values for multiple platforms using comma separated values in the Platform attribute, for example<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;OnPlatform x:TypeArguments=&quot;FileImageSource&quot;&gt;\r\n   &lt;On Platform=&quot;iOS, Android&quot; Value=&quot;history.png&quot;\/&gt;\r\n   &lt;On Platform=&quot;UWP&quot; Value=&quot;uwphistory.png&quot;\/&gt;\r\n&lt;\/OnPlatform&gt;\r\n<\/pre>\n<p>OnPlatform&#8217;s Platform attribute currently supports iOS, Android, UWP, macOS, GTK, Tizen and WPF.<\/p>\n<p>Ofcourse we can simply use Device.RuntimePatform in code-behind to find out what platform the application is running on if preferred.<\/p>\n<p><strong>OnIdiom<\/strong><\/p>\n<p>Working in much the same was as OnPlatform. OnIdiom is used for handling different XAML or code based upon the current device is classed as a Unsupported, Phone, Tablet, Desktop, TV or Watch.<\/p>\n<p>In XAML we might change the StackLayout orientation, for example<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;StackLayout.Orientation&gt;\r\n   &lt;OnIdiom x:TypeArguments=&quot;StackOrientation&quot;&gt;\r\n      &lt;OnIdiom.Phone&gt;Vertical&lt;\/OnIdiom.Phone&gt;\r\n      &lt;OnIdiom.Tablet&gt;Horizontal&lt;\/OnIdiom.Tablet&gt;\r\n   &lt;\/OnIdiom&gt;\r\n&lt;\/StackLayout.Orientation&gt;\r\n<\/pre>\n<p>We can use the Idiom within code-behind by checking the state of Device.Idiom.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When developing a Xamarin Forms cross platform application, we&#8217;ll have a shared library where (hopefully) the bulk of our code will go, be it C# or XAML. However we may still need to make platform specific changes to our XAML, whether it&#8217;s images, margins, padding etc. Along with the possible differences per platform we also [&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":[122],"tags":[],"class_list":["post-6357","post","type-post","status-publish","format-standard","hentry","category-xamarin-forms"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6357","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=6357"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6357\/revisions"}],"predecessor-version":[{"id":6518,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6357\/revisions\/6518"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=6357"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=6357"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=6357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}