ContactManager ContactManager ContactManager ContactManager Class

定義

代表 Windows 通訊錄中所保存的 PeerContact 物件之集合。Represents a collection of PeerContact objects which persist in a Windows Address Book.

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

範例

下列範例程式碼說明如何列舉PeerNearMe並將其加入為PeerContact至本機ContactManager:The following example code illustrates how to enumerate a PeerNearMe and add it as a PeerContact to the local 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類別。This class has no public constructors, since a reference to it is returned by the PeerCollaboration class.

相關聯的通訊錄PeerContact管理包括在 對等主應用程式與遠端對等電腦相關聯。Address books that are associated with PeerContact management include those associated with the host peer and a remote peer. 任何一部電腦可能會與其他對等,共同作業遠端對等,但不是主對等的本機對等電腦時,可以新增至ContactManager主對等。Either computer may collaborate with other peers; when a peer is local to the remote peer but not the host peer, it can be added to the ContactManager of the host peer. 某些作業ContactManager類別,例如AddContactDeleteContact、 相互關聯到相關聯的 Windows 通訊錄的遠端對等上所執行的作業。Certain operations on the ContactManager class, such as AddContact and DeleteContact, correlate to the associated operations being performed on the Windows Address book of the remote peer.

主對等可存取此永續性儲存體PeerContact可以在其中顯示想要參與的對等的應用程式與相關聯的資訊。The host peer has access to this persistent storage for PeerContact information, which can be associated with the applications in which a peer intends to participate. APeerApplication識別本身的使用者身分ContactManager的電腦上的持續性存放區。A PeerApplication can identify itself as a user of a ContactManager persistent store on a computer.

屬性

LocalContact LocalContact LocalContact LocalContact

取得代表本機對等電腦的 PeerContactGets the PeerContact representing the local peer.

SynchronizingObject SynchronizingObject SynchronizingObject SynchronizingObject

當屬性值設定後,因非同步作業而未引發的所有事件都會有建立該特定 SynchronizingObject 之執行緒上回呼的相關事件處理常式。When this property value is set, all events not fired as the result of an asynchronous operation will have the associated event handlers called back on the thread that created the specific SynchronizingObject.

方法

AddContact(PeerContact) AddContact(PeerContact) AddContact(PeerContact) AddContact(PeerContact)

將指定的 PeerContact 加入至本機對等項目的 ContactManagerAdds the specified PeerContact to the ContactManager of the local peer.

CreateContact(PeerNearMe) CreateContact(PeerNearMe) CreateContact(PeerNearMe) CreateContact(PeerNearMe)

為指定的 PeerContact 物件,建立 PeerNearMe 執行個體。Creates a PeerContact instance for the specified PeerNearMe object.

CreateContactAsync(PeerNearMe, Object) CreateContactAsync(PeerNearMe, Object) CreateContactAsync(PeerNearMe, Object) CreateContactAsync(PeerNearMe, Object)

建立指定之 PeerNearMe 物件的連絡執行個體。Creates a contact instance for the specified PeerNearMe object.

DeleteContact(PeerContact) DeleteContact(PeerContact) DeleteContact(PeerContact) DeleteContact(PeerContact)

從本機對等項目的 ContactManager 中移除指定的 PeerContactRemoves the specified PeerContact from the ContactManager of the local peer.

DeleteContact(PeerName) DeleteContact(PeerName) DeleteContact(PeerName) DeleteContact(PeerName)

從本機對等項目的 ContactManager 中移除與指定 PeerName 相關聯的 PeerContactRemoves the PeerContact associated with the specified PeerName from the ContactManager of the local peer.

Dispose() Dispose() Dispose() Dispose()

釋放由 ContactManager 物件使用的所有資源。Releases all resources used by the ContactManager object.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetContact(PeerName) GetContact(PeerName) GetContact(PeerName) GetContact(PeerName)

傳回指定之 PeerContactPeerName 物件。Returns the PeerContact object for the specified PeerName.

GetContacts() GetContacts() GetContacts() GetContacts()

傳回 PeerContactCollection,內含遠端對等電腦 ContactManager 中的所有連絡人。Returns a PeerContactCollection that contains all contacts within the ContactManager of the remote peer.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)
UpdateContact(PeerContact) UpdateContact(PeerContact) UpdateContact(PeerContact) UpdateContact(PeerContact)

更新與 PeerContact 相關聯的資料 。Updates the data associated with the PeerContact.

事件

ApplicationChanged ApplicationChanged ApplicationChanged ApplicationChanged

會在 PeerApplication 中與 PeerContact 相關聯的 ContactManager 變更時引發。Raised whenever a PeerApplication associated with a PeerContact in the ContactManager has changed.

CreateContactCompleted CreateContactCompleted CreateContactCompleted CreateContactCompleted

會在 CreateContact(PeerNearMe) 方法完成時引發。Raised whenever a CreateContact(PeerNearMe) method has completed.

NameChanged NameChanged NameChanged NameChanged

會在 PeerName 中與 PeerContact 相關聯的 ContactManager 變更時引發。Raised whenever the PeerName associated with a PeerContact in the ContactManager has changed.

ObjectChanged ObjectChanged ObjectChanged ObjectChanged

當連絡人已註冊 PeerObject 物件內的物件變更時,就會引發這個事件。Raised whenever an object within a contact's registered PeerObject objects has changed.

PresenceChanged PresenceChanged PresenceChanged PresenceChanged

PeerContactContactManager 的顯示狀態變更時,就會引發這個事件。Raised whenever the presence status of a PeerContact in the ContactManager has changed.

SubscriptionListChanged SubscriptionListChanged SubscriptionListChanged SubscriptionListChanged

當訂閱的連絡人清單變更時,就會引發這個事件。Raised when the list of subscribed contacts changes.

適用於

另請參閱