Xamarin.Essentials: Share

The Share class enables an application to share data such as text and web links to other applications on the device.

Get started

To start using this API, read the getting started guide for Xamarin.Essentials to ensure the library is properly installed and set up in your projects.

Using Share

Add a reference to Xamarin.Essentials in your class:

using Xamarin.Essentials;

The Share functionality works by calling the RequestAsync method with a data request payload that includes information to share to other applications. Text and Uri can be mixed and each platform will handle filtering based on content.


public class ShareTest
{
    public async Task ShareText(string text)
    {
        await Share.RequestAsync(new ShareTextRequest
            {
                Text = text,
                Title = "Share Text"
            });
    }

    public async Task ShareUri(string uri)
    {
        await Share.RequestAsync(new ShareTextRequest
            {
                Uri = uri,
                Title = "Share Web Link"
            });
    }
}

User interface to share to external application that appears when request is made:

Share

Platform Differences

  • Subject property is used for desired subject of a message.

Files

Preview feature

Sharing files is available as an experimental preview in Xamarin.Essentials version 1.1.0. This features enables an app to share files to other applications on the device. To enable this feature set the following property in your app's startup code:

ExperimentalFeatures.Enable(ExperimentalFeatures.ShareFileRequest);

After the feature enabled any file can be shared. Xamarin.Essentials will automatically detect the file type (MIME) and request a share. Each platform may only support specific file extensions.

Here is a sample of writing text to disk and sharing it to other apps:

var fn =  "Attachment.txt";
var file = Path.Combine(FileSystem.CacheDirectory, fn);
File.WriteAllText(file, "Hello World");

await Share.RequestAsync(new ShareFileRequest
{
    Title = Title,
    File = new ShareFile(file)
});

API

Find more Xamarin videos on Channel 9 and YouTube.