UI, Xamarin Essentials

Sharing information in Xamarin Forms

Sometimes, we need to share information from our App to other apps, such as an address, a file, an URL among others. Xamarin Essential gives us the power to do it! In this post we will be learning how to do it.

This post will be divided in the following topics:

? Platform Supports

? Sharing overload methods

? Experimental preview

First of all… What do I need?

  • Add the following NuGet Package: Xamarin.Essentials

You can read more information about Xamarin Essential here.

Let’s start!!!

There are some things that we have to be clear about the meaning and the support for each platform:

?Title:       Is the title that will get the information to be shared.

?Subject:  Is the subtitle (If applicable) that will get the information to shared.

?Text:        Is the body of the message.

The method that is responsible to the make the information being shared is the RequestAsync method inside the Share class. It has 4 overloads that gives us the flexibility to decide the information type that will be shared. Depending of the overload method used, the information is composed by different data such as subject, url, among others, let’s learn one bye one :

It’s important to note that the data will be shared respecting the rules of the selected application. For example: If we share by email, we can see the title and the text or an attachment in the email message, but if we share it on WhatsApp the title will not be shown.

RequestAsync ( Text ): Receives a string representing the body message that will be shared.

RequestAsync ( Text , Title ) : Receives two string. representing the body and the title that will be shared. (Respectively).

RequestAsync ( ShareTestRequest ) : Receives a ShareTextRequest class type. This class receives: Title, subject, Uri and text as parameters.

There are other overload method, but only as an experimental preview.

A experimental preview, as their name suggests, are functions or libraries from preview versions of Xamarin.Forms available for experimental purposes and you need to indicate that you want to enable it. If you want to know more information about it, you can enter here.

RequestAsync ( ShareFileRequest ): Receives a ShareFileRequest class type. This class receives: Title and a File as parameters

First, in your App.xaml.cs you must add the following line:

Then, you can add it:

Thanks for reading ?!!!

Spanish post: https://medium.com/@reyes.leomaris/compartiendo-informaci%C3%B3n-share-en-xamarin-forms-4c6525c19b5d

References: https://docs.microsoft.com/en-us/xamarin/essentials/share?context=xamarin%2Fxamarin-forms&tabs=uwp

Tagged , ,

1 thought on “Sharing information in Xamarin Forms

Leave a Reply

Your email address will not be published. Required fields are marked *