2. HAVE YOU EVER……
Developed a Xamarin.Forms App but wants to
use some platform-specific functionality
Wanted your Xamarin.Forms Apps can do anything
that a native Apps can do
If your answer is “YES”.
Then, you should give Dependency Service a try.
3. So, What is Dependency Service?
A Dependency resolver for
Xamarin.Forms Apps
Allowing Apps to call into
platform-specific functionality
from shared code.
4. Uhh… How does it Work?
When calling DependencyService.Get<Interface> at shared code, Dependency Service would get the
right implementation of interface when runtime.
5. How to use Dependency Service?
Implement your interface in
each platform.
(in .iOS, .Droid, .WinPhone)
Add some metadata attribute
before each implementation.
Declare your interface in shared
code which define how you
interact with platform-specific
functionality.
Interface
declaration
Implementation
Per Platform
Registration
3 step to follow
6. Let’s take an Example!
Xamarin Text-to-Speech
Author : Craig Dunn
Source Code: https://goo.gl/PNwhde
8. Step1 : Create Interface
Create Interface
1
Add interface ITextToSpeech in shared code
Note: Classes implementing the interface must
have a parameterless constructor to work with
the Dependency Service
9. Step2 : Implement Iterface (in iOS)
Create Interface
1
Implement Iterface in Project.iOS
Implement
Interface in each
platform
2
10. Step2 : Implement Iterface (in Android)
Create Interface
1
Implement Iterface in Project.Droid
Implement
Interface in each
platform
2
11. Step2 : Implement Iterface (in Windows Phone)
Create Interface
1
Implement Iterface in Project.WinPhone
Implement
Interface in each
platform
2
12. Step3 : Add Metadata Attribute before implementation
Create Interface
1
In .iOS
In .Droid
In .WinPhone
Add metadata
attribute before
Implementation.
3
13. Finally, we can test our result in MainPage!
Create Interface
1
Call Dependency
Service in
MainPage
4
Add a button to Speech the Text!
14. Let’s “see” the result!
In IOS In WinPhoneIn Android
Want to hear it?
Check this out: https://goo.gl/PNwhde