在 Xamarin 中根据 ActivityType 自定义共享消息 iOS

Customize share message based on ActivityType, in Xamarin iOS

我正在尝试创建 UIActivityViewController 并将每条消息发送到 Activity 类型 (Email/SMS/Facebook/Twitter/etc)。我在 objective c 中读到了许多人们这样做的方法。但出于某种原因,我无法让它与 Xamarin/Monotouch 一起使用。有人在 Xamarin 中做过吗?

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

我见过一些示例执行诸如传入 activity 项数组以及应用程序活动数组之类的操作。我如何在 Xamarin 中执行此操作? UIActivity 类型只是 NSString,我无法将 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 方法,但我不知道如何初始化我的 UIActivityViewController 与那个 UIActivityItemProvider。我如何在 Xamarin 中执行此操作?

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

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

知道了。事实证明,您将子类化的 UIActivityItemProvider 作为 activity 项传递进来...

使用我的子类 UIActivityItemProvider 创建 UIActivityViewController:

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

子类 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);
    }
}