在xamarin中形成如何与facebook和g +和twitter共享图像和文本

时间:2017-06-12 02:36:40

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

我正在为Xamarin-Forms应用程序工作,我必须与社交网络共享图像和文本。我尝试过使用Xamarin.auth插件,但它对我不起作用。请建议任何其他社交分享插件。

2 个答案:

答案 0 :(得分:6)

在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

答案 1 :(得分:0)

对于那些仍在寻找的人,我在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);
    }

我没有使用subject参数,所以我删除了它。而且文本没有显示出来,我还没有找到解决方法。对于找到显示消息文本方式的任何人,请给出答案和评论,以便我解决您的问题。