次の方法で共有


Message examples

The following examples show how messages are sent between the Visual Studio Tools add-in and the Silverlight code running on the web client. These examples are messages used for the Salesperson Analysis sample.

Sending a string value from the server

This example shows a message that sends a string value for the Salesperson ID from the Visual Studio Tools add-in on the server to the Silverlight application running on the client. The first line of the example checks a variable that was set based on whether the web client is being used. Notice how the value was encoded so that it can be sent as a byte array.

The following code is the C# version of this example.

if (IsWebClient == true)
{
    // Send the selected salesperson ID to the web client
    // Convert the string value to encoded bytes to send
    byte[] bytedata;
    System.Text.UTF8Encoding val = new UTF8Encoding();
    bytedata = val.GetBytes(SalespersonMaintenanceForm.RmSalesperson.
    SalespersonId.Value);

    // Send the byte data to the web client
    proxy.SendMessage(new WCVstCustomMessage("SalespersonAnalysis.GPAddIn",
    "SalespersonAnalysisSL.GPWCAddIn", "TransferSalespersonID", bytedata));
}

The following code is the Visual Basic version of this example.

If IsWebClient = True Then
    ' Send the selected salesperson ID to the web client
    ' Convert the string value to encoded bytes to send
    Dim bytedata() As Byte
    Dim val As System.Text.UTF8Encoding = New UTF8Encoding()
    bytedata = val.GetBytes(SalespersonMaintenanceForm.RmSalesperson. _
    SalespersonId.Value)

    ' Send the byte data to the web client
    proxy.SendMessage(New WCVstCustomMessage("SalespersonAnalysis.GPAddIn", _
    "SalespersonAnalysisSL.GPWCAddIn", "TransferSalespersonID", bytedata))
End If

Receiving a string value on the web client

This example shows how the string message sent from the server is processed in the Silverlight code on the web client. Notice how the Description property of the message is examine to determine whether the message should be processed. The byte stream from the Data property is converted back into a string value, and the string is used to set the value of a text box in the Silverlight application.

The following code is the C# version of this example.

public void ProcessMessage(WCVstCustomMessage message)
{
    if (message.Description == "TransferSalespersonID")
    {
        // Convert the byte array data back into a string
        string s = System.Text.Encoding.UTF8.GetString(message.Data, 0,
        message.Data.Length);

        // Set the window field value
        saWindow.textBoxSalespersonID.Text = s;
    }
}

The following code is the Visual Basic version of this example.

Public Sub ProcessMessage(message As WCVstCustomMessage) Implements IWCCustomAddIn.ProcessMessage

    If message.Description = "TransferSalespersonID" Then
        ' Convert the byte array data back into a string
        Dim s As String = System.Text.Encoding.UTF8.GetString(message.Data, _
        0, message.Data.Length)

        ' Set the window field value
        saWindow.textBoxSalespersonID.Text = s
    End If
End If