【问题标题】:Sharing text to social networks, xamarin.forms Android将文本分享到社交网络,xamarin.forms Android
【发布时间】:2018-01-16 06:36:05
【问题描述】:

我一直在尝试实现向社交平台分享文本的功能。我尝试使用 DependencyService 的代码仅在我的电子邮件的“主题”中返回我的文本。当我尝试将文本复制到剪贴板时,这不起作用。文本也不会传递到任何其他社交平台。请查看下面的代码。我有什么遗漏吗?

IShareService(接口类)

public interface IShareService
    {
        void SharePageLink(string link);
    }

ShareService(Android 项目中的类)

[assembly: Dependency(typeof(ShareService))]
namespace LoyaltyWorx.Droid
{
    public class ShareService: IShareService
    {
        public void SharePageLink(string text)
        {
            var context = Forms.Context;
            Activity activity = context as Activity;

            Intent share = new Intent(Intent.ActionSend);
            share.SetType("text/plain");
            share.AddFlags(ActivityFlags.ClearWhenTaskReset);
            share.PutExtra(Intent.ExtraSubject, "Cool");
            share.PutExtra(Intent.ExtraSubject, text);

            activity.StartActivity(Intent.CreateChooser(share, "Share text!"));

        }

    }
}

Promotions.xaml.cs(要分享的文字)

 private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
        {
            string shareText = "Promotion: " + store + "- " + description;
            DependencyService.Get<IShareService>().SharePageLink(shareText);
        }

【问题讨论】:

    标签: c# xamarin xamarin.forms share


    【解决方案1】:

    您可以在 Xamarin.Forms 中使用 Share plugin 共享消息或链接、将文本复制到剪贴板或在任何 Xamarin 或 Windows 应用中打开浏览器。

    在 Portable、Android 和 UWP 项目中添加 Plugin.Share 引用,并像这样使用:

    using Plugin.Share; 
    
    void OpenBrowser() {  
        CrossShare.Current.OpenBrowser("http://www.google.com");  
    }  
    void TextShare() {  
        CrossShare.Current.Share("Share Text", "Sample Title");  
    }  
    void LinkShare() {  
        CrossShare.Current.ShareLink("Link To Share", "http://www.stackoverflow.com/");  
    }   
    

    更多详情请参考this官方文档。

    【讨论】:

      猜你喜欢
      • 2013-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多