Share Data And URI In Xamarin Forms Application Using Xamarin Essentials For Android And UWP

The DataTransfer class is available at Xamarin.Essentials API. It is used within an application to share data, such as text and web links, to other applications on the device. Android, iOS, and UWP offer unique operating systems and platform APIs that developers have access to all in C#, leveraging Xamarin. Xamarin.Essentials provides a single cross-platform API that works with any Xamarin.Forms, Android, iOS, or UWP application that can be accessed from shared code no matter how the user interface is created.

Reading this article, you can learn how to share data and URI in Xamarin Forms application using Xamarin Essentials for Android and Universal Windows Platform with XAML and Visual C# in cross-platform application development.

The following important tools are required for developing Xamarin Forms App,

  1. Windows 10 (Recommended)
  2. Visual Studio 2017
  3. Android API 19 or higher and UWP 10.0.16299.0 or higher.

Now we can discuss step by step App development.

Step 1


  • Open Visual studio 2017 -> Start -> New Project.
  • Select Cross-Platform (under Visual C#-> Mobile App (Xamarin.Forms).
  • Give the Suitable Name for your App (XamFormShare) ->OK.



Step 2

Select the Cross-Platform template as Blank APP ->set Platform as Android and UWP and code sharing strategy as .NET standard,.

Afterward, Visual Studio creates 3 projects (Portable, Android, UWP).

Step 3

For adding the reference,

Right-click your solution (XamFormShare) and select "Manage NuGet Packages".

For adding Xamarin.Essentials Reference, Choose Browse and Search Xamarin.Essentials, select the package and select all the projects (portable, Android, UWP) and install it.


Step 4

Add the Label and Button controls in Mainpage.Xaml for displaying the Title and sharing the Data and URI.

  1. <Label FontAttributes="Bold" Text="Share data in Xamarin Forms application using Xamarin Essentials for Android and UWP" VerticalOptions="Center" HorizontalOptions="Center"/>  
  2. <Entry Placeholder="Type the Text to Share" x:Name="entShare"/>  
  3. <Entry Placeholder="Type the URI to Share" x:Name="entShareuri"/>  
  4. <Button x:Name="btnShare" Text="Share" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" Clicked="Onbtnshare" />  
  5. <Button x:Name="btnShareuri" Text="Share URI" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" Clicked="OnbtnshareURI" />  

Step 5

Add the following code in the MainActivity.cs in XamFormShare.Android project.

In onCreate() method.

  1. Xamarin.Essentials.Platform.Init(this, savedInstanceState);  

Also, add the below method.

  1. publicoverridevoid OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults) {  
  2.     Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);  
  3.     base.OnRequestPermissionsResult(requestCode, permissions, grantResults);  

Step 6

Add the following namespace and code in Mainpage.Xaml.cs.

  1. using Xamarin.Essentials;  
  2. asyncvoid Onbtnshare(object sender, EventArgs args) {  
  3.     await ShareText(entShare.Text);  
  4. }  
  5. asyncvoid OnbtnshareURI(object sender, EventArgs args) {  
  6.     await ShareUri(entShareuri.Text);  
  7. }  
  8. publicasync Task ShareText(string text) {  
  9.     await DataTransfer.RequestAsync(new ShareTextRequest {  
  10.         Text = text,  
  11.             Title = "Share Text"  
  12.     });  
  13. }  
  14. publicasync Task ShareUri(string uri) {  
  15.     await DataTransfer.RequestAsync(new ShareTextRequest {  
  16.         Uri = uri,  
  17.             Title = "Share Web Link"  
  18.     });  

Automatically, the following code will be generated in XAML code view, while we are done in the design view.

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="" xmlns:x="" xmlns:local="clr-namespace:XamFormShare" x:Class="XamFormShare.MainPage">  
  3.     <StackLayout>  
  4.         <Label FontAttributes="Bold" Text="Share data in Xamarin Forms application using Xamarin Essentials for Android and UWP" VerticalOptions="Center" HorizontalOptions="Center" />  
  5.         <Entry Placeholder="Type the Text to Share" x:Name="entShare" />  
  6.         <Entry Placeholder="Type the URI to Share" x:Name="entShareuri" />  
  7.         <Button x:Name="btnShare" Text="Share" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" Clicked="Onbtnshare" />  
  8.         <Button x:Name="btnShareuri" Text="Share URI" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" Clicked="OnbtnshareURI" />  
  9.     </StackLayout>  
  10. </ContentPage>  

Step 7

We will test Android and UWP. So, we can set multiple Startup Projects as XamFormShare.Droid and XamFormShare.UWP (universal Windows).


Step 8

Deploy your App in UWP and Android. The output of the XamFormShare App is shown below.


Click the "Share Text" button for sharing a text.


Click the shareURI button for sharing a URI.



Now, you have successfully shared the data and URI in Xamarin Forms application using Xamarin Essentials for Android and Universal Windows Platform using Visual C# and Xamarin.

Similar Articles