Share Plugin is used to easily share text, links, or to open a browser.
After reading this article, you will learn how to share text, link or open a browser in Xamarin.Forms Application for Android and Universal Windows Platform with XAML and Visual C# in cross platform application development.
The important tools are given below, which are required to develop UWP.
- Windows 10 (Recommended).
- Visual Studio 2015 Community Edition (It is a free software available online).
- Using Visual studio 2015 Installer, Enable the Xamarin (Cross Platform Mobile development and C#/.NET while install/Modify Visual studio 2015.
Now, we can discuss step by step app development.
Step 1
Open Visual Studio 2015 -> Start -> New Project-> Select Cross-Platform (under Visual C#-> Blank app (Xamarin.Forms Portable)-> Give the suitable name for your app (XamFormShare) ->OK.
Step 2
Now, create project “XamFormShare_Droid”, choose the Target and minimum platform version for your Universal Windows Project and create project “XamFormShare_UWP”.
Step 3
Afterwards, Visual Studio creates 6 projects and displays Getting Started.XamarinPage. Now, we have to update Xamarin.Forms Reference for Portable Project and XamFormShare_Droid project, using Manage NUGet Packages.
Also, add Plugin.Share Reference in the Portable, Android and UWP projects.
Step 4
Add XAML page for sharing Demo. Right click XamFormShare (Portable) project and select ADD-> NewItem.
Select ->CrossPlatform-> FormXamlPage-> Give the relevant name.
Step 5
In Share.Xaml page, add 3 buttons and label.
- <StackLayout>
- <Label Text="Xamarin Forms Share Plugin Demo - UWP and Android " FontSize="20" VerticalOptions="Center" HorizontalOptions="Center" />
- <Button Text="Open Browser" FontSize="20" TextColor="Gray" Clicked="OpenBrowser_Clicked" />
- <Button Text="Share a Text" FontSize="20" TextColor="Gray" Clicked="Text_Share_Clicked" />
- <Button Text="Share a Link" FontSize="20" TextColor="Gray" Clicked="Link_Share_Clicked" />
- </StackLayout>
Step 6
Add the code given below in Share.Xaml.cs for
each button click event.
- using Plugin.Share;
- void OpenBrowser_Clicked(object sender, EventArgs args) {
- CrossShare.Current.OpenBrowser("http://www.c-sharpcorner.com/members/vijayaragavan-s3");
- }
- void Text_Share_Clicked(object sender, EventArgs args) {
- CrossShare.Current.Share("Text of Share", "Title");
- }
- void Link_Share_Clicked(object sender, EventArgs args) {
- CrossShare.Current.ShareLink("Link To Share", "http://www.c-sharpcorner.com/");
- }
Step 7
Open (double click) the file App.cs in Solution Explorer-> XamFormShare (portable) and set the Root page.
Step 8
We will test Android and UWP. Thus, we can set the multiple startup projects as XamFormShare.Droid and XamFormShare.UWP (Universal Windows).
Step 9
Change the Configuration Manager settings and go to Build -> Configuration Manager.
Uncheck all the Build and deploy options to the iOS, Windows, WinPhone, check the Droid and UWP.
Step 10
Deploy your app in Local Machine and the output of the XamFormShare app is shown below.
After clicking, open Browser button.
After clicking, sharing a Text button is given below.
After Clicking, Share a Link button is given below.
Summary
Now, you have successfully tested sharing text, links, or opening a Browser in Xamarin.Forms Application for Cross Platform application development using Visual C# and Xamarin.