PeerWatcher.Added 事件
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
在無線範圍內找到對等應用程式時發生。
// 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);
}
});
}