日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2024/10/8 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何在基于 Silverlight 的本地应用程序之间实现通信 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
http://msdn.microsoft.com/zh-cn/library/dd833075(VS.95).aspx 在兩個Silverlight應用間數據通信(包括與Flash通信) 如何在基于 Silverlight 的本地應用程序之間實現通信

下面的代碼示例演示如何使用 LocalMessageSender 和 LocalMessageReceiver 類。

在 XAML 中,Receiver 類定義 TextBlock 以便顯示它接收的消息。Sender 類定義 Button 以便發送消息,并且定義 TextBlock 以便顯示它接收的響應。

在代碼隱藏中,Receiver 類初始化 LocalMessageReceiver 并處理 MessageReceived 事件。Sender 類初始化 LocalMessageSender 并為發送消息提供自定義處理。這使得發送器可以反復發送消息,直到接收器響應。這在某些情況下很有用,例如在接收應用程序的加載時間較長時。

發送器將嘗試次數隨消息一起傳遞到 SendAsync(String, Object) 方法調用。在接收器最終響應時,發送器將該嘗試次數與響應一起顯示。

該 HTML 示例承載發送應用程序的兩個副本和接收應用程序的一個副本。這說明接收器可以接收來自多個發送器的消息。

?

代碼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);
}

}
}

?

代碼 using System;
using System.Windows.Controls;
using System.Windows.Messaging;

namespace ReceivingApplication
{
public partial class Receiver : UserControl
{
public Receiver()
{
InitializeComponent();

LocalMessageReceiver messageReceiver
=
new LocalMessageReceiver("receiver",
ReceiverNameScope.Global, LocalMessageReceiver.AnyDomain);
messageReceiver.MessageReceived
+= messageReceiver_MessageReceived;
try
{
messageReceiver.Listen();
}
catch (ListenFailedException)
{
output.Text
= "Cannot receive messages." + Environment.NewLine +
"There is already a receiver with the name 'receiver'.";
}
}

private void messageReceiver_MessageReceived(
object sender, MessageReceivedEventArgs e)
{
e.Response
= "response to " + e.Message;
output.Text
=
"Message: " + e.Message + Environment.NewLine +
"NameScope: " + e.NameScope + Environment.NewLine +
"ReceiverName: " + e.ReceiverName + Environment.NewLine +
"SenderDomain: " + e.SenderDomain + Environment.NewLine +
"Response: " + e.Response;
}
}
}

?

?

代碼 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<html >
<!-- 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>

?

轉載于:https://www.cnblogs.com/virusswb/articles/1664394.html

總結

以上是生活随笔為你收集整理的如何在基于 Silverlight 的本地应用程序之间实现通信的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。