Xamarin.Forms Application For Social Media Link Sharing

Introduction
 
This article demonstrates how to developXamarin.Forms applications for sharing links on social media. Xamarin is a platform that allows us to create a multi-platform mobile application for Android, Windows, and iOS through a single integrated development environment (IDE).
 
 
 
Let's start.
 
Step 1 
 
Open Visual Studio and go to New Project >> Installed >> Visual C# >> Cross-Platform.
 
Select Cross-Platform app, then give your project a name and location.
 
 
 
Step 2 
 
Install the following NuGet package to your project. 
  • Plugin.Share 
 
 
Now, select the following NuGet package and select your project to install the package. 
 
 
 
The package is installed successfully in your project. 
 
 
  
Step 3 
 
Open Solution Explorer >> Project Name (Portable) >> MainPage.xaml. Open the design view of this page.
 
 
 
The code is given below.
 
 
 
XAML Code 
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4.              xmlns:local="clr-namespace:Link_Share"  
  5.              x:Class="Link_Share.MainPage">  
  6.     <ContentPage.Content>  
  7.         <StackLayout>  
  8.             <Button Text="Share"   
  9.                    HorizontalOptions="Center"  
  10.                    VerticalOptions="Center"  
  11.                    Clicked="Button_Clicked"/>  
  12.         </StackLayout>  
  13.     </ContentPage.Content>  
  14. </ContentPage>  
  15.       
Step 4  
 
Open Solution Explorer >> Project Name (Portable) >> MainPage.xaml.cs. Open the design view of this page.
 
 
 
The code is given below.
 
 
 
This is the code for the button. Click the button and give the URL that you want to share the link for.
 
  
 
C# Code 
  1. using Plugin.Share;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Threading.Tasks;  
  7. using Xamarin.Forms;  
  8.   
  9. namespace Link_Share  
  10. {  
  11.     public partial class MainPage : ContentPage  
  12.     {  
  13.         public MainPage()  
  14.         {  
  15.             InitializeComponent();  
  16.         }  
  17.   
  18.         private void Button_Clicked(object sender, EventArgs e)  
  19.         {  
  20.             CrossShare.Current.ShareLink("https://www.c-sharpcorner.com/members/ajith-kumar51");  
  21.         }  
  22.     }  
  23. }   
Step 5 
 
Next, select the "Build & Deploy" option followed by selecting from the list of Android Emulators or Simulators. You can choose any API (Application Program Interface) Level Emulator or Simulator to run it.
 
Output 
 
After a few seconds, you will see your app working.
 
Click the "Share" button. It will open the Social Media option as displayed.
 
 
 
Choose any Social Media. I have chosen WhatsApp Media and shared the link.
 
 
 The result is displayed below. 
 
 
 
Finally, we have successfully created a Xamarin.Forms application for link sharing on Social Media. 


Similar Articles