繁体   English   中英

xamarin形式:打电话和发送电子邮件(IOS,Android和UWP)

[英]xamarin forms: Calling phone and sending email (IOS, Android and UWP)

当前使用以下代码实现呼叫和电子邮件功能,但仅在Android中有效,而在IOS中无效。 另外,我在UWP中需要这些功能。

致电:

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

对于邮件:

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

有可用的软件包吗?

Xamarin.EssentialsNuget )可作为预览包使用,它具有以下功能: 打开默认的邮件应用程序并附加诸如收件人,主题和正文之类的信息 ,以及使用特定号码打开电话拨号程序

在blog.xamarin.com上还有关于Xamarin.Essentials的博客文章

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

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

如果您正在使用期望一个EmailMessage实例的重载,则应该传递一个字符串对象列表。 在这种情况下,应执行以下操作:

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

使用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);
                }

您好: 在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文档中找到: 撰写SMS文档

==> 因此,您可以在Xamarin应用程序中创建 (如果尚未完成) 共享服务接口 ,然后在UWP应用程序中使用这些代码实现...


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


使用插件

另外,您可以使用Xamarin插件:

在我们的应用程序中,我们正在使用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();
        }
    }

如果不想等待截止到今天仍在预发布中的Xamarin要点 ,可以使用此开源插件 它适用于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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM