Share via


PeerWatcher.Added Evento

Definición

Se produce cuando se encuentra una aplicación del mismo nivel dentro del intervalo inalámbrico.

// 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) 

Tipo de evento

Requisitos de Windows

Características de aplicaciones
proximity

Comentarios

Importante

Para Windows Phone aplicaciones 8.x, se producirá un error al llamar a PeerFinder.ConnectAsync desde un controlador de eventos agregado. En su lugar, llámalo fuera de este controlador de eventos, por ejemplo, cuando el usuario haya elegido explícitamente conectarse a un elemento del mismo nivel.

Después de iniciar peerWatcherllamando al método Start, se genera el evento Addedevent para cada aplicación del mismo nivel que se encuentra dentro del intervalo inalámbrico. PeerWatchercontinúa examinando las aplicaciones del mismo nivel dentro del intervalo hasta que se llama al método Stop. Con addedevent, puede mostrar las aplicaciones del mismo nivel al usuario a medida que se encuentran.

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);
        }
    });
}

Se aplica a

Consulte también