共用方式為


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

已發行訊息的唯一發行集識別碼。 將此值傳遞至 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 app,只有在呼叫的應用程式位於前景時,發行集和訂閱才會作用中。

重要

鄰近 API 不提供驗證、加密或訊息完整性。 請勿使用鄰近性來交換使用者敏感性資訊,例如密碼、財務資料、簡訊、電子郵件、相片或政府識別碼。

另請參閱

適用於

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

已發行訊息的唯一發行集識別碼。

屬性

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 app,只有在呼叫的應用程式位於前景時,發行集和訂閱才會作用中。

重要

鄰近 API 不提供驗證、加密或訊息完整性。 請勿使用鄰近性來交換使用者敏感性資訊,例如密碼、財務資料、簡訊、電子郵件、相片或政府識別碼。

另請參閱

適用於