共用方式為


PeerRole 列舉

定義

描述連線到多個對等時對等應用程式的角色。

public enum class PeerRole
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
enum class PeerRole
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
public enum PeerRole
var value = Windows.Networking.Proximity.PeerRole.peer
Public Enum PeerRole
繼承
PeerRole
屬性

Windows 需求

裝置系列
Windows 10 (已於 10.0.10240.0 - for Xbox, see UWP features that aren't yet supported on Xbox 引進)
API contract
Windows.Foundation.UniversalApiContract (已於 v1.0 引進)
應用程式功能
proximity

欄位

Client 2

應用程式是多對等連線中的用戶端對等應用程式。

Host 1

應用程式是多對等連線中的主機對等應用程式。

Peer 0

應用程式是兩對等連線的一部分。

範例

// Default to the peer role.
Windows.Networking.Proximity.PeerRole appRole = Windows.Networking.Proximity.PeerRole.Peer;
bool launchedByTap = false;

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    LaunchActivatedEventArgs launchArgs = e.Parameter as LaunchActivatedEventArgs;

    if (launchArgs.Arguments.Contains("Windows.Networking.Proximity.PeerFinder:StreamSocket"))
    {
        launchedByTap = true;

        if (launchArgs.Arguments.Contains("Role=Host"))
        {
            appRole = Windows.Networking.Proximity.PeerRole.Host;
        }
        else if (launchArgs.Arguments.Contains("Role=Client"))
        {
            appRole = Windows.Networking.Proximity.PeerRole.Client;
        }
        else
        {
            appRole = Windows.Networking.Proximity.PeerRole.Peer;
        }
    }
}

bool started = false;

// Click event for "Advertise" button.
void AdvertiseForPeers(object sender, RoutedEventArgs e)
{
    if (!started)
    {
        Windows.Networking.Proximity.PeerFinder.DisplayName = DisplayNameTextBox.Text;

        if ((Windows.Networking.Proximity.PeerFinder.SupportedDiscoveryTypes &
             Windows.Networking.Proximity.PeerDiscoveryTypes.Triggered) ==
             Windows.Networking.Proximity.PeerDiscoveryTypes.Triggered)
        {
            Windows.Networking.Proximity.PeerFinder.TriggeredConnectionStateChanged +=
                TriggeredConnectionStateChanged;

            WriteMessageText("You can tap to connect a peer device that is " +
                             "also advertising for a connection.\n");
        }
        else
        {
            WriteMessageText("Tap to connect is not supported.\n");
        }

        if ((Windows.Networking.Proximity.PeerFinder.SupportedDiscoveryTypes &
             Windows.Networking.Proximity.PeerDiscoveryTypes.Browse) !=
             Windows.Networking.Proximity.PeerDiscoveryTypes.Browse)
        {
            WriteMessageText("Peer discovery using Wi-Fi Direct is not supported.\n");
        }

        // Set the peer role selected by the user.  
        if (launchedByTap)
        {
            Windows.Networking.Proximity.PeerFinder.Role = appRole;
        }
        else
        {
            switch (GetRoleFromUser())
            {
                case "Peer":
                    Windows.Networking.Proximity.PeerFinder.Role =
                        Windows.Networking.Proximity.PeerRole.Peer;
                    break;
                case "Host":
                    Windows.Networking.Proximity.PeerFinder.Role =
                        Windows.Networking.Proximity.PeerRole.Host;
                    break;
                case "Client":
                    Windows.Networking.Proximity.PeerFinder.Role =
                        Windows.Networking.Proximity.PeerRole.Client;
                    break;
            }
        }

        // Set discoveryData property with user supplied text. 
        var discData = GetDiscoveryDataFromUser();
        var writer = new Windows.Storage.Streams.DataWriter(
            new Windows.Storage.Streams.InMemoryRandomAccessStream());
        writer.WriteString(discData);
        Windows.Networking.Proximity.PeerFinder.DiscoveryData =
            writer.DetachBuffer();

        Windows.Networking.Proximity.PeerFinder.Start();
        started = true;
    }
}

// Write a message to MessageBlock on the UI thread.
private Windows.UI.Core.CoreDispatcher messageDispatcher = Window.Current.CoreWindow.Dispatcher;
async private void WriteMessageText(string message, bool overwrite = false)
{
    await messageDispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
        () =>
        {
            if (overwrite)
                MessageBlock.Text = message;
            else
                MessageBlock.Text += message;
        });
}

備註

Role屬性用於多對等應用程式連線,以識別對等應用程式是否為主機用戶端,或者對等應用程式是否參與對等連線作為對等。 對於多對等應用程式連線,您必須先設定 Role 屬性,才能呼叫 Start 方法。 如果未設定 Role 屬性,則預設值為 Peer

在多對等應用程式案例中,角色會識別要連線的應用程式功能。 主機應用程式最多可以連線到四個用戶端應用程式主機 應用程式只能探索公告為 用戶端應用程式 的應用程式。 用戶端應用程式 只能探索公告為 主機 應用程式的應用程式。 對等角色會識別兩個應用程式案例。 因此, 對等 應用程式只能探索其他 對等 應用程式。 相同的規則適用于使用點選手勢連線的對等應用程式。 例如,應用程式廣告為 主機 應用程式只能點選以與應用程式廣告作為 用戶端應用程式 連線。

適用於

另請參閱