다음을 통해 공유


PeerWatcher.Removed 이벤트

정의

피어 앱이 더 이상 무선 범위 내에 있지 않을 때 발생합니다.

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

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

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

이벤트 유형

Windows 요구 사항

앱 기능
proximity

설명

Removed 이벤트는 열거된 피어 앱의 컬렉션에서 피어 앱이 제거될 때 발생합니다. 이 문제는 사용자가 앱을 전환하거나 디바이스가 더 이상 범위 내에 있지 않은 경우에 발생할 수 있습니다.

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_Removed(PeerWatcher sender, PeerInformation peerInfo)
{
    var result = Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
    {
        lock (this)
        {
            // Find and remove the peer form the list of discovered peers.
            for (int i = 0; i < _discoveredPeers.Count; i++)
            {
                if (_discoveredPeers[i].Id == peerInfo.Id)
                {
                    _discoveredPeers.RemoveAt(i);
                }
            }
        }
    });
}

적용 대상

추가 정보