Share via


連絡人卡片

連絡人卡片會顯示連絡人 (Windows 用來代表人員和企業的機制) 的連絡資訊,例如姓名、電話號碼和地址。 連絡人卡片也可讓使用者編輯連絡資訊。 您可以選擇顯示精簡的連絡人卡片,或是包含額外資訊的完整連絡人卡片。

Important APIShowContactCard 方法ShowFullContactCard 方法IsShowContactCardSupported 方法Contact class

有兩種顯示連絡人卡片的方式:

  • 做為標準連絡人卡片,出現在的飛出視窗中,並且可消失關閉 (在使用者按一下連絡人卡片外部時消失)。
  • 做為完整連絡人卡片,佔用大部分空間,但無法消失關閉 (使用者必須按一下 [關閉] 才能將其關閉)。
Screenshot showing a standard contact card.
標準連絡人卡片
Screenshot showing a full contact card.
完整連絡人卡片

這是正確的控制項嗎?

當您想要顯示連絡人的連絡資訊時,請使用連絡人卡片。 如果您只想要顯示連絡人的名稱和圖片,請使用個人圖片控制項

顯示標準連絡人卡片

  1. 您通常會因為使用者按一下某個項目 (按鈕或者個人圖片控制項) 而顯示連絡人卡片。 我們並不想要隱藏元素。 為了避免隱藏,我們需要建立描述元素位置及大小的 Rect

    我們來建立為我們這樣做的公用程式函式,稍後會用到。

    // Gets the rectangle of the element 
    public static Rect GetElementRectHelper(FrameworkElement element) 
    { 
        // Passing "null" means set to root element. 
        GeneralTransform elementTransform = element.TransformToVisual(null); 
        Rect rect = elementTransform.TransformBounds(new Rect(0, 0, element.ActualWidth, element.ActualHeight)); 
        return rect; 
    } 
    
    
  2. 呼叫 ContactManager.IsShowContactCardSupported 方法,以判斷您是否可以顯示連絡人卡片。 如果不支援,則會顯示錯誤訊息 (此範例假設您要顯示連絡人卡片來回應按一下事件)。

    // Contact and Contact Managers are existing classes 
    private void OnUserClickShowContactCard(object sender, RoutedEventArgs e) 
    { 
        if (ContactManager.IsShowContactCardSupported()) 
        { 
    
    
  3. 使用您在步驟 1 建立的公用程式函式,取得引發事件的控制項範圍 (這樣連絡人卡片就不會蓋住它)。

            Rect selectionRect = GetElementRect((FrameworkElement)sender); 
    
  4. 取得您要顯示的 Contact 物件。 此範例只是建立簡單的連絡人,但您的程式碼應該擷取實際的連絡人。

                // Retrieve the contact to display
                var contact = new Contact(); 
                var email = new ContactEmail(); 
                email.Address = "jsmith@contoso.com"; 
                contact.Emails.Add(email); 
    
  5. 呼叫 ShowContactCard 方法顯示連絡人卡片。

            ContactManager.ShowFullContactCard(
                contact, selectionRect, Placement.Default); 
        } 
    } 
    

以下是完整的程式碼範例:

// Gets the rectangle of the element 
public static Rect GetElementRect(FrameworkElement element) 
{ 
    // Passing "null" means set to root element. 
    GeneralTransform elementTransform = element.TransformToVisual(null); 
    Rect rect = elementTransform.TransformBounds(new Rect(0, 0, element.ActualWidth, element.ActualHeight)); 
    return rect; 
} 
 
// Display a contact in response to an event
private void OnUserClickShowContactCard(object sender, RoutedEventArgs e) 
{ 
    if (ContactManager.IsShowContactCardSupported()) 
    { 
        Rect selectionRect = GetElementRect((FrameworkElement)sender);

        // Retrieve the contact to display
        var contact = new Contact(); 
        var email = new ContactEmail(); 
        email.Address = "jsmith@contoso.com"; 
        contact.Emails.Add(email); 
    
        ContactManager.ShowContactCard(
            contact, selectionRect, Placement.Default); 
    } 
} 

顯示完整連絡人卡片

若要顯示完整的連絡人卡片,請呼叫 ShowFullContactCard 方法,而不是 ShowContactCard

private void onUserClickShowContactCard() 
{ 
   
    Contact contact = new Contact(); 
    ContactEmail email = new ContactEmail(); 
    email.Address = "jsmith@hotmail.com"; 
    contact.Emails.Add(email); 
 
 
    // Setting up contact options.     
    FullContactCardOptions fullContactCardOptions = new FullContactCardOptions(); 
 
    // Display full contact card on mouse click.   
    // Launch the People’s App with full contact card  
    fullContactCardOptions.DesiredRemainingView = ViewSizePreference.UseLess; 
     
 
    // Shows the full contact card by launching the People App. 
    ContactManager.ShowFullContactCard(contact, fullContactCardOptions); 
} 

擷取「真實」的連絡人

本文中的範例會建立簡單的連絡人。 在實際應用程式中,您可能會想要擷取現有的連絡人。 如需相關指示,請參閱連絡人和行事曆