<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="test.Index" %> <!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"> <head runat="server"> <title></title> <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script> <script src="Silverlight.js" type="text/javascript"></script> <script type="text/javascript"> function onSilverlightError(sender, args) { var appSource = ""; if (sender != null && sender != 0) { appSource = sender.getHost().Source; } var errorType = args.ErrorType; var iErrorCode = args.ErrorCode; if (errorType == "ImageError" || errorType == "MediaError") { return; } var errMsg = "Silverlight 应用程序中未处理的错误 " + appSource + "\n"; errMsg += "代码: " + iErrorCode + " \n"; errMsg += "类别: " + errorType + " \n"; errMsg += "消息: " + args.ErrorMessage + " \n"; if (errorType == "ParserError") { errMsg += "文件: " + args.xamlFile + " \n"; errMsg += "行: " + args.lineNumber + " \n"; errMsg += "位置: " + args.charPosition + " \n"; } else if (errorType == "RuntimeError") { if (args.lineNumber != 0) { errMsg += "行: " + args.lineNumber + " \n"; errMsg += "位置: " + args.charPosition + " \n"; } errMsg += "方法名称: " + args.methodName + " \n"; } 引发新错误(errMsg); } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> <ContentTemplate> <input type="button" class="btn-search" value="查询" id="btnSearch" /> <div style="width: 100%; height: 700px"> <object id="SL" data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="700px"> <param name="source" value="ClientBin/SL.xap" /> <param name="onError" value="onSilverlightError" /> <param name="background" value="white" /> <param name="minRuntimeVersion" value="4.0.60310.0" /> <param name="autoUpgrade" value="true" /> <param name="windowless" value="true" /> <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.60310.0" style="text-decoration: none"> <img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="获取 Microsoft Silverlight" style="border-style: none" /> </a> </object> </div> </ContentTemplate> </asp:UpdatePanel> </div> </form> </body> </html> <script type="text/javascript" language="javascript"> $(document).ready(function () { $("#btnSearch").click(function () { getMess(); }); }); function getMess() { $.post("Index.aspx", { "action": "search", "param": "level" }, function (data) { setTimeout(function () { var slHost = document.getElementById("SL"); var page = slHost.Content.Page; page.process(data); }, 1000); }); } </script>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Text; namespace test { public partial class Index : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (Request["action"] != null) { if (Request["param"] != null) { System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer(); StringBuilder sb = new StringBuilder(); sb.Append(Request["param"].ToString()); string strs = jss.Serialize(sb.ToString()); Response.Write(sb.ToString()); Response.End(); } } else { } } } } }
<UserControl x:Class="SL.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400"> <Grid x:Name="LayoutRoot" Background="White"> </Grid> </UserControl>
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.Windows.Browser; namespace SL { public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); HtmlPage.RegisterScriptableObject("Page", this); } [ScriptableMember] public void process(string arg) { if (arg.Length > 0) { MessageBox.Show(arg); } } } }