Xamarin.Forms Features That Will Make Your Life Easier

Preview:

Citation preview

Xamarin. Forms Features That Will Make Your Life Easier: DependencyService & MessagingCenter

Andres Castro

My Background

Andres Castro• ASP.NET MVC

• ASP.NET WEBFORMS

• Web Services

• Xamarin

Who Is Bluetube

• Established in 1999

• Award-Winning Solutions

• Experts with Technology and Usability

• Enterprise Corporations & Non-profit Clients

• Premier Xamarin Consulting Partner

• Xamarin Test Cloud Consulting Partners

Bluetube designs and

builds enterprise mobile

apps & responsive

websites.

Dependency Service

What is it?

The Xamarin.Forms Dependency Service is a simple

dependency resolver that simplifies how platform specific

features are implemented.

Basically, it allows you to register classes which will then be

pre-initialized for you upon request. It removes the need for

you to “new” every time you need to use that class.

Dependency Service

How does it work?

There are 3 steps necessary to successfully using the

dependency service.

1. Create an interface that will define how each platform

should be implemented.

2. Register each platform-specific implementation of the class

above.

3. Use the implemented interface using

DependencyService.Get<T>

Dependency Service

Creating the Interface

As an example, I will be creating a interface that allows you to

open the Phone Dialer from within your app.

Dependency Service

Registering the Android Implementation

Dependency Service

Registering the iOS Implementation

Dependency Service

Using the Code

Messaging Center

What is it?

The Xamarin.Forms Messaging Center allows you to send and

receive “Messages” without knowing who they are going to or

who they are coming from.

It is very similar to using Event Handlers, the only difference is

that you are hooking into the Messaging Center.

Messaging Center

How does it work?

There are three parts to using the Messaging Center

1. Subscribe – Subscribes the messaging center to listen to a

certain message. You can then perform an action based on

that message.

2. Send – Sends a message through the messaging center so

that any subscribed classes receive the message.

3. Unsubscribe – Stops listening to any messages that might

come through.

Messaging Center

Example

Lets extend the existing Phone Dialer Example to navigate us

back, which will take us to a new page after the call code has

executed.

Messaging CenterLet’s first create a message class that will help to wrap data

we want to send.

Messaging Center

Subscribing to the Message

Messaging Center

Sending the Message

Recap

– Covered what the Dependency Service is and how to

implement it.

– Saw how to call platform specific code using the

Dependency Service.

– Covered what the Messaging Center is and how to

implement it.

– Used the Messaging Center to implement navigation inside

the app.

Dependency Service & Messaging Center

References– http://developer.xamarin.com/recipes/android/fundam

entals/intent/launch_the_phone_dialer/

– http://developer.xamarin.com/recipes/ios/shared_reso

urces/phone/dial-phone-uri/

– http://developer.xamarin.com/guides/cross-

platform/xamarin-forms/dependency-service/

– http://developer.xamarin.com/guides/cross-

platform/xamarin-forms/messaging-center/

Thank you to our sponsors!