GVKun编程网logo

Silverlight中JS访问C#代码学习(js访问servlet)

23

以上就是给各位分享Silverlight中JS访问C#代码学习,其中也会对js访问servlet进行解释,同时本文还将给你拓展MySilverlight系列(10)——Silverlight中的Ink

以上就是给各位分享Silverlight中JS访问C#代码学习,其中也会对js访问servlet进行解释,同时本文还将给你拓展My Silverlight系列(10)—— Silverlight中的InkCanvas、Silverlight 2学习教程(五):JavaScript与Silverlight托管代码相互调用、Silverlight 2学习教程(六): Silverlight托管代码调用Javascript中的JSON对象、Silverlight 2学习教程(十):Silverlight 2.0 URL 访问策略等相关知识,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

Silverlight中JS访问C#代码学习(js访问servlet)

Silverlight中JS访问C#代码学习(js访问servlet)

 Silverligh中System.Windows.browser命名空间下提供了供JS和SL后台代交互的类.

 

ScriptableTypeAttribute:指定类或者结构体能被JS访问的能力.

 

ScriptableMemberAttribute:指定一个方法,属性或者事件能被JS访问的能力.

 

HtmlPage.RegisterScriptableObject("hwj383 ",this);//如果这个类中没有可以Scriptable的对象会在运行时出异常

 

客户端通过为SL的指定一个Id如:

 

然后通过:

客户端对象.Content.注册名.方法/属性来访问;

var Plugin = document.getElementById("SilverlightPlugin");

如本例是: Plugin.Content.hwj383.JsCalltest();

 

[ScriptableMember(ScriptAlias = "AliasName")] //指定一别名,客户端通过别名来访问

 

 

 

对自定义类的访问

 

 

客户端可以通过:

 

var v = SilverlightPlugin.Content.services.createObject("hwj");//创建一个对象
v.Add(1,2);//调用其方法

 

未完,待续......

My Silverlight系列(10)—— Silverlight中的InkCanvas

My Silverlight系列(10)—— Silverlight中的InkCanvas

有许多人喜欢手写板或者涂鸦板之类的东西,而并不怎么喜欢输入法,因此Microsoft专门有Ink这个东西用于处理鼠标画图。不得不说这个东西功能十分的强大,也让许多用户使用起来非常方便,用微软开发出来的很多Ink与Bitmap结合的API,即使不会用Photoshop的人也能轻松打开一张图片,然后在自己喜欢的地方写上一段话或者签个名什么的。这个功能,Winform上面有,WPF上面也有,当然Silverlight上也有,只不过作为起步不久的Silverlight版Ink,功能尚不够强大,目前能够开放给我们使用的,只有InkPresenter这一个控件。

也许开发WPF的人都没怎么听说过这个控件而只听说过InkCanvas——那是一个在WPF上对Ink功能封装得非常完善的控件,我们可以使用它进行画图和橡皮擦等一系列的事情。当然,如果你去研究过这个控件,你就可以发现其实它其实是通过DataBinding在InkPresenter上进行了进一步的封装。由于WPF与Silverlight不同的继承结构,恐怕在Silverlight上很难照搬WPF上那一套,即我们不能对其进行一对一的Port,所以如果要在Silverlight上实现一个InkCanvas,就要另辟蹊径。

如果你使用过WPF的InkCanvas控件,你将会发现它支持EraseByPoint,EraseBystroke,Ink三种模式,而且支持复制、粘贴,而且可以轻松地扩展出撤销与重做两个功能。但是后面的一系列功能,不是InkCanvas的核心功能,只要前三者得以实现,那么这个InkCanvas就可以正常的运作了。那么,我们首先从这三种模式中用于画图的Ink模式说起。

InkCanvas的核心,其实在于它内部的InkPresenter,在Silverlight中InkPresenter仅仅是Canvas的子类,只不过它多了strokes这么一个属性用于存储和展示画上去的所有stroke。因此,它把如何生成一个stroke的问题完全留给了我们。先来看一下stroke的定义:

复制代码

  //  Summary:

    
     Represents a collection of points that correspond to a stylus-down, move,

    
     and stylus-up sequence.

     public   sealed   class  stroke : DependencyObject

    
{

        
// Summary:

        
     Initializes a new instance of the System.Windows.Ink.stroke class.

        public stroke();

        

        
     Initializes a new instance of the System.Windows.Ink.stroke class with the

        
     specified System.Windows.Input.StylusPointCollection.

        
 Parameters:

        
   stylusPoints:

        
     A System.Windows.Input.StylusPointCollection that represents the System.Windows.Ink.stroke.public stroke(StylusPointCollection stylusPoints);


        
     Gets or sets the properties of the stroke, such as System.Windows.Ink.DrawingAttributes.Height,

        
     System.Windows.Ink.DrawingAttributes.Width, System.Windows.Ink.DrawingAttributes.Color,0)">     or System.Windows.Ink.DrawingAttributes.OutlineColor.

        
 Returns:

        
     The System.Windows.Ink.DrawingAttributes of the stroke.

        public DrawingAttributes DrawingAttributes getset; }

        
     Gets or sets the stylus points of the System.Windows.Ink.stroke.

        
     The System.Windows.Input.StylusPointCollection that contains the stylus points

        
     that represent the current System.Windows.Ink.stroke.

        public StylusPointCollection StylusPoints set; }


        
     Retrieves the bounding Box for the System.Windows.Ink.stroke object.

        
     A System.Windows.Rect structure defining the bounding Box for the System.Windows.Ink.stroke

        
     object.public Rect GetBounds();

        
     Indicates whether a specified System.Windows.Input.StylusPointCollection

        
     intersects with a System.Windows.Ink.stroke object.

        
   stylusPointCollection:

        
     The System.Windows.Input.StylusPointCollection used to check for intersection

        
     with the System.Windows.Ink.stroke object.

        
     true if the specified System.Windows.Input.StylusPointCollection intersects

        
     with the System.Windows.Ink.stroke object; otherwise, false.public bool HitTest(StylusPointCollection stylusPointCollection);

    }

复制代码

其中DrawingAttributes这个属性是用于描述画笔的颜色的,而StylusPoints描述了stroke内点的集合。学过数学的人都知道,线是由点组成的,因此只要我们找到了应该插入到这个stroke中所有的点,那么生成一个新的stroke不在话下。所幸MouseEventArgs中,有一个StylusDevice只读属性,而它的一个公共方法public StylusPointCollection GetStylusPoints(UIElement relativeto)可以在鼠标事件触发的时候,得到这些“点”的集合。我们只需要为InkPresenter加上MouseLeftButtonDown,MouseMove,MouseLeftButtonUp三个handler,那么我们就可以在鼠标进行轨迹上把那些点加到线上,并将这条线加入到InkPresenter这个“面”里。代码比较多,最后我会把工程放在下面,就不一段一段的贴了。

其实这个Ink模式,不算什么难点,而后面这个EraseBystroke也相对简单,最笨的方法就是遍历InkPresenter内所有的stroke,然后一一检验它是否与我们的"Eraser"有交叉,如果有,则将它Remove。但是,最后这个EraseByPoint可没那么容易了,因为当橡皮将一条线拦腰截断的时候,不但要把擦掉的部分去掉,还要把余下的两段保留在strokes这个strokeCollection中,这才能达到一分为二的效果。我最初在实现这个功能的时候,由于设计的算法时间复杂度居高不下,造成如果相交的线过多,或者橡皮拖动太快,就会出现卡死的现象。在与微软silverlight开发小组的stefan swick交流之后,他决定实现这一功能,并且将其做成一个Custom Control。昨天他告诉我他把这个东西做好了,要我去他的Blog上下载。今天我仔细研究了他的算法,发现这个算法与我的算法有一个最大的不同之处就是:我在将一条线一分为二的过程中,完全是按照从前向后的顺序,将每个点一一挎贝并缓存,从前向后判断这个点是否被橡皮擦中,如果被擦中的话,马上生成一个新的stroke,把旧的加入strokes内,并对新的stroke进行上述相同的操作。而stefan的算法则分为了两个部分,首先从前向后把前面没有被擦中的点取出来存到一个新的stroke中,然后停止,再从后往前寻找后面的点,将没有被擦中的点加入到一个新的stroke中,直到遇到被擦中的点停止。这样的话,可以保证一个stroke可以被一分为二。

经过我的测试,执行并没有什么问题。但是由于我们向stroke中插入点,完全依赖于MouseMove事件,如果我们的鼠标移动速度过快,那么被插入的这些本就离散的点,它们之前的间隔会变得更大。这在Ink模式下不会有什么问题,但是在EraseByPoint模式下,就会因被去掉的点附近没有其他的点,而一次性擦掉很大的一段,这是由于我们在插入点和擦除的时候没有做任何的优化造成的,希望这个问题能得到解决。

大家可以到http://blogs.msdn.com/swick/archive/2008/11/30/erasing-ink-in-silverlight-2.aspx去看stefan的原文,那里提供工程原件的下载,我就不再多此一举把它上传到博客园来浪费空间了。至于上面提到的问题,如果大家有什么优化的方式和算法,希望可以告诉我们,谢谢!

---------------------------------------------------------------

Silverlight 2学习教程(五):JavaScript与Silverlight托管代码相互调用

Silverlight 2学习教程(五):JavaScript与Silverlight托管代码相互调用

要实现JavaScript调用Silverlight程序里面的托管代码,需要先在应用程序的启动(Application_Startup)事件里注册要进行访问的对象,而要从Silverlight的托管代码里访问HTML页面对象或者页面中的JavaScript,使用HtmlPageDocument/HtmlElementHtmlWindow即可。

下面,我们就以例子来说明两者相互访问的方法,代码里面有很详细的注释,这里不再累述。


Page.xaml

<UserControl x:Class="SilverlightApplication1.Page"
    xmlns="http://schemas.microsoft.com/client/2007" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <Canvas Canvas.Top="20">
            <TextBlock Canvas.Top="10" Canvas.Left="20">请输入您的姓名: </TextBlock>
            <TextBox x:Name="UserInput" Width="200" Height="30" Canvas.Top="40" Canvas.Left="20"></TextBox>
            <TextBlock x:Name="Msg" Canvas.Top="90" Canvas.Left="20" Foreground="Navy" FontSize="36"></TextBlock>
            <Button Click="Button_Click" Content="单击我" FontSize="24" Width="160" Height="60" x:Name="BtnTest" Canvas.Top="160" Canvas.Left="20"></Button>
        </Canvas>
    </Grid>
</UserControl>

Page.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
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 SilverlightApplication1
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
        }


        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string UserInputContent = this.UserInput.Text;
            if (UserInputContent.Equals(String.Empty))
            {
                UserInputContent = "Hello Silverlight World!";
            }
            else
            {
                UserInputContent = "你好," + UserInputContent;
            }
            HtmlWindow win = HtmlPage.Window;
            this.Msg.Text = UserInputContent;
            win.Alert("Silverlight 里面弹出的对话框。 " + UserInputContent);
            //执行页面中的js函数:
            win.Eval("getArraytest()");
            Object[] args = { "将此参数传递给 js 函数" };
            win.Invoke("getArrayTest", args);
            //如果页面中的值
            HtmlDocument doc = HtmlPage.Document;
            doc.GetElementById("UserName").SetAttribute("value", this.UserInput.Text);
        }

        [ScriptableMember()]
        public string InterInvole()
        {
            string username = HtmlPage.Document.GetElementById("UserName").GetAttribute("value");
            this.UserInput.Text = username;
            this.Msg.Text = "您输入了:" + username; 
            return "你从js脚本中调用了 Silverlight 里面的方法。";
        }
    }
}


App.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
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 SilverlightApplication1
{
    public partial class App : Application
    {

        public App()
        {
            this.Startup += this.Application_Startup;
            this.Exit += this.Application_Exit;
            this.UnhandledException += this.Application_UnhandledException;

            InitializeComponent();
        }

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            // Load the main control           
            Page p = new Page();
            HtmlPage.RegisterScriptableObject("SilverlightApplicationExample", p);

            // 请注意这里的定义方法,如果这里的p写成 new Page(),则Javascript基本不能给 UserInput 赋值!
            this.RootVisual = p;
        }

        private void Application_Exit(object sender, EventArgs e)
        {

        }
        private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {

        }
    }
}

SilverlightApplication1TestPage.aspx

<%@ Page Language="C#" AutoEventWireup="true" %>

<%@ Register Assembly="System.Web.Silverlight" Namespace="System.Web.UI.SilverlightControls" TagPrefix="asp" %>
<!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>Silverlight 2托管代码与Javascript交互的例子</title>
 <script type="text/javascript">
 //<!{CDATA[
 //定义全局变量:
 var testvar = "孟宪会";
 
 //定义全局函数:
 function getArraytest()
 {
  if(arguments.length > 0)
  {
   alert("js 对话框:您传递了参数。" + arguments[0]);
   return  arguments[0];
  }
  else
  {
   alert("js 对话框:无参数调用。");
   return "js 函数返回值";
  }
 } 
 
function SetUserName()
{
  alert(SilverlightPlugin.Content.SilverlightApplicationExample.InterInvole());
}

//定义Silverlight插件对象
var SilverlightPlugin = null;;
function pluginLoaded(sender)
{
   SilverlightPlugin = sender.get_element();  
}
 //]]>
 </script>
</head>
<body style="height: 100%; margin: 0;">
 <form id="form1" runat="server">
 <div style="border: 2px solid #EEE; margin: 20px;padding:20px">
  请输入你的名字:<input id="UserName" type="text" value="" />
  <input type="button" onclick="SetUserName()" value="将名字传递到 Silverlight" />
 </div>
 <br />
 <div style="border: 2px solid #EEE;margin: 20px;">
  <asp:ScriptManager ID="ScriptManager1" runat="server">
  </asp:ScriptManager>
  <asp:Silverlight ID="Xaml1" runat="server" OnPluginLoaded="pluginLoaded" Source="~/ClientBin/SilverlightApplication1.xap" Version="2.0" Width="400px" Height="300px" />
 </div>
 </form>
</body>
</html>

 

运行结果:

Silverlight 2学习教程(六): Silverlight托管代码调用Javascript中的JSON对象

Silverlight 2学习教程(六): Silverlight托管代码调用Javascript中的JSON对象

 

 

在上一篇Blog文章中,讲述了JavaScript与Silverlight托管代码相互调用的一些方法。实际上,HtmlWindow的GetProperty方法和Invoke/InvokeSelf方法的返回值是Object类型的,代表 DOM对象或者JavaScript对象(HtmlDocument、HtmlElement、HtmlObject、ScriptObject)的返回值自动作为最接近的类型进行返回,但是,程序开发人员仍然需要明确地将该对象转换成相应的类型。所有的数字,由于跨浏览器的原因,都作为Double类型返回,如果需要Int32类型,则执行Convert.ToInt32()方法即可。

在现代的Web应用中,JSON的使用越来越频繁。Silverlight 2中要调用JavaScript中的JSON对象,首先在托管代码中声明一个类,类的属性与JSON对象的属性一致(不必完全一致),然后在托管代码中将ScriptObject对象转换成声明的这个类型即可。

下面是一个完整的例子:

Page.xaml:

< UserControl  x:Class ="SilverlightApplication1.Page"
    xmlns
="http://schemas.microsoft.com/client/2007"  
    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"  
    Width
="600"  Height ="480" >
    
< Grid  x:Name ="LayoutRoot"  Background ="White" >
        
< Canvas  Canvas.Top ="20" >
            
< TextBlock  Canvas.Top ="10"  Canvas.Left ="20" > 请输入您的姓名:  </ TextBlock >
            
< TextBox  x:Name ="UserInput"  Width ="200"  Height ="30"  Canvas.Top ="40"  Canvas.Left ="20" ></ TextBox >
            
< TextBlock  x:Name ="Msg"  Canvas.Top ="90"  Canvas.Left ="20"  Foreground ="Navy"  FontSize ="18"  Width ="500" ></ TextBlock >
            
< Button  Click ="Button_Click"  Content ="单击我"  FontSize ="24"  Width ="160"  Height ="60"  x:Name ="BtnTest"  Canvas.Top ="160"  Canvas.Left ="20" ></ Button >
            
< Button  Click ="JSONButton_Click"  Content ="JavaScript JSON 对象测试"  FontSize ="24"  Width ="300"  Height ="50"  Canvas.Top ="240"  Canvas.Left ="20" ></ Button >
            
< TextBlock  x:Name ="Msg2"  Canvas.Top ="300"  Canvas.Left ="20"  Foreground ="Navy"  FontSize ="18"  Width ="500" ></ TextBlock >
            
< TextBlock  x:Name ="Msg3"  Canvas.Top ="320"  Canvas.Left ="20"  Foreground ="Navy"  FontSize ="18"  Width ="500" ></ TextBlock >
        
</ Canvas >
    
</ Grid >
</ UserControl >

Page.xaml.cs:

using  System;
using  System.Collections.Generic;
using  System.Linq;
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;
using  System.Runtime.Serialization.Json;

namespace  SilverlightApplication1
{
    
public   partial   class  Page : UserControl
    {
        
public  Page()
        {
            InitializeComponent();
        }


        
private   void  Button_Click( object  sender, RoutedEventArgs e)
        {
            
string  UserInputContent  =   this .UserInput.Text;
            
if  (UserInputContent.Equals(String.Empty))
            {
                UserInputContent 
=   " Hello Silverlight World! " ;
            }
            
else
            {
                UserInputContent 
=   " 你好, "   +  UserInputContent;
            }
            HtmlWindow win 
=  HtmlPage.Window;
            
this .Msg.Text  =  UserInputContent;
            win.Alert(
" Silverlight 里面弹出的对话框。 "   +  UserInputContent);
            
// 执行页面中的js函数:
            win.Eval( " getArraytest() " );
            Object[] args 
=  {  " 将此参数传递给 js 函数 "  };
            win.Invoke(
" getArrayTest " , args);
         
            
// 如果页面中的值
            HtmlDocument doc  =  HtmlPage.Document;
            doc.GetElementById(
" UserName " ).SetAttribute( " value " this .UserInput.Text);          
        }
        
        [ScriptableMember()]
        
public   string  InterInvole()
        {
            
string  username  =  HtmlPage.Document.GetElementById( " UserName " ).GetAttribute( " value " );
            
this .UserInput.Text  =  username;
            
this .Msg.Text  =   " 您输入了: "   +  username; 
            
return   " 你从js脚本中调用了 Silverlight 里面的方法。 " ;
        }

        
private   void  JSONButton_Click( object  sender, RoutedEventArgs e)
        {
            ScriptObject so 
=  HtmlPage.Window.Invoke( " ReturnObject " null as  ScriptObject;
            Staple s 
=  so.ConvertTo < Staple > ();
            
this .Msg2.Text  =   " 大家好,我在  JavaScript JSON 对象中的名称是: "   +   s.UserName;            
        }

        
// 接受Html页面传递的 JSON 字符串
        [ScriptableMember()]
        
public   void  ReveiveJSON( string  jsonString)
        {
            
// 注意引用:System.Runtime.Serialization.Json

            DataContractJsonSerializer json 
=   new  DataContractJsonSerializer( typeof (Staple));
            System.IO.MemoryStream ms 
=   new  System.IO.MemoryStream(System.Text.Encoding.Unicode.GetBytes(jsonString));
            Staple staple 
=   new  Staple();
            staple 
=  (Staple)json.Readobject(ms);
            Msg3.Text 
=   " UserId =  "   +  staple.UserId.ToString()  +   "  , UserName =  "   +  staple.UserName;
        }
    }

    
public   class  Staple
    {
        
public   string  UserName {  set get ; }
        
public  Double UserId {  set get ; }       
    }
}

App.xaml.cs:

using  System;
using  System.Collections.Generic;
using  System.Linq;
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  SilverlightApplication1
{
    
public   partial   class  App : Application
    {

        
public  App()
        {
            
this .Startup  +=   this .Application_Startup;
            
this .Exit  +=   this .Application_Exit;
            
this .UnhandledException  +=   this .Application_UnhandledException;

            InitializeComponent();
        }

        
private   void  Application_Startup( object  sender, StartupEventArgs e)
        {
            
//  Load the main control           
            Page p  =   new  Page();
            HtmlPage.RegisterScriptableObject(
" SilverlightApplicationExample " , p);

            
//  请注意这里的定义方法,如果这里的p写成 new Page(),则Javascript基本不能给 UserInput 赋值!
             this .RootVisual  =  p;
        }

        
private   void  Application_Exit( object  sender, EventArgs e)
        {

        }
        
private   void  Application_UnhandledException( object  sender, ApplicationUnhandledExceptionEventArgs e)
        {

        }
    }
}

SilverlightApplication1TestPage.aspx:

<% @ Page Language = " C# "  AutoEventWireup = " true "   %>

<% @ Register Assembly = " System.Web.Silverlight "  Namespace = " System.Web.UI.SilverlightControls "  TagPrefix = " asp "   %>
<! 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 > Silverlight 2托管代码与Javascript交互的例子 </ title >
 
< script  type ="text/javascript" >
 
// <!{CDATA[
  // 定义全局变量:
  var  testvar  =   " 孟宪会 " ;
 
 
// 定义全局函数:
  function  getArraytest()
 {
  
if (arguments.length  >   0 )
  {
   alert(
" js 对话框:您传递了参数。 "   +  arguments[ 0 ]);
   
return   arguments[ 0 ];
  }
  
else
  {
   alert(
" js 对话框:无参数调用。 " );
   
return   " js 函数返回值 " ;
  }
 } 
 
function  SetUserName()
{
  alert(SilverlightPlugin.Content.SilverlightApplicationExample.InterInvole());
}


 
var  Staple  =  {
               UserId:
100 ,
               UserName:
' 孟宪会 ' ,
               SayHello:
function (){alert( this .UserName)}
               };
               
function  ReturnObject()
{
  
return  Staple;
}

function  SendJSONToSilverlight()
{
  SilverlightPlugin.Content.SilverlightApplicationExample.ReveiveJSON(JSON.stringify(Staple));
}

// 定义Silverlight插件对象
var  SilverlightPlugin  =   null ;;
function  pluginLoaded(sender)
{
   SilverlightPlugin 
=  sender.get_element();  
}
 
// ]]>
  </ script >
 
< script  src ="json2.js"  type ="text/javascript" ></ script >
 
<!--  http://www.JSON.org/json2.js  -->
</ head >
< body  style ="height: 100%; margin: 0;" >
 
< form  id ="form1"  runat ="server" >
 
< div  style ="border: 2px solid #EEE; margin: 20px;padding:20px" >
  请输入你的名字:
< input  id ="UserName"  type ="text"  value =""   />
  
< input  type ="button"  onclick ="SetUserName()"  value ="将名字传递到 Silverlight"   />   < input  type ="button"  onclick ="SendJSONToSilverlight()"  value ="将JSON传递到 Silverlight"   />  
 
</ div >
 
< br  />
 
< div  style ="border: 2px solid #EEE;margin: 20px;" >
  
< asp:ScriptManager  ID ="ScriptManager1"  runat ="server" >
  
</ asp:ScriptManager >
  
< asp:Silverlight  ID ="Xaml1"  runat ="server"  OnPluginLoaded ="pluginLoaded"  Source ="~/ClientBin/SilverlightApplication1.xap"  Version ="2.0"  Width ="600px"  Height ="480px"   />
 
</ div >
 
</ form >
</ body >
</ html >

单击“JavaScript JSON 对象测试”按钮,运行结果如下:

 

Silverlight 2学习教程(十):Silverlight 2.0 URL 访问策略

Silverlight 2学习教程(十):Silverlight 2.0 URL 访问策略

由于安全原因和其他一些安全威胁,如传递Cookie,允许地址转向等,Silverlight 限制跨安全区域、跨域和跨协议的 URL访问。例如,如果你的Silverlight 程序嵌入在一个域下,而你试图使用 WebClient对象去访问保存在另外一个域下的文件,则该请求就会失败,并且这种错误提示不是该错误的准确提示。如果想要跨域访问,则需要配置 clientaccesspolicy.xml 或者 crossdomain.xml 等文件。

下表列出了Silverlight 2.0 中 URL 访问规则:

  WebClient对象 Media、images、ASX XAML 文件、Font 文件 流媒体 允许的协议 HTTP,HTTPS HTTP,HTTPS,FILE HTTP,FILE HTTP 跨协议访问 不允许 不允许 不允许 不允许来自HTTPS的访问 跨Web域访问 不允许 如果不是来自HTTPS则允许 不允许 允许 跨安全区域访问(Windows) 不允许 不允许 不允许 不允许 跨安全区域访问(Macintosh) 不允许 允许 不允许 允许 允许重定向 同域允许 允许 同域允许 不允许

关于Silverlight中JS访问C#代码学习js访问servlet的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于My Silverlight系列(10)—— Silverlight中的InkCanvas、Silverlight 2学习教程(五):JavaScript与Silverlight托管代码相互调用、Silverlight 2学习教程(六): Silverlight托管代码调用Javascript中的JSON对象、Silverlight 2学习教程(十):Silverlight 2.0 URL 访问策略的相关知识,请在本站寻找。

本文标签: