連絡先カード

連絡先カードには、Contact (個人や企業を表すために Windows によって使用されるメカニズム) で使用されている名前、電話番号、住所などの連絡先情報が表示されます。 連絡先カードを使用して、ユーザーは連絡先情報を編集することもできます。 コンパクトな連絡先カードを表示するか、追加の情報を含む完全な連絡先カードを表示することができます。

重要な API: ShowContactCard メソッドShowFullContactCard メソッドIsShowContactCardSupported メソッドContact クラス

連絡先カードを表示する方法は 2 つあります。

  • 簡易非表示に対応したポップアップに表示される標準的な連絡先カード -- ユーザーが連作先カードの外部をクリックすると、連絡先カードは消えます。
  • 多くのスペースを使用し、簡易非表示に対応していない完全な連絡先カード -- 閉じるにはユーザーが [閉じる] をクリックする必要があります。
標準の連絡先カードを示すスクリーンショット。
標準の連絡先カード
連絡先の完全なカードを示すスクリーンショット。
完全な連絡先カード

これは適切なコントロールですか?

連絡先の連絡先情報を表示する場合は、連絡先カードを使用します。 連絡先の名前と画像のみを表示する場合は、ユーザー画像コントロール を使用します。

標準の連絡先カードの表示

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

完全な連絡先カードの表示

完全な連絡先カードを表示するには、ShowContactCard メソッドではなく ShowFullContactCard メソッドを呼び出します。

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

"実際の” 連絡先の取得

この記事の例では、単純な連絡先を作成します。 実際のアプリでは、おそらく既存の連絡先を取得します。 手順については、連絡先とカレンダーの記事をご覧ください。