MFMailComposeViewController

MFMailComposeViewController

本文介绍了在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

在这种情况下,您必须挖掘具体的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中,添加以下内容:

in your AppDelegate.cs, add this:

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 14:00