

Xamarin forms Tabbed page – UWP with images
source link: https://depblog.weblogs.us/2017/07/12/xamarin-forms-tabbed-page-uwp-with-images/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Xamarin forms Tabbed page – UWP with images
When developing cross platform apps with Xamarin forms, you’ll notice that your apps will look and feel right at home on each OS. This because the nice people of Xamarin render each Xamarin forms control as a native control with the needed control properties all filled in.
You can still tweak some of these properties when they are leveraged through the Xamarin forms control abstraction or use custom renderers and effects to get down to the native control layer.
Most of the time this is all working great, but when you try to take UWP into account, you’ll notice not everything works straight out of the box.
Hence this blog post, showing you how you can hack the current TabbedPage implementation of Xamarin forms for UWP to enable pivot headers with images!
If you’ll use following XAML in Xamarin forms, you’ll get a nice Pivot control in UWP
But when you run the app, you’ll notice that on UWP nothing is happening with the Icon property of the NavigationPage
While on iOS you’ll get something like this
So what gives? Well, the current Xamarin forms tabbed page implementation will not take into account this Icon property and ignore it during rendering.
Reading the guidelines from Microsoft, it’s recommended to actually do use icons for pivot headers if possible, read on it here https://docs.microsoft.com/en-us/windows/uwp/controls-and-patterns/tabs-pivot.
The result should be something like this
Now let me show you a hack / workaround to get the same result on UWP. I stress hack, because of the current TabbedPage implementation in Xamarin forms there is no direct way to handle this.
Searching for a solution, I needed to open up the source code of Xamarin forms, fortunately it’s open source
If you go to the TabbedPageRenderer that is used for UWP, you’ll see that a specific XAML style is used for the Pivot control. This style is called TabbedPageStyle!
Setting the style is done here https://github.com/xamarin/Xamarin.Forms/blob/4e83b8ee0fa9274a1183ca41117eb91df825e341/Xamarin.Forms.Platform.UAP/TabbedPageRenderer.cs#L148
And the actual Style can be found here https://github.com/xamarin/Xamarin.Forms/blob/8c5fd096945301a2db0d85baf77ce46812a8d89f/Xamarin.Forms.Platform.UAP/TabbedPageStyle.xaml
To create a working solution, we need to change the HeaderTemplate that is specified inside that TabbedPageStyle. Normally we would just drop in a new Style with the same name inside our UWP App.Xaml file to override the one from Xamarin forms. But due to how Xamarin forms is initialised during startup this won’t work. So a small interception has to be made to get this going.
Add a new Styles.Xaml file in your UWP project and add following Style element to it
This will enable the use of the Icon property that we have set in our Xamarin forms xaml for each tab of the tabbedPage. Only thing left is swapping out the current HeaderTemplate and using ours.
So be sure to tell UWP we have this Style in our ResourceDictionary by adding it in the App.Xaml
After that, open up the App.Xaml.cs file and look for the Xamarin.Forms.Forms.Init(e); line.
Below it add an extra line of code to do the actual Template swapping
Like I said this is a hack… but it works perfectly instead of using the default Xamarin forms TabbedPageStyle inside the Pivot header template we’ll now be using our own TabbedPageStyle2.
Only one small detail remains, you’ll notice I added a converter inside the Style. This was needed because the Icon property on the NavigationPage of Xamarin forms maps to a FileImageSource but that is not processable straight away as a source value for the Image control inside UWP.
The converter will get the File property of the FileImageSource and return that as a valid source. With this in place you’ll get the correct image shown!
Now if you have done everything right, you’ll get the following result, a Pivot control for a TabbedPage with images on UWP
As always a complete demo solution can be found on my Github here…
Recommend
-
106
-
67
Change Randomly A View’s Color Hi everyone, here is the third part of the Xamarin Forms Tips series. Today, we will look at how to change a...
-
20
Navigate from CollectionView to Details Page in Xamarin.FormsNavigate from CollectionView to Details Page in Xamarin.Forms - YouTube Back Skip navigation ...
-
15
Windows 下的高 DPI 应用开发(UWP / WPF / Windows Forms / Win32) 发布于 2018-10-18 10:06 更新于 2021-01-04 12:33 ...
-
21
Cross Platform Images in Xamarin.Forms517 views•Jan 20, 2021580ShareSave
-
14
When developing mobile applications, having a good UI/UX is one of the most important things. One way we can help with our UX by having smooth transitions between pages. Xamarin Forms uses the default standard transition navigation:
-
14
#XamarinForms #FontIcons #FontAwesome
-
7
Xamarin Forms Circular Activity Indicator For UWP ...
-
12
#Share #Essentials #ImageSharingShare Im...
-
5
Xamarin UWP: Use multilanguage resource files properly December 17, 2021 by banditothIIf you are experiencing the oddity that the UWP version of your application can...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK