PeerWatcher.Added 事件

定义

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

// Register
event_token Added(TypedEventHandler<PeerWatcher, PeerInformation const&> const& handler) const;

// Revoke with event_token
void Added(event_token const* cookie) const;

// Revoke with event_revoker
PeerWatcher::Added_revoker Added(auto_revoke_t, TypedEventHandler<PeerWatcher, PeerInformation const&> const& handler) const;
public event TypedEventHandler<PeerWatcher,PeerInformation> Added;
function onAdded(eventArgs) { /* Your code */ }
peerWatcher.addEventListener("added", onAdded);
peerWatcher.removeEventListener("added", onAdded);
- or -
peerWatcher.onadded = onAdded;
Public Custom Event Added As TypedEventHandler(Of PeerWatcher, PeerInformation) 

事件类型

Windows 要求

应用功能
proximity

注解

重要

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

通过调用 Start方法启动 PeerWatcher后,将针对在无线范围内找到的每个对等应用引发 Addedevent。 PeerWatcher会继续扫描范围内的对等应用,直到调用 Stop方法。 使用 Addedevent,可以在找到用户时向用户显示对等应用。

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;
    }
}
private void PeerWatcher_Added(PeerWatcher sender, PeerInformation peerInfo)
{
    var result = Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
    {
        lock (this)
        {
            _discoveredPeers.Add(peerInfo);
        }
    });
}

适用于

另请参阅