{"id":46344,"date":"2020-02-06T09:08:24","date_gmt":"2020-02-06T17:08:24","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/xamarin\/?p=46344"},"modified":"2020-02-06T09:08:24","modified_gmt":"2020-02-06T17:08:24","slug":"tabs-and-triggers","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/tabs-and-triggers\/","title":{"rendered":"Toggling Tabs with Triggers"},"content":{"rendered":"<p><a href=\"https:\/\/docs.microsoft.com\/en-us\/xamarin\/xamarin-forms\/\">Xamarin.Forms<\/a> makes setting Tabs Icons super easy! All there is to do is set the Icon property to the path of an image and you are done.<\/p>\n<pre class=\"lang:default decode:true prettyprint\">&lt;Tab Title=\"Browse\" Icon=\"tab_feed.png\"&gt;<\/pre>\n<p>&nbsp;<\/p>\n<h2>Tabs and Fonts<\/h2>\n<p><span data-contrast=\"auto\">Better still<\/span><span data-contrast=\"auto\">,<\/span><span data-contrast=\"auto\"> you can use <\/span><a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/xamarin.forms.fontimagesource?view=xamarin-forms\"><b><span data-contrast=\"none\">FontImageSource<\/span><\/b><\/a> <span data-contrast=\"auto\">to have scalable vector graphics used for your icons<\/span><span data-contrast=\"auto\">.<\/span> <span data-contrast=\"auto\">As well as<\/span> <span data-contrast=\"auto\">avoid all <\/span><span data-contrast=\"auto\">that hassle<\/span><span data-contrast=\"auto\"> with resolution dependent images.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">A great source of fonts that can be used for <\/span><span data-contrast=\"auto\">i<\/span><span data-contrast=\"auto\">cons can be found at <\/span><a href=\"https:\/\/fontawesome.com\/\"><span data-contrast=\"none\">FontAwesome<\/span><\/a><span data-contrast=\"auto\"> or <\/span><a href=\"https:\/\/glyphsearch.com\/\"><span data-contrast=\"none\">GlyphSearch<\/span><\/a><span data-contrast=\"auto\">.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">\u00a0<\/span><span data-contrast=\"auto\">Once you&#8217;ve downloaded you chosen .<\/span><span data-contrast=\"auto\">tff<\/span><span data-contrast=\"auto\"> file to use the custom font you will need to embed a copy as local resource\/asset in the native projects to make them accessible from the <\/span><span data-contrast=\"auto\">Xamarin.Forms<\/span><span data-contrast=\"auto\"> shared code.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">\u00a0<\/span><span data-contrast=\"auto\">iOS and Android have different ways of reference embedded resources. In order to abstract out <\/span><span data-contrast=\"auto\">these difference<\/span><span data-contrast=\"auto\">s<\/span><span data-contrast=\"auto\"> and access the font resources consistently from XAML<\/span><span data-contrast=\"auto\">,<\/span><span data-contrast=\"auto\"> we use the magic of <\/span><b><span data-contrast=\"auto\">OnPlatform<\/span><\/b><span data-contrast=\"auto\"> in the Applications resource dictionary as shown below.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<pre class=\"lang:default decode:true\">&lt;Application.Resources&gt;\u00a0\r\n\u00a0\u00a0\u00a0&lt;ResourceDictionary&gt;\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;OnPlatform x:TypeArguments=\"x:String\" x:Key=\"FontAwesomeSolid\"&gt;\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;On Platform=\"iOS\" Value=\"FontAwesome5Free-Solid\"\/&gt;\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;On Platform=\"Android\" Value=\"fa-solid-900.ttf#Solid\"\/&gt;\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;\/OnPlatform&gt;\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;OnPlatform x:TypeArguments=\"x:String\" x:Key=\"FontAwesomeRegular\"&gt;\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;On Platform=\"iOS\" Value=\"FontAwesome5Free-Regular\"\/&gt;\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;On Platform=\"Android\" Value=\"fa-regular-400.ttf#Regular\"\/&gt;\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&lt;\/OnPlatform&gt;\u00a0\r\n\u00a0\u00a0\u00a0&lt;\/ResourceDictionary&gt;\u00a0\r\n&lt;\/Application.Resources&gt;<\/pre>\n<p><span data-contrast=\"auto\">\u00a0<\/span><span data-contrast=\"auto\">Note, this process can be automated using a <\/span><span data-contrast=\"auto\">third<\/span><span data-contrast=\"auto\">&#8211;<\/span><span data-contrast=\"auto\">party<\/span><span data-contrast=\"auto\"> tool such as <\/span><a href=\"https:\/\/www.mfractor.com\/\"><span data-contrast=\"none\">MFractor<\/span><\/a><span data-contrast=\"auto\">.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<ul>\n<li>Check out this <a href=\"https:\/\/www.mfractor.com\/blogs\/news\/using-custom-fonts-in-xamarin-forms\">MFractor blog using custom fonts in Xamarin.Forms<\/a><\/li>\n<\/ul>\n<p><span data-contrast=\"auto\">Below is an example where a Font has been set as a StaticResource on the Tab Icon using FontImageSource.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<pre class=\"lang:xhtml decode:true\">&lt;Tab Title=\"Browse\"&gt;\u00a0\r\n\u00a0\u00a0\u00a0 &lt;Tab.Icon&gt;\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;FontImageSource FontFamily=\"{StaticResource FontAwesomeRegular}\" Glyph=\"&amp;#xf004;\"\/&gt;\u00a0\r\n\u00a0\u00a0\u00a0 &lt;\/Tab.Icon&gt;\u00a0\r\n&lt;\/Tab&gt;<\/pre>\n<p><center><img decoding=\"async\" class=\"alignnone wp-image-46357\" src=\"http:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/02\/2020-02-06_03-36-42-PM-2.png\" alt=\"FontAwesome Tabs\" width=\"500\" height=\"145\" srcset=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/02\/2020-02-06_03-36-42-PM-2.png 809w, https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/02\/2020-02-06_03-36-42-PM-2-300x87.png 300w, https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/02\/2020-02-06_03-36-42-PM-2-768x223.png 768w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/center><\/p>\n<p><span data-contrast=\"auto\">However, <\/span><span data-contrast=\"auto\">what if you want <\/span><span data-contrast=\"auto\">the ability<\/span><span data-contrast=\"auto\"> to switch between icons based on the selected tab? <\/span><span data-contrast=\"auto\">One option is to write some logic in code which changes the icons on an event. <\/span><span data-contrast=\"auto\">W<\/span><span data-contrast=\"auto\">ith <\/span><a href=\"https:\/\/docs.microsoft.com\/en-us\/xamarin\/xamarin-forms\/app-fundamentals\/triggers#applying-a-trigger-using-a-style\"><span data-contrast=\"none\">StyleTriggers<\/span><\/a><span data-contrast=\"auto\"> this can be done entirely in XAML!<\/span><\/p>\n<h2 aria-level=\"1\">Triggers<\/h2>\n<p><span data-contrast=\"auto\">Triggers allow you to set a property or group of properties based on the value of another property.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">The simplest and most common form of trigger is the property trigger.\u00a0 It is supported by any Forms Element that has the <\/span><b><span data-contrast=\"auto\">Triggers <\/span><\/b><span data-contrast=\"auto\">element. As the name suggests<\/span><span data-contrast=\"auto\">,<\/span><span data-contrast=\"auto\"> this holds a collection of Triggers that will be applied to the control.\u00a0<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Below<\/span><span data-contrast=\"auto\">,<\/span><span data-contrast=\"auto\"> we have an example property trigger that changes the background of an <\/span><b><span data-contrast=\"auto\">Entry <\/span><\/b><span data-contrast=\"auto\">when it has focus<\/span><span data-contrast=\"auto\">:<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<pre class=\"lang:default decode:true\">&lt;Entry Placeholder=\"Username\"&gt;\u00a0\r\n\u00a0\u00a0\u00a0 &lt;Entry.Triggers&gt;\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;Trigger TargetType=\"Entry\" Property=\"IsFocused\" Value=\"True\"&gt;\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;Setter Property=\"BackgroundColor\" Value=\"Yellow\" \/&gt;\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;\/Trigger&gt;\u00a0\r\n\u00a0\u00a0\u00a0 &lt;\/Entry.Triggers&gt;\u00a0\r\n&lt;\/Entry&gt; <span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/pre>\n<h3><span data-contrast=\"none\">Style Triggers<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/h3>\n<p><span data-contrast=\"auto\">Some controls do not have a Triggers property<\/span><span data-contrast=\"auto\"> and so<\/span><span data-contrast=\"auto\">,<\/span><span data-contrast=\"auto\"> do not <\/span><span data-contrast=\"auto\">directly support triggers. All is not lost<\/span><span data-contrast=\"auto\">,<\/span><span data-contrast=\"auto\"> however<\/span><span data-contrast=\"auto\">,<\/span><span data-contrast=\"auto\"> as you can simply have a trigger within a style. Style triggers are a powerful way of dynamically changing the visual appearance of a control based on the value of a <\/span><span data-contrast=\"auto\">bindable<\/span><span data-contrast=\"auto\"> property. They are very similar to property triggers but <\/span><span data-contrast=\"auto\">can be applied to any control that has a Style property, such as a Tab.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Below<\/span><span data-contrast=\"auto\">,<\/span><span data-contrast=\"auto\"> we have an example where we can use the checked state of a Tab to determine which Glyph to use<\/span><span data-contrast=\"auto\">:<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<pre class=\"lang:default decode:true\">&lt;Style TargetType=\"Tab\" x:Key=\"BrowseTab\"&gt;\u00a0\r\n\u00a0\u00a0\u00a0 &lt;Style.Triggers&gt;\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;Trigger TargetType=\"Tab\"\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Property=\"IsChecked\" Value=\"False\"&gt;\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;Setter Property=\"Icon\" &gt;\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;Setter.Value&gt;\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;FontImageSource FontFamily=\"{StaticResource FontAwesomeRegular}\" Glyph=\"&amp;#xf24d;\"\/&gt;\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;\/Setter.Value&gt;\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;\/Setter&gt;\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;\/Trigger&gt;\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;Trigger TargetType=\"Tab\"\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Property=\"IsChecked\" Value=\"True\"&gt;\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;Setter Property=\"Icon\" &gt;\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;Setter.Value&gt;\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;FontImageSource FontFamily=\"{StaticResource FontAwesomeSolid}\" Glyph=\"&amp;#xf24d;\"\/&gt;\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;\/Setter.Value&gt;\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;\/Setter&gt;\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;\/Trigger&gt;\u00a0\r\n\u00a0\u00a0\u00a0 &lt;\/Style.Triggers&gt;\u00a0\r\n&lt;\/Style&gt;<\/pre>\n<p><center><img decoding=\"async\" src=\"http:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/02\/Screen-Recording-2020-01-13-at-16.38.45-1.gif\" alt=\"Animation showing Tabs icons toggling\" width=\"700\" height=\"227\" class=\"aligncenter size-full wp-image-46360\" \/><\/center><\/p>\n<h2 aria-level=\"1\">Learn More About Tabs and Triggers<\/h2>\n<p><span data-contrast=\"auto\">Learn more about <\/span><a href=\"https:\/\/docs.microsoft.com\/en-us\/xamarin\/xamarin-forms\/app-fundamentals\/shell\/tabs\"><span data-contrast=\"none\">Tabs<\/span><\/a><span data-contrast=\"auto\"> and <\/span><a href=\"https:\/\/docs.microsoft.com\/en-us\/xamarin\/xamarin-forms\/app-fundamentals\/triggers\"><span data-contrast=\"none\">Triggers<\/span><\/a><span data-contrast=\"auto\"> and from our amazing documentation.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p aria-level=\"1\"><span data-contrast=\"none\">You can also check out the sample app on <\/span><a href=\"https:\/\/github.com\/xamcat\/xam-twitch\/tree\/master\/XamTwitch\/XamTwitch\"><span data-contrast=\"none\">GitHub<\/span><\/a><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559738&quot;:240,&quot;335559739&quot;:0,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">For <\/span><span data-contrast=\"auto\">video walkthrough <\/span><span data-contrast=\"auto\">that demonstrates <\/span><span data-contrast=\"auto\">this technique in action<\/span><span data-contrast=\"auto\">,<\/span><span data-contrast=\"auto\"> check out <\/span><a href=\"https:\/\/youtu.be\/h3j4s9YnxPk?t=1382\"><span data-contrast=\"none\">Live Stream: Zimmer EP02 &#8211; Building a mobile app with Ben Buttigieg<\/span><\/a><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><center><iframe width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/h3j4s9YnxPk\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/center><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn more about how Xamarin.Forms makes setting Tab Icons super easy and how using Triggers allow you to set a property or group of properties based on the value of another property. <\/p>\n","protected":false},"author":18053,"featured_media":46360,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[367,7745],"tags":[1063,7950,16],"class_list":["post-46344","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-xamarin-forms","category-xaml","tag-tabs","tag-triggers","tag-xamarin-forms"],"acf":[],"blog_post_summary":"<p>Learn more about how Xamarin.Forms makes setting Tab Icons super easy and how using Triggers allow you to set a property or group of properties based on the value of another property. <\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/46344","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\/18053"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=46344"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/46344\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media\/46360"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=46344"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=46344"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=46344"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}