語言

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
實作

範例

以下範例程式碼說明如何枚舉 a PeerNearMe 並將其作為 a 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 類別上的某些操作,如 AddContact 和 DeleteContact,會與遠端對端的 Windows 通訊錄執行的相關操作相關聯。

主機節點可存取此持久儲存 PeerContact 資訊,這些資訊可與其打算參與的應用程式相關聯。 A PeerApplication 可以自我識別為電腦上持久儲存的使用者 ContactManager 。

屬性

名稱 Description
LocalContact

會得到代表本地節點的 。PeerContact

SynchronizingObject

當此屬性值被設定時,所有未因非同步操作觸發的事件,相關事件處理程序都會被回調至產生特定 SynchronizingObject的執行緒。

方法

名稱 Description
AddContact(PeerContact)

將指定 PeerContact 值加到本地節點的 上 ContactManager 。

CreateContact(PeerNearMe)

為指定的PeerContact物件建立一個PeerNearMe實例。

CreateContactAsync(PeerNearMe, Object)

為指定 PeerNearMe 物件建立一個聯絡實例。

DeleteContact(PeerContact)

移除本地對等節點的指定PeerContactContactManager值。

DeleteContact(PeerName)

移除 PeerContact 與指定 PeerNameContactManager 相關的 與本地節點相關的部分。

Dispose()

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

Equals(Object)

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

(繼承來源 Object)
GetContact(PeerName)

回傳 PeerContact 指定的 PeerName物件。

GetContacts()

回傳 PeerContactCollection 包含遠端對等端所有聯絡人 的 ContactManager a。

GetHashCode()

做為預設哈希函式。

(繼承來源 Object)
GetType()

取得目前實例的 Type。

(繼承來源 Object)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
ToString()

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

(繼承來源 Object)
UpdateContact(PeerContact)

更新與 PeerContact相關資料。

事件

名稱 Description
ApplicationChanged

每當PeerApplication與某PeerContactContactManager人相關時,都會被提起。

CreateContactCompleted

當方法 CreateContact(PeerNearMe) 完成時即升起。

NameChanged

每當 PeerName 與 a PeerContactContactManager 的關聯改變時,都會被提升。

ObjectChanged

每當聯絡人註冊 PeerObject 物件內的物件發生變化時,該訊息會被提出。

PresenceChanged

每當存在PeerContactContactManager狀態改變時,都會被提出。

SubscriptionListChanged

當訂閱聯絡人名單變動時會被提出。

適用於

另請參閱