Olá, neste post irei demonstrar como você pode implementar a função de “Share” para compartilhar textos e links em suas aplicações Xamarin.Forms.
ADICIONANDO O NUGET PACKAGE
Clique com o botão direito em cima de sua Solution e selecione “Manage NuGet Packages for Solution…”.
Digite “Plugin.Share” e selecione o plugin como demonstrado na imagem a seguir.
Selecione o projeto compartilhado e clique no botão “Install”.
Xaml
Para este exemplo utilizei um ToolbarItem para ser o meu botão de share.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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:ShareDemo" | |
x:Class="ShareDemo.MainPage"> | |
<ContentPage.ToolbarItems> | |
<ToolbarItem Icon="share.png" Clicked="Share_Tapped"/> | |
</ContentPage.ToolbarItems> | |
<StackLayout> | |
<Image Source="logo.png" | |
HorizontalOptions="Center" | |
VerticalOptions="CenterAndExpand" /> | |
<Label Text="http://www.julianocustodio.com" | |
HorizontalOptions="Center" | |
VerticalOptions="CenterAndExpand" /> | |
</StackLayout> | |
</ContentPage> |
C#
Referencie o Plugin.Share, crie um ShareMessage definindo Text, Title e Url. Em seguida passe ShareMessage como parâmetro no método CrossShare.Current.Share.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Plugin.Share; | |
using System; | |
using Xamarin.Forms; | |
namespace ShareDemo | |
{ | |
public partial class MainPage : ContentPage | |
{ | |
public MainPage() | |
{ | |
InitializeComponent(); | |
} | |
private void Share_Tapped(object sender, EventArgs e) | |
{ | |
var ShareMessage = new Plugin.Share.Abstractions.ShareMessage | |
{ | |
Text = "Exemplo de como compartilhar textos ou links em Aplicações Xamarin.Forms. / Example of how to share texts or links in Xamarin.Forms Applications.", | |
Title = "Share", | |
Url = "https://www.julianocustodio.com" | |
}; | |
CrossShare.Current.Share(ShareMessage); | |
} | |
} | |
} |
Resultado
Esse e todos os exemplos deste blog encontram-se disponíveis no GitHub.
Meus parabéns pelo post e pelo blog.
Saberia informar se tem como adicionar outras opções a janela de compartilhamento e se tem a possibilidade de traduzir o plugin, já que as opções de copiar e adicionar a lista de leitura ficam em inglês.
CurtirCurtir
Olá Gleison,
Obrigado.
Esse plugin tem o intuito de compartilhar, textos e links, então ele identifica todos os apps instalados no dispositivo que possa ser realizado o compartilhamento.
E em relação ao idioma, ele respeita o idioma do dispositivo, se você reparar, no GIF apresentado no final do meu exemplo ele está em português.
Espero ter ajudado. Abraço
CurtirCurtir