代码之家  ›  专栏  ›  技术社区  ›  Sreejith Sree

xamarin表单:拨打电话和发送电子邮件(IOS、Android和UWP)

  •  1
  • Sreejith Sree  · 技术社区  · 6 年前

    目前正在使用以下代码来实现通话和电子邮件功能,但它只在安卓系统中工作,在IOS系统中不工作。此外,我需要UWP中的这些功能。

    电话:

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

    邮寄:

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

    有这个的套餐吗?

    5 回复  |  直到 6 年前
        1
  •  3
  •   Marcel Kirchhoff    6 年前

    Xamarin.Essentials ( Nuget )作为预览软件包提供,并包含对两者都适用的功能 open the default mail app and attach information such as the recipients, subject and the body 以及 open the phone dialer with a certain number .

    还有一个 blog post 关于Xamarin。在博客上提供基本信息。沙马林。通用域名格式。

    编辑: 至于你的邮件问题,沙马林。Essentials需要一组字符串作为收件人,这样您就可以同时向多个人发送邮件。只需传递一个带有单个值的字符串数组。

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

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

    var recipients = new List<string> {"me@watercod.es"};
    
        2
  •  1
  •   Sreejith Sree    6 年前

    使用Xamarin更新呼叫和邮寄功能的完整代码。重要的是,这可能会帮助其他人。

    电话:

       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
  •  0
  •   KFactory    6 年前

    你好 在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插件:

        4
  •  0
  •   Markus Michel    6 年前

    在我们的应用程序中,我们使用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
  •  0
  •   Daniel    6 年前

    如果你不想等待 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);
    }