Scope
This Xamarin Workshop Guide was created for the The Portuguese National Meeting of IT Students (ENEI) by Sara Silva with the original content available here. With the goal to extend it to the global community, it was published in a new project called Xam Community Workshop with the main goal for any developer or user group to customize it to their events.
Before reading this article you must read:
- Xamarin Guide 1: Create a Xamarin Forms Project
- Xamarin Guide 2: Create the Model and the Data Source
- Xamarin Guide 3: Create the SessionsView
- Xamarin Guide 4: Create the SessionDetailsView
Guide 5. Add ShareService
An application that allows sharing content in social networks brings more value to the users, because it allows sharing with others something the user thinks is important or relevant. An application for events like 1010 ENEI Sessions App could not miss this feature.
Each platform has its own implementation to share content in the social network. This means we need to create an abstraction of the share feature to use it in ENEI.SessionsApp and then in each platform we need to implement that abstraction.
Let's see how the Abstraction Pattern can be created!
In ENEI.SessionsApp project, create the interface IShareService as in the following:
- public interface IShareService
- {
- void ShareLink(string title, string status, string link);
- }
Windows Phone
- [assembly: Dependency(typeof(ShareService))]
- namespace ENEI.SessionsApp.WinPhone.Services
- {
- public class ShareService : IShareService
- {
- public void ShareLink(string title, string status, string link)
- {
- var task = new ShareLinkTask { Title = title, Message = status, LinkUri = new Uri(link) };
- Device.BeginInvokeOnMainThread(() =>
- {
- try
- {
- task.Show();
- }
- catch (Exception ex)
- {
- // todo handle the error
- }
- });
- }
- }
- }
- [assembly: Dependency(typeof(ShareService))]
- namespace ENEI.SessionsApp.Droid.Services
- {
- public class ShareService : IShareService
- {
- public void ShareLink(string title, string status, string link)
- {
- var intent = new Intent(global::Android.Content.Intent.ActionSend);
- intent.PutExtra(global::Android.Content.Intent.ExtraText, string.Format("{0} - {1}", status ?? string.Empty, link ?? string.Empty));
- intent.PutExtra(global::Android.Content.Intent.ExtraSubject, title ?? string.Empty);
- intent.SetType("text/plain");
- intent.SetFlags(ActivityFlags.ClearTop);
- intent.SetFlags(ActivityFlags.NewTask);
- Android.App.Application.Context.StartActivity(intent);
- }
- }
- }
- [assembly: Dependency(typeof(ShareService))]
- namespace ENEI.SessionsApp.iOS.Services
- {
- public class ShareService : IShareService
- {
- public void ShareLink(string title, string status, string link)
- {
- var actionSheet = new UIActionSheet("Share on");
- foreach (SLServiceKind service in Enum.GetValues(typeof(SLServiceKind)))
- {
- actionSheet.AddButton(service.ToString());
- }
- actionSheet.Clicked += delegate(object a, UIButtonEventArgs b)
- {
- SLServiceKind serviceKind = (SLServiceKind)Enum.Parse(typeof(SLServiceKind), actionSheet.ButtonTitle(b.ButtonIndex));
- ShareOnService(serviceKind, title, status, link);
- };
- actionSheet.ShowInView(UIApplication.SharedApplication.KeyWindow.RootViewController.View);
- }
- private void ShareOnService(SLServiceKind service, string title, string status, string link)
- {
- if (SLComposeViewController.IsAvailable(service))
- {
- var slComposer = SLComposeViewController.FromService(service);
- slComposer.SetInitialText(status);
- slComposer.SetInitialText(title != null ? string.Format("{0} {1}", title, status) : status);
- if (link != null)
- {
- slComposer.AddUrl(new NSUrl(link));
- }
- slComposer.CompletionHandler += (result) =>
- {
- UIApplication.SharedApplication.KeyWindow.RootViewController.InvokeOnMainThread(() =>
- {
- UIApplication.SharedApplication.KeyWindow.RootViewController.DismissViewController(true, null);
- });
- };
- UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(slComposer, true, null);
- }
- }
- }
- }
- private void ShareGesture_OnTapped(object sender, EventArgs e)
- {
- var tappedEventArg = e as TappedEventArgs;
- if (tappedEventArg != null)
- {
- var session = tappedEventArg.Parameter as Session;
- if (session != null)
- {
- var shareService = DependencyService.Get<IShareService>();
- if (shareService != null)
- {
- var status = string.Format("Não percas a sessão {0} de {1}.", session.Name, session.Speaker.Name);
- shareService.ShareLink("ENEI 2015", status, "https://enei.pt/");
- }
- }
- }
- }
- [assembly: Dependency(typeof(ShareService))]

Karthik Muthu KaruppanPosted Apr 23, 2015, 11:19 AM
good one
Santhakumar MunuswamyPosted Apr 21, 2015, 8:30 AM
Thanks for nice article
NitinPosted Apr 21, 2015, 8:21 AM
nice