Share Plugin In Xamarin.Forms Application For Android And UWP

Share Plugin is used to easily share text, links, or to open a browser.

After reading this article, you will learn how to share text, link or open a browser in Xamarin.Forms Application for Android and Universal Windows Platform with XAML and Visual C# in cross platform application development.

The important tools are given below, which are required to develop UWP.

  1. Windows 10 (Recommended).
  2. Visual Studio 2015 Community Edition (It is a free software available online).
  3. Using Visual studio 2015 Installer, Enable the Xamarin (Cross Platform Mobile development and C#/.NET while install/Modify Visual studio 2015.

Now, we can discuss step by step app development.

Step 1

Open Visual Studio 2015 -> Start -> New Project-> Select Cross-Platform (under Visual C#-> Blank app (Xamarin.Forms Portable)-> Give the suitable name for your app (XamFormShare) ->OK.

 

Step 2

Now, create project “XamFormShare_Droid”, choose the Target and minimum platform version for your Universal Windows Project and create project “XamFormShare_UWP”.

 

Step 3

Afterwards, Visual Studio creates 6 projects and displays Getting Started.XamarinPage. Now, we have to update Xamarin.Forms Reference for Portable Project and XamFormShare_Droid project, using Manage NUGet Packages.

 

Also, add Plugin.Share Reference in the Portable, Android and UWP projects.

 
Step 4

Add XAML page for sharing Demo. Right click XamFormShare (Portable) project and select ADD-> NewItem.

Select ->CrossPlatform-> FormXamlPage-> Give the relevant name.

 

Step 5

In Share.Xaml page, add 3 buttons and label.

  1. <StackLayout>  
  2.     <Label Text="Xamarin Forms Share Plugin Demo - UWP and Android " FontSize="20" VerticalOptions="Center" HorizontalOptions="Center" />  
  3.     <Button Text="Open Browser" FontSize="20" TextColor="Gray" Clicked="OpenBrowser_Clicked" />  
  4.     <Button Text="Share a Text" FontSize="20" TextColor="Gray" Clicked="Text_Share_Clicked" />  
  5.     <Button Text="Share a Link" FontSize="20" TextColor="Gray" Clicked="Link_Share_Clicked" />   
  6. </StackLayout>   
 

Step 6

Add the code given below in Share.Xaml.cs for each button click event.

  1. using Plugin.Share;  
  2. void OpenBrowser_Clicked(object sender, EventArgs args) {  
  3.     CrossShare.Current.OpenBrowser("http://www.c-sharpcorner.com/members/vijayaragavan-s3");  
  4. }  
  5. void Text_Share_Clicked(object sender, EventArgs args) {  
  6.     CrossShare.Current.Share("Text of Share""Title");  
  7. }  
  8. void Link_Share_Clicked(object sender, EventArgs args) {  
  9.     CrossShare.Current.ShareLink("Link To Share""http://www.c-sharpcorner.com/");  
  10. }   
 

Step 7

Open (double click) the file App.cs in Solution Explorer-> XamFormShare (portable) and set the Root page.

 

Step 8

We will test Android and UWP. Thus, we can set the multiple startup projects as XamFormShare.Droid and XamFormShare.UWP (Universal Windows).

 

Step 9

Change the Configuration Manager settings and go to Build -> Configuration Manager.

Uncheck all the Build and deploy options to the iOS, Windows, WinPhone, check the Droid and UWP.

 

Step 10

Deploy your app in Local Machine and the output of the XamFormShare app is shown below.

 

After clicking, open Browser button.

 

After clicking, sharing a Text button is given below.

 

After Clicking, Share a Link button is given below.

 

Summary

Now, you have successfully tested sharing text, links, or opening a Browser in Xamarin.Forms Application for Cross Platform application development using Visual C# and Xamarin.


Similar Articles