PeerWatcher 类

定义

动态发现无线范围内的对等应用。

public ref class PeerWatcher sealed
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class PeerWatcher final
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public sealed class PeerWatcher
Public NotInheritable Class PeerWatcher
继承
Object Platform::Object IInspectable PeerWatcher
属性

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

注解

可以使用 FindAllPeersAsync 方法获取范围内所有对等方的列表。 但是, FindAllPeersAsync 方法会扫描对等方一次,然后完成。 或者,可以使用 PeerWatcher 类扫描对等方,并在找到更新时获取更新,并增量更新可用对等应用列表。 PeerWatcher 会持续扫描范围内的新对等应用,并删除过时的对等应用。 可以通过处理 已添加 事件(在找到新的对等应用时发生)和删除过时的对等应用时发生的 Removed 事件来更新对等应用列表。 PeerWatcher 将继续扫描,直到调用 Stop 方法、 PeerFinder.FindAllPeersAsyncPeerFinder.ConnectAsync 方法。

重要

对于Windows Phone 8.x 应用,从 EnumerationCompletedAddedUpdated 事件处理程序中调用 PeerFinder.ConnectAsync 将失败。 相反,请在这些事件处理程序之外调用它,例如,当用户显式选择连接到对等方时。

若要创建 PeerWatcher 类的实例,请调用 PeerFinder.Start 方法,然后调用 CreateWatcher 方法。

private PeerWatcher _peerWatcher;
private bool _peerWatcherIsRunning = false;
private bool _peerFinderStarted = false;

// The list of peers discovered by the PeerWatcher.
ObservableCollection<PeerInformation> _discoveredPeers = new ObservableCollection<PeerInformation>();
void PeerFinder_StartPeerWatcher(object sender, RoutedEventArgs e)
{
    if (!_peerFinderStarted)
    {
        // PeerFinder must be started first.
        return;
    }

    if (_peerWatcherIsRunning)
    {
        // PeerWatcher is already running.
        return;
    }

    try
    {
        if (_peerWatcher == null)
        {
            _peerWatcher = PeerFinder.CreateWatcher();

            // Add PeerWatcher event handlers. Only add handlers once.
            _peerWatcher.Added += PeerWatcher_Added;
            _peerWatcher.Removed += PeerWatcher_Removed;
            _peerWatcher.Updated += PeerWatcher_Updated;
            _peerWatcher.EnumerationCompleted += PeerWatcher_EnumerationCompleted;
            _peerWatcher.Stopped += PeerWatcher_Stopped;
        }

        // Empty the list of discovered peers.
        _discoveredPeers.Clear();

        // Start the PeerWatcher.
        _peerWatcher.Start();

        _peerWatcherIsRunning = true;
    }
    catch (Exception ex)
    {
        // Exceptions can occur if PeerWatcher.Start is called multiple times or
        // PeerWatcher.Start is called the PeerWatcher is stopping.
    }
}

protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
    if (_peerWatcher != null)
    {
        // Remove event handlers.
        _peerWatcher.Added -= PeerWatcher_Added;
        _peerWatcher.Removed -= PeerWatcher_Removed;
        _peerWatcher.Updated -= PeerWatcher_Updated;
        _peerWatcher.EnumerationCompleted -= PeerWatcher_EnumerationCompleted;
        _peerWatcher.Stopped -= PeerWatcher_Stopped;

        _peerWatcher = null;
    }
}

属性

Status

获取 PeerWatcher 对象的当前状态。

方法

Start()

开始动态搜索无线范围内的对等应用。

Stop()

停止在无线范围内动态搜索对等应用。

事件

Added

在无线范围内找到对等应用时发生。

EnumerationCompleted

扫描操作完成后发生,并且已找到无线范围内的所有对等应用。

Removed

当对等应用不再在无线范围内时发生。

Stopped

PeerWatcher 对象已停止时发生。

Updated

当无线范围内的对等应用的 DisplayNameDiscoveryData 发生更改时发生。

适用于

另请参阅