Silverlight 中一些必须知道的技巧(zhuan tie)

Silverlight 中一些必须知道的技巧(zhuan tie),第1张

概述1,在Silverlight获取初始化参数 页面上XAML代码如下: < Grid x:Name ="LayoutRoot" Background ="White" > < ListBox Margin

1,在Silverlight获取初始化参数
页面上XAML代码如下:

          <      GrID       x:name      ="LayoutRoot"       Background      ="White"             >      
< ListBox margin ="76,68,197" x:name ="ListBox" HorizontalAlignment ="left" WIDth ="226" />
</ GrID > @H_419_69@

 用了ListBox控件,为了显示多个参数值
前台HTML代码如下:

 

                 <      object       data      ="data:application/x-silverlight,"       ID      ="XamlObject"       type      ="application/x-silverlight-2"      
wIDth
="100%" height ="100%" >
< param name ="source" value ="ClIEntBin/SilverlightTest11.0.xap" />
< param name ="onerror" value ="onSilverlightError" />
< param name ="background" value ="white" />
< param name ="minRuntimeVersion" value ="3.0.40818.0" />
< param name ="autoUpgrade" value ="true" />
< param name ="initparams" value ="ID=12343,name=silverlight学习" />
< a href ="http://go.microsoft.com/fwlink/?linkID=149156&v=3.0.40818.0" style ="text-decoration: none;" >
< img src ="http://go.microsoft.com/fwlink/?linkID=108181" alt ="获取 Microsoft Silverlight"
style
="border-style: none" />
</ a >
</ object > @H_419_69@

 

 

 

 注意:name=initParams 这个节点,后面的value 他就是sl中要获取的参数,value中的参数一般定义为value="key=value,key=value..."
C#获取初始化参数.在App.xaml的 初始化方法Application_Startup写上如下代码:

private voID Application_Startup(object sender,StartupEventArgs e)		{            MainPage main = new MainPage();            this.RootVisual = main;            foreach (string item in e.InitParams.Keys)            {                main.ListBox.Items.Add(new TextBlock()                {                    Text = String.Format("网页参数:{0} = {1}",item,e.InitParams[item])                });            }           }@H_419_69@ 

OK,效果图我就不演示啦,大家自己试试...

2,sl获取URL传递参数

   这个比较简单,主要用了HTMLPage.document.queryString[key],key就是参数的名字了@H_419_69@ 

3,silverlight 捕获一些常用的浏览器信息

            browserinformation brow = HTMLPage.browserinformation;            this.txtBlock1.Text = string.Format("浏览器名称:{0}",brow.name);            txtBlock1_copy.Text = string.Format("浏览器版本:{0}",brow.browserVersion);            txtBlock1_copy1.Text = string.Format("浏览器 *** 作系统名称:{0}",brow.Platform);            txtBlock1_copy2.Text = string.Format("代理字符串:{0}",brow.UserAgent);@H_419_69@@H_419_69@ 

4,silverlight *** 作HTML元素
  XAML代码省略
C# 代码:

   private voID button_Click(object sender,RoutedEventArgs e)        {            HTMLElement img = HTMLPage.document.GetElementByID("img11");            img.SetAttribute("wIDth",txtwIDth.Text);            img.SetAttribute("height",txtheight.Text);        }@H_419_69@ 

前台HTML代码:

          <      div      >      
< img ID ="img11" src ="silverlight.jpg" /></ div > @H_419_69@

 

通过sl代码 来动态改变前台img的尺寸

5,HTML元素 *** 作Silverlight对象

 XAML代码如下: 

 

                 <      Ellipse       x:name      ="elipse"       Fill      ="White"       stroke      ="Black"       Height      ="138"       margin      ="136,302,88"       VerticalAlignment      ="Bottom"      />      
< TextBlock x:name ="txtState" Height ="20" margin ="136,317,53" VerticalAlignment ="Bottom" textwrapPing ="Wrap" /> @H_419_69@

 

 

C# 代码:

public HTMLAndSilverlightDemo()		{			// 为初始化变量所必需			InitializeComponent();            HTMLElement select = HTMLPage.document.GetElementByID("sel1");            select.AttachEvent("onchange",new EventHandler<HTMLEventArgs>(Select_onChange));		}        public voID Select_onChange(object sender,HTMLEventArgs e)         {            HTMLElement select = sender as HTMLElement;            string value = select.GetAttribute("value");            txtState.Text = value;            switch (value)            {                case "红色":                    this.elipse.Fill = new SolIDcolorBrush(colors.Red);                    break;                case "绿色":                    this.elipse.Fill = new SolIDcolorBrush(colors.Green);                    break;                case "蓝色":                    this.elipse.Fill = new SolIDcolorBrush(colors.Blue);                    break;                default:                    break;            }        }@H_419_69@ 

HTML代码:

                 <      div      >       请选择:      <      select       ID      ="sel1"      >      
< option value ="红色" > 红色 </ option >
< option value ="绿色" > 绿色 </ option >
< option value ="蓝色" > 蓝色 </ option >
</ select ></ div > @H_419_69@

 

主要思路是,是通过前台的一个下拉列表的更改,来改变silverlight中椭圆的颜色. 大家可以根据这个思路开扩展.

6,使用httpUtility类

 UrlEncode 和UrlDecode 还有HTMLEncode和HTMLDecode 都是大家在做 Asp.net时候常用到的编码类.  在silvelight中 他在httpUtility类中可调用 具体的调用和在asp.net中一样,这里就不做具体介绍了.

7,使用document.cookies读写cookie

 为了 *** 作简单,我写了一个cookieHelp类    c#代码如下:

 #region cookie *** 作        /// <summary>        /// 添加cookie        /// </summary>        /// <param name="key"></param>        /// <param name="value"></param>        public static voID setcookie(string key,string value)        {            DateTime expire = DateTime.UtcNow + TimeSpan.FromDays(30);            string cookie = string.Format("{0}={1};expires={2}",key,value,expire.ToString("R"));            HTMLPage.document.SetProperty("cookie",cookie);        }        /// <summary>        /// 获取cookie        /// </summary>        /// <param name="key"></param>        /// <returns></returns>        public static string Getcookie(string key)        {            key += '=';            string[] cookies = HTMLPage.document.cookies.Split(';');            foreach (string cookie in cookies)            {                string cookieStr = cookie.Trim();                if (cookieStr.StartsWith(key,StringComparison.OrdinalignoreCase))                {                    //分隔出key的值                    string[] vals = cookieStr.Split('=');                    if (vals.Length >=2)                    {                        return vals[1];//返回值                    }                    return string.Empty;                }            }            //没有找到就返回空字符串            return string.Empty;        }        #endregion@H_419_69@ 

8,使用 HTMLPage.Window类 页面导航

           string url = "www.baIDu.com";            Uri uri = new Uri(url,UriKind.relativeOrabsolute);            HTMLPage.Window.Navigate(uri);@H_419_69@ 

消息提示:

  三种d出窗口:

            HTMLPage.Window.Alert("alert");            HTMLPage.Window.Confirm("你确定吗?");            HTMLPage.Window.Prompt("请输入密码");@H_419_69@ 

9,在silverlight中调用JavaScript

XAML代码:

 

          <      Canvas       Height      ="74"       margin      ="8,129,48,0"       VerticalAlignment      ="top"      >      
< TextBlock Height ="18" WIDth ="159" Canvas.left ="8" Canvas.top ="8" Text ="2,在Silverlight调用JavaScript" textwrapPing ="Wrap" />
< button Height ="28" WIDth ="111" Canvas.left ="19" Canvas.top ="39" Content ="Invoke" Click ="button_Click_2" />
< button Height ="28" WIDth ="111" Canvas.left ="151" Canvas.top ="39" Content ="InvokeSelf" Click ="button_Click_3" />
</ Canvas > @H_419_69@

 

 

 

C#:

  private voID button_Click_2(object sender,RoutedEventArgs e)        {            HTMLPage.Window.Invoke("callJs","Invokes");        }        private voID button_Click_3(object sender,RoutedEventArgs e)        {            //创建脚本            ScriptObject callJs = (ScriptObject)HTMLPage.Window.GetProperty("callJs");            callJs.InvokeSelf("InvokeSelf");        }   private voID UserControl_Loaded(object sender,RoutedEventArgs e)        {            //Js脚本            string JsText = "function callJs(msg){alert(msg);}";            //创建脚本            HTMLElement element = HTMLPage.document.CreateElement("Script");            element.SetAttribute("type","text/JavaScript");            element.SetProperty("text",JsText);                       //添加脚本到页面中            HTMLPage.document.Body.AppendChild(element);        }@H_419_69@ 

这里用了两种方法来调用Js脚本.其效果一样的. 两个方法分别是 Invoke 和InvokeSelf.

10,在JavaScript 调用Silverlight

通过创建一个矩形来演示 *** 作  C#;

public UserControl1()		{			// 为初始化变量所必需			InitializeComponent();            HTMLPage.RegisterScriptableObject("Builder",this);		}//定义这个方法为脚本成员        [ScriptableMember]        public voID CreateRect(int wIDth,int height)         {            Rectangle rect = new Rectangle();            rect.WIDth = wIDth;            rect.Height = height;            rect.Fill = new SolIDcolorBrush(colors.Blue);            this.canvase.Children.Clear();            this.canvase.Children.Add(rect);        }@H_419_69@ 

JavaScript: 

<script type="text/JavaScript"> function createRect() { var xamlObj = document.all("XamlObject"); xamlObj.content.Builder.CreateRect(document.all("txtWIDth").value,document.all("txtHeight").value); } </script> @H_419_69@ 

HTML:

 

                 <      div      >      
宽度:
< input type ="text" ID ="txtWIDth" />
高度:
< input type ="text" ID ="txtHeight" />
< input type ="button" value ="改变" onclick ="createRect()" />
</ div > @H_419_69@

 

 

 

效果. 大家ctrl+c --Ctrl+v 可以试试... 

注:以上代码需要引用System.windows.browser;这个命名空间

总结

以上是内存溢出为你收集整理的Silverlight 中一些必须知道的技巧(zhuan tie)全部内容,希望文章能够帮你解决Silverlight 中一些必须知道的技巧(zhuan tie)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1031658.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存