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

Xamarin.Forms Features That Will Make Your Life Easier

Embed Size (px)

Citation preview

Page 1: Xamarin.Forms Features That Will Make Your Life Easier

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

Andres Castro

Page 2: Xamarin.Forms Features That Will Make Your Life Easier

My Background

Andres Castro• ASP.NET MVC

• ASP.NET WEBFORMS

• Web Services

• Xamarin

Page 3: Xamarin.Forms Features That Will Make Your Life Easier

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.

Page 4: Xamarin.Forms Features That Will Make Your Life Easier

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.

Page 5: Xamarin.Forms Features That Will Make Your Life Easier

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>

Page 6: Xamarin.Forms Features That Will Make Your Life Easier

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.

Page 7: Xamarin.Forms Features That Will Make Your Life Easier

Dependency Service

Registering the Android Implementation

Page 8: Xamarin.Forms Features That Will Make Your Life Easier

Dependency Service

Registering the iOS Implementation

Page 9: Xamarin.Forms Features That Will Make Your Life Easier

Dependency Service

Using the Code

Page 10: Xamarin.Forms Features That Will Make Your Life Easier

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.

Page 11: Xamarin.Forms Features That Will Make Your Life Easier

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.

Page 12: Xamarin.Forms Features That Will Make Your Life Easier

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.

Page 13: Xamarin.Forms Features That Will Make Your Life Easier

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

we want to send.

Page 14: Xamarin.Forms Features That Will Make Your Life Easier

Messaging Center

Subscribing to the Message

Page 15: Xamarin.Forms Features That Will Make Your Life Easier

Messaging Center

Sending the Message

Page 16: Xamarin.Forms Features That Will Make Your Life Easier

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

Page 17: Xamarin.Forms Features That Will Make Your Life Easier

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/

Page 18: Xamarin.Forms Features That Will Make Your Life Easier
Page 19: Xamarin.Forms Features That Will Make Your Life Easier

Thank you to our sponsors!