如何使用Xamarin表单以.vcf / vcard格式与whtsapp共享我的应用程序的联系数据

时间:2018-12-07 10:16:41

标签: xamarin xamarin.forms xamarin.ios xamarin.android

我有一个在输入数据后创建.vcf文件的应用程序。它存储了电话提供的存储空间,我想单击列表视图项并获取vcf文件,并通过电子邮件,SMS,WhatsApp,Skype等共享它,如何在IOS和Android中实现此功能。

谢谢

1 个答案:

答案 0 :(得分:0)

我得到了创建.vcf文件的答案,如下所示。要共享该文件,请点击以下链接:https://github.com/adamped/ShareDialog

私人无效Share_Clicked(对象发送者,EventArgs e)         {             尝试             {                 var _btn =发送者为Button;                 var record = _btn.BindingContext作为Contact;                 int tempcontactID = record.ContactID;

            if (record.CardFrontImage == null)
            {
                record.CardImage = record.CardBackImage;
            }
            else
            {
                record.CardImage = record.CardFrontImage;
            }

            string baseimage = Convert.ToBase64String(record.CardImage);

            var vcf = new StringBuilder();  //vcf code start
            vcf.AppendLine("BEGIN:VCARD");
            vcf.AppendLine("VERSION:3.0");
            vcf.AppendLine($"N:{record.ContactName};{string.Empty}; ;;");
            vcf.AppendLine($"FN:{record.ContactName}");
            vcf.AppendLine($"ORG:{record.CompanyName}");
            vcf.AppendLine($"TITLE:{record.Designation}");
            vcf.AppendLine($"PHOTO;ENCODING=BASE64;TYPE=PNG:{baseimage}");
            vcf.AppendLine($"TEL;TYPE=work,voice;VALUE=uri:tel:{record.PhoneNumber}");

            vcf.AppendLine("END:VCARD");

            string fileName = Path.Combine("/storage/emulated/0/Android/data/com.Gamma.GammaNetworkingApp/files/", record.ContactID + record.ContactName + ".vcf");

            using (var writer = new StreamWriter(fileName))
            {
                writer.Write(vcf.ToString());
            }

            string text = File.ReadAllText(fileName);

            bool doesExist = File.Exists(fileName);

            if (doesExist == true)
            {
                var share = DependencyService.Get<IShare>();
                share.Show("Contact share", record.ContactName, fileName);
            }
        }
        catch (Exception ex)
        {
            string test = ex.ToString();
            Navigation.PushAsync(new HomePage());
        }
    }