【问题标题】:How to open default email app inbox in Xamarin Forms如何在 Xamarin Forms 中打开默认电子邮件应用收件箱
【发布时间】:2019-05-10 12:43:43
【问题描述】:

我正在开发一个 Xamarin Forms 应用程序并尝试将默认邮件客户端直接打开到收件箱。

我能够打开并传递数据以使用 XF Essentials 撰写消息

Email.ComposeAsync(message);

但我希望该应用在按下按钮时打开默认邮件应用的收件箱。这在 Xamarin Forms 中可行吗?

【问题讨论】:

  • 检查this answer,你必须在你的android项目上实现它,然后通过dependency injection使用它
  • 非常感谢。我今天早上上班时看到了这两个答案。感谢您抽出宝贵时间回复。

标签: xamarin.forms


【解决方案1】:

我认为Dependency Service 是您所需要的。 在您的 Forms 项目中创建一个界面:

public interface IOpenManager
{
    void openMail();
}

然后在每个平台上实现它,对于 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<IOpenManager>().openMail(); 调用此依赖项

【讨论】:

  • 谢谢!!我很难找到任何有用的东西,但这正是我需要的。
猜你喜欢
  • 2019-09-06
  • 2018-03-20
  • 2011-12-27
  • 1970-01-01
  • 1970-01-01
  • 2015-10-02
  • 2021-12-29
  • 2019-04-17
  • 1970-01-01
相关资源
最近更新 更多