Add Contact Features in 4 Lines of Code with Xamarin.Essentials

Avatar

James

Integrating contact functionality has been a common task that I have been asked to develop for mobile apps over the years. This has ranged from a full contacts directory to simple contact information inside an app. Most requested features include the ability to call the contact, send an sms or email, and navigate to a location. In the past, I would have to implement several native APIs myself or install several libraries that could provide the functionality. Today, with Xamarin.Essentials all of this functionality is available in a single library and can be implemented in just 4 lines of code! So, let’s build a contacts application with Xamarin.Essentials and Xamarin.Forms.

Animation of a contact application built with Xamarin

The Contact

The first thing to do is define the Contact class. This will have all of the information that is needed when using the Xamarin.Essentials APIs. This includes a phone number, email, and address.

Contact User Interface

There are a lot of ways to present contact information and for our example we can use a grid to display all of the information. We can use ImageButton for each item to enable users to take action. Each Commandis bound to a method that will be called from the ViewModel that I will outline next.

This will give us a very pretty user interface for our contact.

User interface for a contact card

Phone Dialer, Sms, Email, and Navigation!

Each Command that is bound in the user interface has a method that will be executed when the button is clicked. Each method uses a Xamarin.Essentials API to execute native functionality from a single line of code! Here is the full ViewModel from the sample app:

Note here that on each platform we are wrapping the method call in a Try/Catch block. This is important in case the feature is not available on the specific device. Beyond that, we are done! With just 4 tiny lines of code we have integrated tons of great native functionality into our contacts app!

Learn More

Be sure to browse through the full Xamarin.Essentials documentation for setup instructions and API information. Subscribe to the Xamarin Developers YouTube channel and browse through theXamarin.Essentials API of the Week series that feature short videos outlining a single API. Looking for more blogs on Xamarin.Essentials? Be sure to checkout the blog archive that is filled with great posts.

Find more awesome information on cross-platform development with Xamarin. If you are looking to add a backend to your app take a look at App Center Data & Azure Cosmos DB. Also, checkout the full GeoContacts Sample on GitHub.

Finally, you can can grab the source code for this blog on GitHub and try it today!

Avatar
James Montemagno

Principal Program Manager, Mobile Developers Tools

Follow James   

2 Comments
Avatar
Eric Brunner 2019-08-02 20:27:42
Awesome
Avatar
favour emmanuel 2019-08-04 12:20:11
Excellent