ProximityDevice.PublishMessage 方法

定义

重载

PublishMessage(String, String)

将消息发布到指定消息类型的订阅者。

PublishMessage(String, String, MessageTransmittedHandler)

将消息发布到指定消息类型的订阅者。 传输消息后,将调用指定的处理程序。

PublishMessage(String, String)

将消息发布到指定消息类型的订阅者。

public:
 virtual long long PublishMessage(Platform::String ^ messageType, Platform::String ^ message) = PublishMessage;
/// [Windows.Foundation.Metadata.Overload("PublishMessage")]
long PublishMessage(winrt::hstring const& messageType, winrt::hstring const& message);
[Windows.Foundation.Metadata.Overload("PublishMessage")]
public long PublishMessage(string messageType, string message);
function publishMessage(messageType, message)
Public Function PublishMessage (messageType As String, message As String) As Long

参数

messageType
String

Platform::String

winrt::hstring

要传递给订阅者的消息类型。

message
String

Platform::String

winrt::hstring

要传递给订阅者的消息。

返回

Int64

long long

long

已发布消息的唯一发布 ID。 将此值传递给 StopPublishingMessage 方法以停止发布消息。

属性

Windows 要求

应用功能
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);
}

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

注解

可以使用 SubscribeForMessage 方法订阅已发布的消息。

消息将继续发布,直到调用 StopPublishingMessage 方法或释放 ProximityDevice 对象为止。

消息将传递到订阅消息类型 (的所有应用程序,如 messageType 参数) 所示。 消息类型值是区分大小写的字符串,由两部分组成: 协议 和子类型。 协议先是,后跟一个点 (.) ,然后是子类型。 对于 PublishMessage 方法, 协议 必须始终为“Windows”。 子类型是字母数字字符和以下附加字符的字符串: . ( ) + , - : = @ ; $ _ ! * '子类型长度不能超过 250 个字符。

若要使用其他消息类型(如“WindowsMime.”或“NDEF:WriteTag”)发布消息,必须使用 PublishBinaryMessage 方法。

可以使用 PublishUriMessage 方法将统一资源标识符 (URI) 发布到近似计算机。

消息在传输之前会转换为 UTF-8 字符。

对于 UWP 应用,仅当调用应用位于前台时,发布和订阅才处于活动状态。

重要

邻近感应 API 不提供身份验证、加密或消息完整性。 不要使用邻近感应来交换用户敏感信息,如密码、财务数据、短信、电子邮件、照片或政府 ID 号。

另请参阅

适用于

PublishMessage(String, String, MessageTransmittedHandler)

将消息发布到指定消息类型的订阅者。 传输消息后,将调用指定的处理程序。

public:
 virtual long long PublishMessage(Platform::String ^ messageType, Platform::String ^ message, MessageTransmittedHandler ^ messageTransmittedHandler) = PublishMessage;
/// [Windows.Foundation.Metadata.Overload("PublishMessageWithCallback")]
long PublishMessage(winrt::hstring const& messageType, winrt::hstring const& message, MessageTransmittedHandler const& messageTransmittedHandler);
[Windows.Foundation.Metadata.Overload("PublishMessageWithCallback")]
public long PublishMessage(string messageType, string message, MessageTransmittedHandler messageTransmittedHandler);
function publishMessage(messageType, message, messageTransmittedHandler)
Public Function PublishMessage (messageType As String, message As String, messageTransmittedHandler As MessageTransmittedHandler) As Long

参数

messageType
String

Platform::String

winrt::hstring

要传递给订阅者的消息类型。

message
String

Platform::String

winrt::hstring

要传递给订阅者的消息。

messageTransmittedHandler
MessageTransmittedHandler

传输消息时要调用的处理程序。

返回

Int64

long long

long

已发布消息的唯一发布 ID。

属性

Windows 要求

应用功能
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

注解

可以使用 SubscribeForMessage 方法订阅已发布的消息。

消息将继续发布,直到调用 StopPublishingMessage 方法或释放 ProximityDevice 对象为止。

消息将传递到订阅消息类型 (的所有应用程序,如 messageType 参数) 所示。 消息类型值是区分大小写的字符串,由两部分组成: 协议 和子类型。 协议先是,后跟一个点 (.) ,然后是子类型。 对于 PublishMessage 方法, 协议 必须始终为“Windows”。 子类型是字母数字字符和以下附加字符的字符串: . ( ) + , - : = @ ; $ _ ! * '子类型长度不能超过 250 个字符。

若要使用其他消息类型(如“WindowsMime.”或“NDEF:WriteTag”)发布消息,必须使用 PublishBinaryMessage 方法。

可以使用 PublishUriMessage 方法将统一资源标识符 (URI) 发布到近似计算机。

消息在传输之前会转换为 UTF-8 字符。

对于 UWP 应用,仅当调用应用位于前台时,发布和订阅才处于活动状态。

重要

邻近感应 API 不提供身份验证、加密或消息完整性。 不要使用邻近感应来交换用户敏感信息,如密码、财务数据、短信、电子邮件、照片或政府 ID 号。

另请参阅

适用于