【问题标题】:Cannot send email using xamarin ios implementation无法使用 xamarin ios 实现发送电子邮件
【发布时间】:2017-09-20 16:16:34
【问题描述】:

尝试通过以下方法发送电子邮件,但我不断收到 并且没有显示电子邮件ios表单。

" 尝试呈现 MFMailComposeViewController: 0x1560e1a00 on Xamarin_Forms_Platform_iOS_PlatformRenderer:0x157b5c180 其视图不在窗口层次结构中!”

对我需要做些什么来让它发挥作用有什么想法吗?

     public void SendEmail()
     {
            string to="jo@gmail.com";
            string subject="Test";
            string body="This is a test email";

            if (MFMailComposeViewController.CanSendMail)
            {
                var mailComposeViewController = new MFMailComposeViewController();

                mailComposeViewController.SetToRecipients(new[] { to });
                mailComposeViewController.SetSubject(subject);
                mailComposeViewController.SetMessageBody(body, false);

                mailComposeViewController.Finished += (s, args) =>
                {
                    Xamarin.Forms.Device.BeginInvokeOnMainThread
                    (
                        () => { args.Controller.DismissViewController(true, null); }
                    );
                };

                UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(mailComposeViewController, true, null);
            }           
    }

【问题讨论】:

  • 您在什么情况下定义SendEmail? (自定义表单的渲染器还是依赖服务?)
  • 我只有一个在 iOS 项目中实现的接口,方法是发送电子邮件。我愿意接受其他建议
  • @ColeXia 给出答案,我会接受的。非常感谢

标签: xamarin xamarin.ios


【解决方案1】:

我们应该在窗口顶部找到正确的 rootviewController。

试试这个:

var rootController = ((AppDelegate)(UIApplication.SharedApplication.Delegate)).Window.RootViewController.ChildViewControllers[0].ChildViewControllers[1].ChildViewControllers[0];

并将你的最后一行代码修改为

var rootController = ((AppDelegate)(UIApplication.SharedApplication.Delegate)).Window.RootViewController.ChildViewControllers[0].ChildViewControllers[1].ChildViewControllers[0];
var navcontroller = rootController as UINavigationController;
if (navcontroller != null)
    rootController = navcontroller.VisibleViewController;
rootController.PresentViewController (controller, true, null);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-11
    • 2015-09-06
    • 1970-01-01
    • 2021-04-13
    • 2021-09-12
    • 2013-03-30
    • 2017-02-15
    相关资源
    最近更新 更多