Custom Switch in Xamarin Forms

Xamarin Forms is a wonderful ecosystem to develop cross platform mobile applications.

Like any platforms, it has limitations too, but yeah there are work arounds. At times you simply can contribute to the awesome “Open Source Xamarin Forms”

Today we will be discussing a small problem I faced in one of our projects, where Xamarin Forms Switch control fires “Toggled” event both when changed by user or programmatically. This was not ideal for our requirements.

So I planned to create a Custom Switch over the Original Switch.

Warning: This work around might not be the best way to do this.

To get started create a Class File, in my case “CustomSwitch”, inherit it from Switch class of Xamarin Forms.

We will create a IsCustomToggled property (which is Bindable Property), to use instead of IsToggled and CustomToggled instead of Toggled event.

Once we reach the constructor, we subscribe to the original Toggled event with our own function.

Also whenever IsCustomToggled is changed in code / program, we will Unsubscribe before setting original IsToggled, and then subscribe back to the event.

The CustomToggled event has CustomToggledEventArgs which contain the new value for switch and an bool “isUser” for indicating whether the action was result of user interaction or code change.

 

Once the Custom Switch is ready, the usage at any place in project is straight forward,

from Xaml

 

Samples and CustomSwitch available at GitHub

Hope this helps someone with same issue, in case of any queries do drop a mail to muhaymin at fantacode com where I am available Xamarin Consulting  or use the comment section below.

 

 

Upgrading PCL Xamarin Forms Project to .NET Standard Like a Boss

Sorry readers, running own company possess great challenges, so didn’t do justice to my blog, and when I get free time I use it for sleeping :D.

Recently I had to port our existing Xamarin.Forms Project from PCL to .NET Standard.

It’s not a simple Xamarin Forms project, for upgrading simple projects, we have various blog post covering those. I am taking about Xamarin.Forms project of very large scale (around 100k Lines of Code, more than 500 Views &ViewModels,  multiple custom renderes and around 90+ Nuget Libraries).

So all the libraries we used didn’t have support for .NET Standard. So we have multiple requirements here

1. Upgrade our PCL without affecting Git History

2. Keep using Libraries which don’t have .NET Standard support yet.

3. Make sure that your Resx file works as expected. (This I am still experimenting, will be sharing findings).

So the first step you have to make sure is take a copy of your working project in current state and keep it somewhere safe.

If you use git / version control, create a branch and let’s start tinkering with the project. Steps are pretty straight forward.

1. Unload your PCL project (right click -> unload), and start editing it (right -> click edit), VS Mac users, just use Visual Studio code, it’s straight forward.

2. You have to delete everything in the .csproj and insert


So in .NETStandard approach, there is no packages.config. References included in .csproj So you have to move all the stuff in packages.config to your .csproj.

In my case manually changing the to the new format was time consuming for 90+ Nuget Reference I had, so I wrote a simple C# Console application to make my life easier, sharing it here 😀

3. Delete AssemblyInfo.cs, these informations will be in .csproj and packages.config (also in csproj via PackageReference)

4. Basically Reload the project, you are ready to go. But there is catch if your project contains libraries which don’t have .NetStandard.

So we will need to add FallBack criteria for Nuget restores.

Under Property Group add this line (based on you requirements of which previous PCL profile do you want restores to based on)

Other issues you may face is that if you have any references to Microsoft.Bcl.Build nugets in any of your projects (Platform Specific and the new .NET Standard), remove those references.

To avoid possible issues, it is always better to remove the existing PCL reference from Platform Specific Projects, and adding it again.

Points to take care for avoiding potential crashes.

  • If  you are were using Embedded Resources in your PCL project, you have to mark the Build actions again. Otherwise those resources wont load.
  • If you are using Resx for localization, it’s always best to remove the current file (ofcourse take a backup), add one again with same name and replace contents.

Before Building, clean all bin/obj folders, Rebuild the project with fingers crossed :D.

If you face any issue please don’t hesitate to contact me or I can help you with this, Just mail to muhaymin at fantacode dot com

Do share your experiences.

References:

How to Convert PCL Library to NET Standard and Keep Git History – James Montemagno