ProximityDevice.SubscribeForMessage(String, MessageReceivedHandler) Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Crée un abonnement pour un type de message spécifié.
public:
virtual long long SubscribeForMessage(Platform::String ^ messageType, MessageReceivedHandler ^ messageReceivedHandler) = SubscribeForMessage;
long SubscribeForMessage(winrt::hstring const& messageType, MessageReceivedHandler const& messageReceivedHandler);
public long SubscribeForMessage(string messageType, MessageReceivedHandler messageReceivedHandler);
function subscribeForMessage(messageType, messageReceivedHandler)
Public Function SubscribeForMessage (messageType As String, messageReceivedHandler As MessageReceivedHandler) As Long
Paramètres
- messageType
-
String
Platform::String
winrt::hstring
Type de message à remettre à cet abonnement.
- messageReceivedHandler
- MessageReceivedHandler
Gestionnaire que le fournisseur de proximité appellera lorsqu’il remet un message.
Retours
ID unique de l’abonnement.
Configuration requise pour Windows
Fonctionnalités de l’application |
ID_CAP_PROXIMITY [Windows Phone]
proximity
ID_CAP_PROXIMITY [Windows Phone]
|
Exemples
Windows.Networking.Proximity.ProximityDevice proximityDevice;
long publishedMessageId = -1;
long subscribedMessageId = -1;
private void initializeProximityDevice()
{
proximityDevice = Windows.Networking.Proximity.ProximityDevice.GetDefault();
if (proximityDevice != null)
{
PublishMessageButton.Click += PublishMessage;
SubscribeForMessageButton.Click += SubscribeForMessage;
StopSubscribingForMessageButton.Click += StopSubscribingForMessage;
StopPublishingMessageButton.Click += StopPublishingMessage;
}
else
{
WriteMessageText("Failed to initialized proximity device.\n");
}
}
private void PublishMessage(object sender, RoutedEventArgs e)
{
// Stop publishing the current message.
if (publishedMessageId != -1)
{
proximityDevice.StopPublishingMessage(publishedMessageId);
}
publishedMessageId =
proximityDevice.PublishMessage("Windows.SampleMessage", MessageTextBlock.Text);
}
private void SubscribeForMessage(object sender, RoutedEventArgs e)
{
// Only subscribe for the message one time.
if (subscribedMessageId == -1)
{
subscribedMessageId =
proximityDevice.SubscribeForMessage("Windows.SampleMessage", messageReceived);
}
}
private void messageReceived(
Windows.Networking.Proximity.ProximityDevice device,
Windows.Networking.Proximity.ProximityMessage message)
{
MessageBlock.Text += "Message received: " + message.DataAsString + "\n";
}
private void StopSubscribingForMessage(object sender, RoutedEventArgs e)
{
proximityDevice.StopSubscribingForMessage(subscribedMessageId);
subscribedMessageId = -1;
}
private void StopPublishingMessage(object sender, RoutedEventArgs e)
{
proximityDevice.StopPublishingMessage(publishedMessageId);
publishedMessageId = -1;
}
// Write a message to MessageBlock on the UI thread.
private Windows.UI.Core.CoreDispatcher messageDispatcher = Window.Current.CoreWindow.Dispatcher;
async private void WriteMessageText(string message, bool overwrite = false)
{
await messageDispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
() =>
{
if (overwrite)
MessageBlock.Text = message;
else
MessageBlock.Text += message;
});
}
Private proximityDevice As Windows.Networking.Proximity.ProximityDevice
Private publishedMessageId As Long = -1
Private subscribedMessageId As Long = -1
Private Sub initializeProximityDevice()
proximityDevice = Windows.Networking.Proximity.ProximityDevice.GetDefault()
If proximityDevice IsNot Nothing Then
AddHandler PublishMessageButton.Click, AddressOf PublishMessage
AddHandler SubscribeForMessageButton.Click, AddressOf SubscribeForMessage
AddHandler StopSubscribingForMessageButton.Click, AddressOf StopSubscribingForMessage
AddHandler StopPublishingMessageButton.Click, AddressOf StopPublishingMessage
Else
WriteMessageText("Failed to initialized proximity device." & vbTab)
End If
End Sub
Private Sub PublishMessage(sender As Object, e As RoutedEventArgs)
' Stop publishing the current message.
If publishedMessageId <> -1 Then
proximityDevice.StopPublishingMessage(publishedMessageId)
End If
publishedMessageId =
proximityDevice.PublishMessage("Windows.SampleMessage", MessageTextBlock.Text)
End Sub
Private Sub SubscribeForMessage(sender As Object, e As RoutedEventArgs)
' Only subscribe for the message one time.
If subscribedMessageId = -1 Then
subscribedMessageId =
proximityDevice.SubscribeForMessage("Windows.SampleMessage", AddressOf messageReceived)
End If
End Sub
Private Sub messageReceived(
device As Windows.Networking.Proximity.ProximityDevice,
message As Windows.Networking.Proximity.ProximityMessage)
MessageBlock.Text &= "Message received: " & message.DataAsString & vbTab
End Sub
Private Sub StopSubscribingForMessage(sender As Object, e As RoutedEventArgs)
proximityDevice.StopSubscribingForMessage(subscribedMessageId)
subscribedMessageId = -1
End Sub
Private Sub StopPublishingMessage(sender As Object, e As RoutedEventArgs)
proximityDevice.StopPublishingMessage(publishedMessageId)
publishedMessageId = -1
End Sub
' Write a message to MessageBlock on the UI thread.
Private Async Sub WriteMessageText(message As String, Optional overwrite As Boolean = False)
Await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
Sub()
If overwrite Then
MessageBlock.Text = message
Else
MessageBlock.Text &= message
End If
End Sub)
End Sub
Remarques
Une fois que votre application a appelé la méthode SubscribeForMessage, elle reçoit les messages publiés avec le même type de message (comme indiqué par le paramètre messageType ) de tous les appareils de proximité. Pour plus d’informations sur les différents types de messages, consultez les remarques de la rubrique de référence PublishBinaryMessage(String, IBuffer).
Vous pouvez annuler un abonnement en passant l’ID d’abonnement retourné par la méthode SubscribeForMessage à la méthode StopSubscribingForMessage .
Vous pouvez publier un message sur un abonné à l’aide de la méthode PublishMessage, PublishBinaryMessage ou PublishUriMessage .
Important
Les API de proximité ne fournissent pas d’authentification, de chiffrement ou d’intégrité des messages. N’utilisez pas la proximité pour échanger des informations sensibles sur les utilisateurs, telles que des mots de passe, des données financières, des sms, des e-mails, des photographies ou des numéros d’identification gouvernementaux.