在 DependencyService 中访问 ViewController 以呈现 MFMailComposeViewController [英] Access ViewController in DependencyService to present MFMailComposeViewController

查看:13
本文介绍了在 DependencyService 中访问 ViewController 以呈现 MFMailComposeViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何访问我的 DependencyService 中的 ViewController 以呈现 MFMailComposeViewController?我尝试使用 Application.Context 但这似乎只适用于 Android.有什么建议吗?

How can i access the ViewController in my DependencyService to present a MFMailComposeViewController? I tried using Application.Context but this seems to be only working on Android. Any advice?

推荐答案

您可以通过执行 window.RootController.PresentViewController (mail controller, true, null); 来呈现 MFMailComposeViewController.根据您的应用程序架构,RootViewController 可能不是层次结构中可用的 ViewController.在这种情况下,你会得到一个

You can present a MFMailComposeViewController by doing a window.RootController.PresentViewController (mail controller, true, null);. Depending on your app architecture, the RootViewController might not be an usable ViewController in the hierarchy. In that case you get a

警告:尝试呈现 <MFMailComposeViewController: 0x16302c30>在 <Xamarin_Forms_Platform_iOS_PlatformRenderer: 0x14fd1530>其视图不在窗口层次结构中!

Warning: Attempt to present <MFMailComposeViewController: 0x16302c30> on <Xamarin_Forms_Platform_iOS_PlatformRenderer: 0x14fd1530> whose view is not in the window hierarchy!

在这种情况下,您必须挖掘具体的 ViewController,在我的情况下是:

In that case, you have to dig for the concrete ViewController, in my case it is:

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

这有点邪恶,但有效(已提交此问题以备将来修复).

which is a bit wicked, but works (An issue for this have been filed for future fix).

完整的解决方案如下:

在您的 AppDelegate.cs 中,添加:

public UIWindow Window {
    get { return window; }
}

在您的 PCL 项目中,声明接口:ISendMailService.cs

in your PCL project, declare the interface: ISendMailService.cs

public interface ISendMailService
{
    void ComposeMail (string[] recipients, string subject, string messagebody = null, Action<bool> completed = null);
}

在你的 iOS 项目中,实现并注册接口:SendMailService.cs

in your iOS project, implement and register the interface: SendMailService.cs

[assembly: DependencyAttribute(typeof(SendMailService))]

public class SendMailService : ISendMailService
{
    public void ComposeMail (string[] recipients, string subject, string messagebody = null, Action<bool> completed = null)
    {
        var controller = new MFMailComposeViewController ();
        controller.SetToRecipients (recipients);
        controller.SetSubject (subject);
        if (!string.IsNullOrEmpty (messagebody))
            controller.SetMessageBody (messagebody, false);
        controller.Finished += (object sender, MFComposeResultEventArgs e) => {
            if (completed != null)
                completed (e.Result == MFMailComposeResult.Sent);
            e.Controller.DismissViewController (true, null);
        };

        //Adapt this to your app structure
        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);
    }
}

您现在可以从您的 Xamarin.Forms PCL 项目中使用它:

And you can now consume it from your Xamarin.Forms PCL project:

new Button {
    Font = Font.SystemFontOfSize (NamedSize.Medium),
    Text = "Contact us",
    TextColor = Color.White,
    BackgroundColor = ColorsAndStyles.LightBlue,
    BorderRadius = 0,
    Command = new Command (()=>{
        var mailservice = DependencyService.Get<ISendMailService> ();
        if (mailservice == null)
            return;
        mailservice.ComposeMail (new [] {"foo@example.com"}, "Test", "Hello, World");
    })
}

这篇关于在 DependencyService 中访问 ViewController 以呈现 MFMailComposeViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆