如何在基于 Silverlight 的本地应用程序之间实现通信

该 HTML 示例承载发送应用程序的两个副本和接收应用程序的一个副本。这说明接收器可以接收来自多个发送器的消息。请自行选择脚本
核心代码
<UserControl x:Class="SendingApplication.Sender"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel x:Name="LayoutRoot">
        <StackPanel Orientation="Horizontal" >
            <TextBlock Text="Sender" FontSize="20" />
            <Button x:Name="button" Click="Button_Click"
                    Height="20" Margin="20,0,0,0" />
        </StackPanel>
        <TextBlock x:Name="output" />
    </StackPanel>
</UserControl>













C#代码


由于代码麻烦vb咋不给出



using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Messaging;

namespace SendingApplication
{
    public partial class Sender : UserControl
    {
        private LocalMessageSender messageSender;

        public Sender()
        {
            InitializeComponent();
            UpdateButton();
            messageSender = new LocalMessageSender(
                "receiver", LocalMessageSender.Global);
            messageSender.SendCompleted += sender_SendCompleted;
            SendMessage("message from Sender constructor");
        }

        private int clickNumber = 1;

        private void UpdateButton()
        {
            button.Content = "send message 'click " + clickNumber + "'";
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            SendMessage("click " + clickNumber);
            clickNumber++;
            UpdateButton();
        }

        private const int MAX_ATTEMPTS = 10000;
        private int attempt = 1;

        private void SendMessage(string message)
        {
            messageSender.SendAsync(message, attempt);
        }

        private void sender_SendCompleted(object sender, SendCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                LogError(e);
                attempt++;
                if (attempt > MAX_ATTEMPTS)
                {
                    output.Text = "Could not send message.";
                    return;
                }
                SendMessage(e.Message);
                return;
            }

            output.Text =
                "Message: " + e.Message + Environment.NewLine +
                "Attempt " + (int)e.UserState + 
                " completed." + Environment.NewLine +
                "Response: " + e.Response + Environment.NewLine +
                "ReceiverName: " + e.ReceiverName + Environment.NewLine + 
                "ReceiverDomain: " + e.ReceiverDomain;

            // Reset attempt counter.
            attempt = 1;
        }

        private void LogError(SendCompletedEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine(
                "Attempt number {0}: {1}: {2}", (int)e.UserState, 
                e.Error.GetType().ToString(), e.Error.Message);
        }

    }
}











html页面代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<!-- saved from url=(0014)about:internet -->
<head>
  <title>LocalMessaging</title>

  <style type="text/css">
  html, body {
    height: 100%;
    overflow: auto;
  }
  body {
    padding: 0;
    margin: 0;
  }
  #silverlightControlHost1 {
    padding: 0;
    margin: 0;
  }
  #silverlightControlHost2 {
    padding: 0;
    margin: 0;
  }
  </style>
</head>

<body>
  <table border="10" cellpadding="10" cellspacing="10">
    <tr>
      <td>
        <div id="silverlightControlHost1">
          <object data="data:application/x-silverlight-2," 
            type="application/x-silverlight-2" 
            width="400" height="120">
            <param name="source" value="ClientBin/SendingApplication.xap"/>
            <param name="onerror" value="onSilverlightError" />
            <param name="background" value="white" />
          </object>
          <iframe style='visibility:hidden;height:0;width:0;border:0px'>
          </iframe>
        </div>
      </td>
    </tr>
    <tr>
      <td>
        <div id="Div1">
          <object data="data:application/x-silverlight-2," 
            type="application/x-silverlight-2" 
            width="400" height="120">
            <param name="source" value="ClientBin/SendingApplication.xap"/>
            <param name="onerror" value="onSilverlightError" />
            <param name="background" value="white" />
          </object>
          <iframe style='visibility:hidden;height:0;width:0;border:0px'>
          </iframe>
        </div>
      </td>
    </tr>
    <tr>
      <td>
        <div id="silverlightControlHost2">
          <object data="data:application/x-silverlight-2," 
            type="application/x-silverlight-2" 
            width="400" height="120">
            <param name="source" value="ClientBin/ReceivingApplication.xap"/>
            <param name="onerror" value="onSilverlightError" />
            <param name="background" value="white" />
          </object>
          <iframe style='visibility:hidden;height:0;width:0;border:0px'>
          </iframe>
        </div>
      </td>
    </tr>
  </table>
</body>
</html>








  
  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值