【问题标题】:xamarin forms: Calling phone and sending email (IOS, Android and UWP)xamarin 形式:拨打电话和发送电子邮件(IOS、Android 和 UWP)
【发布时间】:2018-06-19 00:38:39
【问题描述】:

目前使用以下代码进行通话和电子邮件功能,但它仅适用于 Android,不适用于 IOS。另外,我需要 UWP 中的这些功能。

来电:

string phoneno = "1234567890";
Device.OpenUri(new Uri("tel:" + phoneno));

对于邮件:

string email = "sreejithsree139@gmail.com";
Device.OpenUri(new Uri("mailto:" + email ));

有任何可用的包吗?

【问题讨论】:

标签: email xamarin.forms phone-call


【解决方案1】:

Xamarin.Essentials (Nuget) 以预览包的形式提供,包含open the default mail app and attach information such as the recipients, subject and the bodyopen the phone dialer with a certain number 的功能。

blog.xamarin.com 上还有一个关于 Xamarin.Essentials 的 blog post

编辑: 至于您的邮件问题,Xamarin.Essentials 需要一个字符串数组作为收件人,因此您可以一次将邮件发送给多个人。只需传递一个具有单个值的字符串数组。

var recipients = new string[1] {"me@watercod.es"};

如果您使用需要 EmailMessage 实例的重载,则应该传递一个字符串对象列表。 在这种情况下,以下应该有效:

var recipients = new List<string> {"me@watercod.es"};

【讨论】:

  • 我用这个包解决了呼叫功能,但对于邮件我没有得到解决方案
  • 我用邮件代码中的发现更新了问题。
  • 我已经编辑了我的答案以回答您关于邮件问题的问题。
  • 目前我正在尝试这样: List recipients = new List();收件人.Add(email.Text);
【解决方案2】:

使用 Xamarin.Essentials 更新呼叫和邮件功能的完整代码,这可能对其他人有所帮助。

来电:

   try
    {
        PhoneDialer.Open(number);
    }
    catch (ArgumentNullException anEx)
    {
        // Number was null or white space
    }
    catch (FeatureNotSupportedException ex)
    {
        // Phone Dialer is not supported on this device.
    }
    catch (Exception ex)
    {
        // Other error has occurred.
    }

对于邮件:

       List<string> recipients = new List<string>();
        string useremail = email.Text;
        recipients.Add(useremail);
                try
                {
                    var message = new EmailMessage
                    {
                        //Subject = subject,
                        //Body = body,
                        To = recipients
                        //Cc = ccRecipients,
                        //Bcc = bccRecipients
                    };
                    await Email.ComposeAsync(message);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Exception:>>"+ex);
                }

【讨论】:

    【解决方案3】:

    您好在 UWP 中拨打电话

    if (Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.ApplicationModel.Calls.PhoneCallManager"))
                {
                    Windows.ApplicationModel.Calls.PhoneCallManager.ShowPhoneCallUI("123", "name to call");
                }
    

    发送文本:

    private async void ComposeSms(Windows.ApplicationModel.Contacts.Contact recipient,
        string messageBody,
        StorageFile attachmentFile,
        string mimeType)
    {
        var chatMessage = new Windows.ApplicationModel.Chat.ChatMessage();
        chatMessage.Body = messageBody;
    
        if (attachmentFile != null)
        {
            var stream = Windows.Storage.Streams.RandomAccessStreamReference.CreateFromFile(attachmentFile);
    
            var attachment = new Windows.ApplicationModel.Chat.ChatMessageAttachment(
                mimeType,
                stream);
    
            chatMessage.Attachments.Add(attachment);
        }
    
        var phone = recipient.Phones.FirstOrDefault<Windows.ApplicationModel.Contacts.ContactPhone>();
        if (phone != null)
        {
            chatMessage.Recipients.Add(phone.Number);
        }
        await Windows.ApplicationModel.Chat.ChatMessageManager.ShowComposeSmsMessageAsync(chatMessage);
    }
    

    在此处的 Microsoft 文档中找到:Compose SMS documentation

    ==> 所以您可以在您的 Xamarin 应用中制作(如果尚未完成)共享服务接口,然后在您的 UWP 应用中使用这些代码实现。 ..


    发送电子邮件: 要在 UWP 中发送电子邮件,您也可以参考 Microsoft 文档: Send Email documentation (UWP)


    使用插件

    您也可以使用 Xamarin 插件:

    【讨论】:

    • 我需要打电话和发邮件(不需要发短信)。目前通话和邮件仅在 android 中工作。而我们如何在IOS中进行这两个操作呢?
    • 我更新了答案:在 UWP 中发送邮件,或者对于 iOS/Android/UWP:使用插件
    【解决方案4】:

    在我们的应用程序中,我们使用 DependencyService 进行电话呼叫。

    因此在我们的 PCL 中,我们有

    public interface IPhoneCall
    {
        void Call(string number);
    }
    

    在 iOS 端,调用方法如下:

    public void Call(string number)
        {
            if (string.IsNullOrEmpty(number))
                return;
            var url = new NSUrl("tel:" + number);
            if (!UIApplication.SharedApplication.OpenUrl(url))
            {
                var av = new UIAlertView("Error",
                             "Your device does not support calls",
                             null,
                             Keys.Messages.BUTTON_OK,
                             null);
                av.Show();
            }
        }
    

    【讨论】:

      【解决方案5】:

      如果不想等待截至今天仍处于预发布状态的Xamarin essentials,您可以使用this open source plugin。它适用于 iOS、Android 和 UWP。 github文档中有一个示例:

      // Make Phone Call
      var phoneDialer = CrossMessaging.Current.PhoneDialer;
      if (phoneDialer.CanMakePhoneCall)
         phoneDialer.MakePhoneCall("+27219333000");
      
      // Send Sms
      var smsMessenger = CrossMessaging.Current.SmsMessenger;
      if (smsMessenger.CanSendSms)
         smsMessenger.SendSms("+27213894839493", "Well hello there from Xam.Messaging.Plugin");
      
      var emailMessenger = CrossMessaging.Current.EmailMessenger;
      if (emailMessenger.CanSendEmail)
      {
          // Send simple e-mail to single receiver without attachments, bcc, cc etc.
          emailMessenger.SendEmail("to.plugins@xamarin.com", "Xamarin Messaging Plugin", "Well hello there from Xam.Messaging.Plugin");
      
          // Alternatively use EmailBuilder fluent interface to construct more complex e-mail with multiple recipients, bcc, attachments etc.
          var email = new EmailMessageBuilder()
            .To("to.plugins@xamarin.com")
            .Cc("cc.plugins@xamarin.com")
            .Bcc(new[] { "bcc1.plugins@xamarin.com", "bcc2.plugins@xamarin.com" })
            .Subject("Xamarin Messaging Plugin")
            .Body("Well hello there from Xam.Messaging.Plugin")
            .Build();
      
          emailMessenger.SendEmail(email);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-05
        • 2020-04-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多