【问题标题】:in xamarin forms how to share images and text with facebook and g+ and twitter在 xamarin 表单中如何与 facebook、g+ 和 twitter 共享图像和文本
【发布时间】:2017-11-13 09:53:35
【问题描述】:

我正在为 Xamarin-Forms 应用程序工作,我必须在其中与社交网络共享图像和文本。我曾尝试使用 Xamarin.auth 插件,但它不适用于我。请推荐任何其他用于社交分享的插件。

【问题讨论】:

    标签: c#-4.0 xamarin xamarin.forms xamarin-studio


    【解决方案1】:

    在 PCL 中:

    using System;
    using Xamarin.Forms;
    
    namespace ShareSample
    {
      public interface IShare
      {
         void Share(string subject, string message, ImageSource image);
      }
    }
    

    Xamarin.Android:

    using Android.App;
    using Android.Content;
    using Android.Graphics;
    using Android.OS;
    using ShareSample.Droid;
    using Xamarin.Forms;
    using Xamarin.Forms.Platform.Android;
    
    [assembly: Dependency(typeof(IShareService))]
    namespace ShareSample.Droid
    {
      public class IShareService : Activity, IShare
      {
         public async void Share(string subject, string message, 
         ImageSource image)
        {
            var intent = new Intent(Intent.ActionSend);
            //intent.PutExtra(Intent.ExtraSubject, subject);
            intent.PutExtra(Intent.ExtraText, message);
            intent.SetType("image/png");
    
            var handler = new ImageLoaderSourceHandler();
            var bitmap = await handler.LoadImageAsync(image, this);
    
            var path = Environment.GetExternalStoragePublicDirectory(Environment.DirectoryDownloads
                + Java.IO.File.Separator + "logo.png");
    
            using (var os = new System.IO.FileStream(path.AbsolutePath, System.IO.FileMode.Create))
            {
                bitmap.Compress(Bitmap.CompressFormat.Png, 100, os);
            }
    
            intent.PutExtra(Intent.ExtraStream, Android.Net.Uri.FromFile(path));
            Forms.Context.StartActivity(Intent.CreateChooser(intent, "Share Image"));
        }
    }
    

    }

    Xamarin.iOS:

     using Foundation;
     using ShareSample.iOS;
     using UIKit;
     using Xamarin.Forms;
     using Xamarin.Forms.Platform.iOS;
    
     [assembly: Dependency(typeof(IShareService))]
     namespace ShareSample.iOS
     {
      public class IShareService : IShare
      {
        public async void Share(string subject, string message, ImageSource image)
        {
            var handler = new ImageLoaderSourceHandler();
            var uiImage = await handler.LoadImageAsync(image);
    
            var img = NSObject.FromObject(uiImage);
            var mess = NSObject.FromObject(message);
    
            var activityItems = new[] { mess, img };
            var activityController = new UIActivityViewController(activityItems, null);
    
            var topController = UIApplication.SharedApplication.KeyWindow.RootViewController;
    
            while (topController.PresentedViewController != null)
            {
                topController = topController.PresentedViewController;
            }
    
            topController.PresentViewController(activityController, true, () => { });
        }
    
      }
    

    }

    在 PCL 中调用 DependencyService:

      using System;
      using Xamarin.Forms;
    
     namespace ShareSample
     {
        public class SharePage : ContentPage
        {
          public SharePage()
          {
            Button sharebutton = new Button()
            {
                Text = "Share",
                HorizontalOptions = LayoutOptions.FillAndExpand,
                VerticalOptions = LayoutOptions.CenterAndExpand,
                TextColor = Color.White,
                BackgroundColor = Color.Blue
    
            };
    
            Image img = new Image()
            {
                Source = "http://www.wintellect.com/devcenter/wp-content/uploads/2013/10/Wintellect_logo.gif",
                Aspect = Aspect.AspectFit
            };
    
            sharebutton.Clicked += (sender, e) =>
            {
                DependencyService.Get<IShare>().Share(" ", "Hi Balaraju. How are you?", img.Source);
            };
    
            StackLayout stack = new StackLayout()
            {
                HorizontalOptions = LayoutOptions.FillAndExpand,
                VerticalOptions = LayoutOptions.FillAndExpand,
                BackgroundColor = Color.Aqua,
                Children = { sharebutton }
            };
    
            Content = stack;
            Padding = new Thickness(0, 20, 0, 0);
        }
      }
     }
    

    不需要任何插件,我们也可以分享文字和图片,

    请找到示例的 DropBox 链接:

    https://www.dropbox.com/s/32o9uuew369yupi/ShareSample.zip?dl=0

    这里的问题是,如果您想分享您设备中可用的特定应用程序(如 facebook、twitter)的图像或文本

    (OR)
    

    在您的设备中没有应用程序:

    使用 Xamarin.Auth

    http://www.c-sharpcorner.com/article/oauth-login-authenticating-with-identity-provider-in-xamarin-forms/

    http://www.c-sharpcorner.com/article/register-identity-provider-for-new-oauth-application/

    https://visualstudiomagazine.com/articles/2014/04/01/using-oauth-twitter-and-async-to-display-data.aspx?m=2

    https://github.com/HoussemDellai/Facebook-Login-Xamarin-Forms

    【讨论】:

    • 此代码是否需要任何平台特定权限?
    • ios 9.3.5 = 崩溃。 *** 由于未捕获的异常“NSGenericException”而终止应用程序,原因:“UIPopoverPresentationController (<_uialertcontrolleractionsheetregularpresentationcontroller:>) 应该在演示发生之前设置一个非零的 sourceView 或 barButtonItem。”
    • @venkata-swamy-balaraju 图片在您的示例中的 iOS 和 Android 中均为空。您能否提供相同的解决方案。
    【解决方案2】:

    对于那些仍在寻找的人,我在 Xamarin.Android 上为 Venkata Swamy 的代码添加了一些修复,它不再适用于已弃用的功能等。

        public async void Share(string message, ImageSource image)
        {
            var intent = new Intent(Intent.ActionSend);
            intent.PutExtra(Intent.ExtraText, message);
            intent.SetType("image/png");
    
            IImageSourceHandler handler = null;
            if (image is UriImageSource)
                handler = new ImageLoaderSourceHandler();
            else if (image is FileImageSource)
                handler = new FileImageSourceHandler();
            else if (image is StreamImageSource) handler = new StreamImagesourceHandler();
            var bitmap = await handler.LoadImageAsync(image, Application.Context);
    
            var path = Environment.GetExternalStoragePublicDirectory(Environment.DirectoryDownloads + File.Separator + "logo.png");
    
            using (var os = new FileStream(path.AbsolutePath, FileMode.Create))
            {
                bitmap.Compress(Bitmap.CompressFormat.Png, 100, os);
            }
    
            var imageUri = FileProvider.GetUriForFile(Application.Context, "com.uajy.atmarewards.fileprovider", path);
            intent.PutExtra(Intent.ExtraStream, imageUri);
            var newIntent = Intent.CreateChooser(intent, "Share Image");
            newIntent.SetFlags(ActivityFlags.NewTask);
            Application.Context.StartActivity(newIntent);
        }
    

    我没有使用主题参数,所以我删除了它。而且文本不知何故没有显示,我还没有找到解决方法。对于找到显示消息文本的方法的任何人,请给出答案并发表评论,以便我可以解决您的问题。

    【讨论】:

      猜你喜欢
      • 2011-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-28
      • 1970-01-01
      • 2013-04-13
      • 1970-01-01
      相关资源
      最近更新 更多