Sometimes you need to use platform specific code in Xamarin Forms. To do this you can use DependencyService. In this example i want to show how you can call an Android specific mail intent.
1) Create an interface in your project folder
1 2 3 4 5 6 7 8 |
using System; namespace Dependencytest { public interface IMailIntent { void sendIntent(string mail); } } |
2) Create a class in your Droid Folder that implements the interface
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[assembly: Xamarin.Forms.Dependency(typeof(MailIntent))] namespace Dependencytest.Droid { public class MailIntent : IMailIntent { public void sendIntent(string mail) { var intent = new Intent(Intent.ActionSend); intent.SetType("message/rfc822"); intent.PutExtra(Intent.ExtraEmail, new[] { "ddd@ddd.de" }); Forms.Context.StartActivity(Intent.CreateChooser(intent, "Send email")); } } } |
Don’t forget the assembly annotation
3) Use DependencyService
Now you can use the DependencyService in your shared code to call the native function
1 |
DependencyService.Get<IMailIntent>().sendIntent("hello@world.com"); |
Recent Comments