xamarin 表单:调用 phone 并发送电子邮件(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 ));
是否有可用的软件包?
你好 在 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 插件:
Xamarin.Essentials (Nuget) is available as a preview package and contains functionality to both open the default mail app and attach information such as the recipients, subject and the body as well as open 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"};
在我们的应用中,我们正在使用 DependencyService 进行 phone 调用。
因此在我们的 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 essentials that is still in pre-release as of today, you can use 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);
}
使用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);
}
目前正在使用以下代码进行通话和电子邮件功能,但它仅适用于 Android,不适用于 IOS。另外,我需要 UWP 中的这些功能。
来电:
string phoneno = "1234567890";
Device.OpenUri(new Uri("tel:" + phoneno));
对于邮件:
string email = "sreejithsree139@gmail.com";
Device.OpenUri(new Uri("mailto:" + email ));
是否有可用的软件包?
你好 在 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 插件:
Xamarin.Essentials (Nuget) is available as a preview package and contains functionality to both open the default mail app and attach information such as the recipients, subject and the body as well as open 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"};
在我们的应用中,我们正在使用 DependencyService 进行 phone 调用。
因此在我们的 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 essentials that is still in pre-release as of today, you can use 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);
}
使用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);
}