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,
- Windows 10 (Recommended)
- Visual Studio 2017
- 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.
- <Label FontAttributes="Bold" Text="Share data in Xamarin Forms application using Xamarin Essentials for Android and UWP" VerticalOptions="Center" HorizontalOptions="Center"/>
- <Entry Placeholder="Type the Text to Share" x:Name="entShare"/>
- <Entry Placeholder="Type the URI to Share" x:Name="entShareuri"/>
- <Button x:Name="btnShare" Text="Share" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" Clicked="Onbtnshare" />
- <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.
- Xamarin.Essentials.Platform.Init(this, savedInstanceState);
Also, add the below method.
- publicoverridevoid OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults) {
- Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
- base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
- }
Step 6
Add the following namespace and code in Mainpage.Xaml.cs.
- using Xamarin.Essentials;
- asyncvoid Onbtnshare(object sender, EventArgs args) {
- await ShareText(entShare.Text);
- }
- asyncvoid OnbtnshareURI(object sender, EventArgs args) {
- await ShareUri(entShareuri.Text);
- }
- publicasync Task ShareText(string text) {
- await DataTransfer.RequestAsync(new ShareTextRequest {
- Text = text,
- Title = "Share Text"
- });
- }
- publicasync Task ShareUri(string uri) {
- await DataTransfer.RequestAsync(new ShareTextRequest {
- Uri = uri,
- Title = "Share Web Link"
- });
- }
Note
Automatically, the following code will be generated in XAML code view, while we are done in the design view.
- <?xml version="1.0" encoding="utf-8" ?>
- <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:XamFormShare" x:Class="XamFormShare.MainPage">
- <StackLayout>
- <Label FontAttributes="Bold" Text="Share data in Xamarin Forms application using Xamarin Essentials for Android and UWP" VerticalOptions="Center" HorizontalOptions="Center" />
- <Entry Placeholder="Type the Text to Share" x:Name="entShare" />
- <Entry Placeholder="Type the URI to Share" x:Name="entShareuri" />
- <Button x:Name="btnShare" Text="Share" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" Clicked="Onbtnshare" />
- <Button x:Name="btnShareuri" Text="Share URI" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" Clicked="OnbtnshareURI" />
- </StackLayout>
- </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.
Summary
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.