在Xamarin iOS

时间:2016-06-02 18:50:35

标签: ios xamarin uiactivityviewcontroller uiactivity

我正在尝试将UIActivityViewController和taler发送到活动类型(电子邮件/短信/ Facebook / Twitter /等)。我已经阅读了很多人在目标c中做到这一点的方法。但出于某种原因,我无法使用Xamarin / Monotouch。有人在Xamarin做过这个吗?

目标-c中的示例: https://www.albertopasca.it/whiletrue/objective-c-custom-uiactivityviewcontroller-icons-and-text/

我已经看到一些示例执行诸如传递活动项数组以及一系列应用程序活动之类的操作。我如何在Xamarin中做到这一点? UIActivityTypes只是NSStrings,我无法将NSString []传递给UIActivityViewController的构造函数

NSObject[] activityItems = new NSObject[] { item1, item2, item3, image };
NSString[] applicationActivities = new NSString[] { UIActivityType.Mail, UIActivityType.PostToTwitter, UIActivityType.Message };

UIActivityViewController activityController = new UIActivityViewController (activityItems, applicationActivities);

我也尝试过子类化UIActivityItemProvider并使用GetItemForActivity方法,但我不知道如何用UIActivityItemProvider初始化我的UIActivityViewController。我如何在Xamarin中做到这一点?

public class MyActivityItemProvider : UIActivityItemProvider
{
    public MyActivityItemProvider ()
    {
    }

    public override NSObject GetItemForActivity (UIActivityViewController activityViewController, NSString activityType)
    {
        return base.GetItemForActivity (activityViewController, activityType);
    }
}

1 个答案:

答案 0 :(得分:4)

知道了。事实证明,您将子类化的UIActivityItemProvider作为活动项目传递...

使用我的子类UIActivityItemProvider创建UIActivityViewController:

MyActivityItemProvider myActivityItemProvider = new MyActivityItemProvider (new NSString("blah blah blah"));
NSObject[] activityItems = new NSObject[] { myActivityItemProvider };
UIActivityViewController activityController = new UIActivityViewController(activityItems, null);

Subclassed UIActivityItemProvider:

public class MyActivityItemProvider : UIActivityItemProvider
{
    public MyActivityItemProvider (NSObject a_placeholderItem) : base (a_placeholderItem)
    {
    }

    public override NSObject GetItemForActivity (UIActivityViewController activityViewController, NSString activityType)
    {
        if (activityType == UIActivityType.Mail)
        {
            return new NSString("Email body");
        }
        else if (activityType == UIActivityType.Message)
        {
            return new NSString("Message body");
        }
        else if (activityType == UIActivityType.PostToTwitter)
        {
            return new NSString("Tweeeety");
        }

        return base.GetItemForActivity (activityViewController, activityType);
    }
}