Search code examples
c#facebookxamarin

Using Facebook API in C#


I'm working on an Android application in C# using Xamarin, this is for a school project. But since the documentation of the Facebook API is all in java it is sometimes hard to find out what exactly to do or find other people with the same problems. Now my problem; I'm trying to set up a sharecontent button which opens a sharecontentdialog, so far everything works great. I'm trying to add photos to the share and that's when shit hits the fan, the photos I have are drawables. I keep getting errors when I try to do this.

My code

    /* Facebook Share Code */
mBtnShared = view.FindViewById<ShareButton>(Resource.Id.btnShare); //Facebook share
mCallBackManager = CallbackManagerFactory.Create();
int id = 0;
Bitmap icon = BitmapFactory.DecodeResource(Resource.Drawable.A1010_1, id);
SharePhoto sharePhoto1 = new SharePhoto.Builder()
.SetBitmap(icon)
.Build();
ShareContent content = new ShareMediaContent.Builder().AddMedium(sharePhoto1).Build();
ShareDialog shareDialog = new ShareDialog(this.Activity);
shareDialog.Show(content);

Api example; https://developers.facebook.com/docs/sharing/android/#Multimedia

    SharePhoto sharePhoto1 = new SharePhoto.Builder()
    .setBitmap(...)
    .build();
SharePhoto sharePhoto2 = new SharePhoto.Builder()
    .setBitmap(...)
    .build();
ShareVideo shareVideo1 = new ShareVideo.Builder()
    .setLocalUrl(...)
    .build();
ShareVideo shareVideo2 = new ShareVideo.Builder()
    .setLocalUrl(...)
    .build();

ShareContent shareContent = new ShareMediaContent.Builder()
    .addMedium(sharePhoto1)
    .addMedium(sharePhoto2)
    .addMedium(shareVideo1)
    .addMedium(shareVideo2)
    .build();

ShareDialog shareDialog = new ShareDialog(...);
shareDialog.show(shareContent, Mode.AUTOMATIC);

The error I'm getting is on screen: Error So I'm thinking that something is wrong with my bitmap but I'm absolutely lost here. I hope you guys can help me!


Solution

  • Looked like I missed a cast that caused the error, needed to add (SharePhoto) in front of the new SharePhoto(..); Fixed :D