本文介绍了如何在 Xamarin Forms 中打开默认电子邮件应用收件箱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发 Xamarin Forms 应用,并尝试将默认邮件客户端直接打开到收件箱.
I'm working on a Xamarin Forms app and am trying to open the the default mail client directly to the Inbox.
我能够使用 XF Essentials 打开并传递数据以撰写消息
I'm able to open and pass data through to compose a message using XF Essentials
Email.ComposeAsync(message);
但我希望应用程序在按下按钮时打开默认邮件应用程序的收件箱.这在 Xamarin Forms 中可行吗?
But I would like the app to open the default mail app's Inbox on a button press. Is this possible in Xamarin Forms?
推荐答案
我认为 依赖服务正是您所需要的.在您的表单项目中创建一个界面:
I think Dependency Service is what you need.Create an interface in your Forms project:
public interface IOpenManager
{
void openMail();
}
然后在每个平台上实现它,对于iOS:
Then implement it on each platform, for iOS:
[assembly: Dependency(typeof(OpenImplementation))]
namespace Demo.iOS
{
public class OpenImplementation : IOpenManager
{
public void openMail()
{
NSUrl mailUrl = new NSUrl("message://");
if (UIApplication.SharedApplication.CanOpenUrl(mailUrl))
{
UIApplication.SharedApplication.OpenUrl(mailUrl);
}
}
}
}
安卓:
[assembly: Dependency(typeof(OpenImplementation))]
namespace Demo.Droid
{
public class OpenImplementation : IOpenManager
{
public void openMail()
{
Intent intent = new Intent(Intent.ActionMain);
intent.AddCategory(Intent.CategoryAppEmail);
Android.App.Application.Context.StartActivity(intent);
}
}
}
最后调用这个依赖:DependencyService.Get().openMail();
这篇关于如何在 Xamarin Forms 中打开默认电子邮件应用收件箱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!