MessageTransmittedHandler 委托

定义

介绍将处理在传输已发布消息时触发的事件的方法。

public delegate void MessageTransmittedHandler(ProximityDevice ^ sender, long long messageId);
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.Guid(4020898634, 63202, 19837, 133, 108, 120, 252, 142, 252, 2, 30)]
class MessageTransmittedHandler : MulticastDelegate
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.Guid(4020898634, 63202, 19837, 133, 108, 120, 252, 142, 252, 2, 30)]
public delegate void MessageTransmittedHandler(ProximityDevice sender, long messageId);
var messageTransmittedHandlerHandler = function(sender, messageId){
/* Your code */
}
Public Delegate Sub MessageTransmittedHandler(sender As ProximityDevice, messageId As Long)

参数

sender
ProximityDevice

发布消息的邻近设备。

messageId
Int64

long long

long

消息的发布 ID。

属性

Windows 要求

设备系列
Windows 10 (在 10.0.10240.0 - for Xbox, see UWP features that aren't yet supported on Xbox 中引入)
API contract
Windows.Foundation.UniversalApiContract (在 v1.0 中引入)
应用功能
ID_CAP_PROXIMITY [Windows Phone] proximity ID_CAP_PROXIMITY [Windows Phone]

示例

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

private void MessagePublished(
    Windows.Networking.Proximity.ProximityDevice sender,
        long messageId)
    {
    // The message has been successfully published.
    }

    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,
                                       AddressOf MessagePublished)
End Sub

Private Sub MessagePublished(
sender As Windows.Networking.Proximity.ProximityDevice,
    messageId As Long)

' The message has been successfully published.
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

注解

消息将继续发布,直到调用 StopPublishingMessage 方法或发布计算机上的 ProximityDevice 释放为止。 消息将传递到订阅相同消息类型的所有应用。

使用 PublishMessage 方法时,唯一支持的消息类型是以“Windows.”开头的消息类型。 若要使用其他消息类型(如“WindowsMime.”或“NDEF:WriteTag”)发布消息,必须使用 PublishBinaryMessage 方法。

每当消息成功传输到设备时,都会调用 MessageTransmittedHandler 委托,无论该设备上是否有任何应用订阅了该消息。

适用于

另请参阅