ContactManager 類別

定義

代表 Windows 通訊錄中所保存的 PeerContact 物件之集合。

public ref class ContactManager sealed : IDisposable
public sealed class ContactManager : IDisposable
type ContactManager = class
    interface IDisposable
Public NotInheritable Class ContactManager
Implements IDisposable
繼承
ContactManager
實作

範例

下列範例程式碼說明如何列舉 , PeerNearMe 並將其新增為 PeerContact 本機 ContactManager

// Displays all contacts and asssociated peer endpoints (PeerEndPoint) in the PeerContactCollection.
private static void DisplayContacts(PeerContactCollection peerContactsCollection)
{
    if (peerContactsCollection == null ||
        peerContactsCollection.Count == 0)
    {
        Console.WriteLine("No contacts to display. To add a contact select option 0 from the menu.");
    }
    else
    {
        foreach (PeerContact pc in peerContactsCollection)
        {
            Console.WriteLine("The contact is: {0}", pc.DisplayName);
            DisplayEndpoints(pc.PeerEndPoints);
        }
    }
    return;
}

//------------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------
// Displays all peer end points (PeerEndPoint) in the PeerEndPointCollection.

private static void DisplayEndpoints(PeerEndPointCollection endpointCollection)
{
    if (endpointCollection == null ||  endpointCollection.Count == 0)
    {
        Console.WriteLine("No peer endpoints in the collection to display.");
    }
    else
    {
        foreach (PeerEndPoint pep in endpointCollection)
        {
            Console.WriteLine("PeerEndPoint is: {0}", pep);
            Console.WriteLine("PeerEndPoint data is:\n  Name: {0}\n EndPoint IP address: {1}\n . Port: {2}\n",
                pep.Name,
                pep.EndPoint.Address,
                pep.EndPoint.Port);
        }
    }
    return;
}

//------------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------
//List PeerNearMe objects that may be added as contacts.

private static void AddContact()
{
    PeerNearMeCollection pnmc = null;
    PeerContactCollection peerContacts = null;
    bool peerNameFound = false;

    PeerApplication application = null;

    try
    {
        Console.WriteLine("Listing the existing contacts...");
        peerContacts = PeerCollaboration.ContactManager.GetContacts();
    }
    catch (PeerToPeerException p2pEx)
    {
        Console.WriteLine("The Peer Collaboration Infrastructure is not responding to the contact enumeration request: {0}", p2pEx.Message);
    }
    catch (Exception ex)
    {
        Console.WriteLine("An unexpected error occurred while attempting to obtain the contact list: {0}", ex.Message);
    }

    DisplayContacts(peerContacts);

    try
    {
        //Adds one of the PeerNearMe objects as a contact.
        pnmc = GetPeersNearMe();
        Console.WriteLine("Please enter the nickname of the peer you wish to add as a contact:");
        string peerNameToAdd = Console.ReadLine();

        application = RegisterCollabApp();

        foreach (PeerNearMe pnm in pnmc)
        {
            PeerInvitationResponse res = null;
            if (pnm.Nickname.Contains(peerNameToAdd))
            {
                peerNameFound = true;
                if (!peerContacts.ToString().Contains(pnm.Nickname))
                {
                    Console.WriteLine("Adding peer {0} to the contact list.", pnm.Nickname);
                    pnm.AddToContactManager();
                }
                else
                {
                    Console.WriteLine("This peer already exists in your contact list.");
                    Console.WriteLine("Sending invitation using the Contact structure instead of the PeerNearMe.");
                    foreach (PeerContact pc in peerContacts)
                    {
                        if (pc.Nickname.Equals(pnm.Nickname))
                        {
                            res = pnm.Invite(application, "Peer Collaboration Sample", application.Data);
                            if (res.PeerInvitationResponseType == PeerInvitationResponseType.Accepted)
                            {
                                Console.WriteLine("Invitation to contact succeeded.");
                            }
                            else
                            {
                                Console.WriteLine("Invitation to contact {0}.", res.PeerInvitationResponseType);
                            }
                        }
                    }
                }
            }
        }

        if (!peerNameFound)
        {
            Console.WriteLine("No such peer exists near you. Cannot add to contacts.");
            return;
        }

        peerContacts = PeerCollaboration.ContactManager.GetContacts();

        Console.WriteLine("Listing the contacts again...");
        DisplayContacts(peerContacts);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Error adding a contact: {0}", ex.Message);
    }
    finally
    {
        application.Dispose();
    }
    return;
}

備註

這個類別沒有公用建構函式,因為 類別會傳 PeerCollaboration 回它的參考。

與管理相關聯的 PeerContact 通訊錄包括與主機對等和遠端對等相關聯的通訊錄。 任一部電腦可能會與其他對等共同作業;當對等是遠端對等的本機,但不是主機對等時,可以將它新增至 ContactManager 主機對等的 。 類別上的 ContactManager 某些作業,例如 AddContactDeleteContact ,會與遠端對等Windows通訊錄上執行的相關聯作業相互關聯。

主機對等可存取此永續性儲存體以取得 PeerContact 資訊,可以與對等想要參與的應用程式相關聯。 PeerApplication可以將自己識別為電腦上永續性存放區的使用者 ContactManager

屬性

LocalContact

取得代表本機對等電腦的 PeerContact

SynchronizingObject

當屬性值設定後,因非同步作業而未引發的所有事件都會有建立該特定 SynchronizingObject 之執行緒上回呼的相關事件處理常式。

方法

AddContact(PeerContact)

將指定的 PeerContact 加入至本機對等項目的 ContactManager

CreateContact(PeerNearMe)

為指定的 PeerContact 物件,建立 PeerNearMe 執行個體。

CreateContactAsync(PeerNearMe, Object)

建立指定之 PeerNearMe 物件的連絡執行個體。

DeleteContact(PeerContact)

從本機對等項目的 ContactManager 中移除指定的 PeerContact

DeleteContact(PeerName)

從本機對等項目的 ContactManager 中移除與指定 PeerName 相關聯的 PeerContact

Dispose()

釋放由 ContactManager 物件使用的所有資源。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetContact(PeerName)

傳回指定之 PeerContactPeerName 物件。

GetContacts()

傳回 PeerContactCollection,內含遠端對等電腦 ContactManager 中的所有連絡人。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)
UpdateContact(PeerContact)

更新與 PeerContact 相關聯的資料 。

事件

ApplicationChanged

會在 PeerApplication 中與 PeerContact 相關聯的 ContactManager 變更時引發。

CreateContactCompleted

會在 CreateContact(PeerNearMe) 方法完成時引發。

NameChanged

會在 PeerName 中與 PeerContact 相關聯的 ContactManager 變更時引發。

ObjectChanged

當連絡人已註冊 PeerObject 物件內的物件變更時,就會引發這個事件。

PresenceChanged

PeerContactContactManager 的顯示狀態變更時,就會引發這個事件。

SubscriptionListChanged

當訂閱的連絡人清單變更時,就會引發這個事件。

適用於

另請參閱