Recently, obviously last year (in 2014) Xamarin’s James Montemagno posted a blog post titled “Holiday Contest: Create a Plugin for Xamarin” and I also managed to develop a very small plugin for Xamarin.Forms with the help of this video. My plugin can be downloaded from here and the source code is here. The awesome thing in this plugin is, despite its functionality and its validating phone numbers, man, I completed the development of this project with in roughly 30 mins. Wow.. that’s amazing isn’t it. boom..
Another very very interesting thing about this plugin is, I didn’t test it before publish it to Nuget and opensource the code in Github.
Coded and shipped something without testing it.. boom.. #previoustweet
— िश्ाम मुहम्मत (@Isham_M_Iqbal) January 2, 2015
Even I tweeted about it. Seriously I didn’t know how to call a plugin / Nuget package in Xamarin.Forms project. My initial thought was its just like importing and calling a Nuget package in Visual Studio but I didn’t succeed it. After tooooooooooooo much of attempts just I have found the way. So lets see how to do it.
Step 1 – Import the Nuget package. Remember you have to import to all the projects of the solution. Ideally you will have 4 folders of projects, one is the common folder for the app where we have to make UI and implement logic. other three folders are for WP, Android and iOS. Please find some help from here to know how to add packages.
Step 2 – Open the App.cs file in the main project folder and make the UI with logic. In my case I am creating an app with a single button,
Click event of the button will use the plugin / Nuget package. So my App.cs code comes as
Lets have a detailed look on this code.
See the line 3, which is
using PhoneCall.Forms.Plugin.Abstractions; this is the place where I call the plugin. While creating a plugin for Xamarin.Forms, we create it with 4 folders one is called “Abstractions” where we implement the interfaces for the plugin, so later we can use these interfaces with other 3 folders (literately projects) which are for WP, Android & iOS (If you can’t get this, please refer the video on creating plugins, I linked that above this page)
After that in the line 11 I have created a button and in the line 12 I have coded for the
Click event of it. On the
Click event I am calling a method
QCall which is implemented from line 26. In line 28 I have created a variable called
notifier which calls the
IPhoneCall interface in my plugin. To make this done, I use the help of
DependencyService class which is a built-in and let shared code to easily resolve Interfaces to platform-specific implementations, allowing you to access features of the iOS, Android and Windows Phone SDKs from your PCL or Shared Project. Then I call a method
MakeQuickCall in line 29 which is in the interface to get my job done.
Here in the App.cs class has a static method that returns a
GetMainPage(). That method is the place where we create UI and since that method should return a
Page we built
ContentPage that has
StackLayout with the code of lines 14-23 and call the
Button we have already created in line 11. In line 19, 20 you can see I have set the
Children property of the
Step 3 – Now open the Android project of the solution and open MainActivity.cs. There just use
using PhoneCall.Forms.Plugin.Droid; and initialize this with
PhoneCallImplementation.Init ();. Thats all. My MainActivity.cs is
DO the same for the WP and iOS projects as well.
Step 4 – Now click on Run -> Upload to Device.
Note : Set correct target API level for Android.
Thats all peeps. Happy coding. 🙂