Xamarin.Forms Features That Will Make Your Life Easier

Download Xamarin.Forms Features That Will Make Your Life Easier

Post on 16-Jul-2015

261 views

Category:

Technology

2 download

Embed Size (px)

TRANSCRIPT

Bluetube Corporate Identity

Xamarin. Forms Features That Will Make Your Life Easier:DependencyService & MessagingCenterAndres CastroMy BackgroundAndres CastroASP.NET MVCASP.NET WEBFORMSWeb ServicesXamarin

Who Is BluetubeEstablished in 1999Award-Winning SolutionsExperts with Technology and UsabilityEnterprise Corporations & Non-profit ClientsPremier Xamarin Consulting PartnerXamarin Test Cloud Consulting Partners

Bluetube designs and builds enterprise mobile apps & responsive websites.

Dependency ServiceWhat 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.- This service makes it easy to register these classes and handles in deciding which registered platform will be used.4Dependency ServiceHow does it work?There are 3 steps necessary to successfully using the dependency service. Create an interface that will define how each platform should be implemented. Register each platform-specific implementation of the class above. Use the implemented interface using DependencyService.GetDependency ServiceCreating the InterfaceAs an example, I will be creating a interface that allows you to open the Phone Dialer from within your app.

- This interface will only have one purpose, and that is to start the call. As you can see the interface only has one method defined and that takes in a string as a phone number. Now that we have our interface we need to define how each platform will implement it.6Dependency ServiceRegistering the Android Implementation

- Note that the [assembly] attribute is needed to help register the implementation with Xamarin.As you can see the android gets the phone number, parses it into a URI and then starts a new intent. Note using the Forms.Context is the same as accessing it through the application context.7Dependency ServiceRegistering the iOS Implementation

8Dependency ServiceUsing the Code

Messaging CenterWhat 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 CenterHow does it work?There are three parts to using the Messaging Center Subscribe Subscribes the messaging center to listen to a certain message. You can then perform an action based on that message. Send Sends a message through the messaging center so that any subscribed classes receive the message. Unsubscribe Stops listening to any messages that might come through.Messaging CenterExampleLets 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 CenterLets first create a message class that will help to wrap data we want to send.

- The navigation message will pass along the object type and any object arguments needed to create a new page using reflection.13Messaging CenterSubscribing to the Message

Subscribe to the message on the root page, the callback method will then call navigateTo which will create a new page.

Subscribe (object subscriber, string message, Action callback, TSender source = null)

Tsender = Message.NavigationMessage,Message = Message.AppNavigationMessag,Subscriber = this,Callback = NavigateTo14Messaging CenterSending the Message

Send (TSender sender, string message)15RecapCovered 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 CenterReferenceshttp://developer.xamarin.com/recipes/android/fundamentals/intent/launch_the_phone_dialer/http://developer.xamarin.com/recipes/ios/shared_resources/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!