• [UWP]UWP中获取联系人/邮件发送/SMS消息发送操作


    这篇博客将介绍如何在UWP程序中获取联系人/邮件发送/SMS发送的基础操作。

    1. 获取联系人

    UWP中联系人获取需要引入Windows.ApplicationModel.Contacts名称空间。

    ContactStore contactStore =
            await ContactManager.RequestStoreAsync(ContactStoreAccessType.AllContactsReadOnly);
    
    IReadOnlyList<Contact> contacts = await contactStore.FindContactsAsync();

    NOTE:

    1). 获取的联系人列表是当前OS关联的微软账户(Hotmail/Outlook/Live邮箱)的联系人。

    2). 获取联系人需要在Package.appxmanifest中将Contacts勾选上。

    另外UWP提供了ContactPicker控件用来选取一个或者多个联系人,

    ContactPicker contactPicker = new ContactPicker();
    
    contactPicker.SelectionMode = ContactSelectionMode.Fields;
    
    contactPicker.DesiredFieldsWithContactFieldType.Add(ContactFieldType.Email);
    contactPicker.DesiredFieldsWithContactFieldType.Add(ContactFieldType.Address);
    contactPicker.DesiredFieldsWithContactFieldType.Add(ContactFieldType.PhoneNumber);
    
    //Select one or more contacts
    IList<Contact> contacts = await contactPicker.PickContactsAsync();
    
    if (contacts != null &&
        contacts.Count > 0)
    {
        foreach (Contact contact in contacts)
        {
            // TODO:
        }
    }
    
    //// Select only one contact
    //Contact contact = await contactPicker.PickContactAsync();
    
    //if (contact != null)
    //{
    //      //TODO:...
    //}

    使用ContactPicker获取联系人时,不需要在Package.appxmanifest中勾选Contacts。

    2. 邮件发送

    private async void SendMail(Contact recipient, StorageFile attchmentFile)
    {
        // Windows.ApplicationModel.Email
        EmailMessage message = new EmailMessage();
        // Mail subject
        message.Subject = "This is a test mail.";
    
        message.Body = "This is a test mail, please ignore.";
    
        if(attchmentFile != null)
        {
            RandomAccessStreamReference stream = 
                RandomAccessStreamReference.CreateFromFile(attchmentFile);
    
            EmailAttachment attachment = new EmailAttachment(attchmentFile.Name, stream);
    
            // Set mail's attachment
            message.Attachments.Add(attachment);
        }
    
        ContactEmail email = recipient.Emails.FirstOrDefault();
    
        if(email != null)
        {
            EmailRecipient emailRecipient = new EmailRecipient(email.Address);
    
            message.To.Add(emailRecipient);
        }
    
        await EmailManager.ShowComposeNewEmailAsync(message);
    }

    3. SMS消息发送

    private async void ComposeSms(Contact recipient,
            string messageBody,
            StorageFile attachmentFile,
            string mimeType)
    {
        var chatMessage = new Windows.ApplicationModel.Chat.ChatMessage();
    
        chatMessage.Body = messageBody;
    
        if (attachmentFile != null)
        {
            var stream = 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);
    }

    感谢您的阅读。

  • 相关阅读:
    hibernate更新
    让hibernate支持mysql的text或者longtext
    hibernate的查询
    微博获取原图时重定向到图片的url
    java判断字符串编码
    爬取微博存入数据库时出错
    循环监听事件出错,只能监听最后一个
    org.hibernate.AssertionFailure: null id don't flus
    eclipse有时候导入了包,但是还是有红线,找不到
    hibernate注解创建表总是失败,显示表不存在
  • 原文地址:https://www.cnblogs.com/yang-fei/p/6170155.html
Copyright © 2020-2023  润新知