Silverlight20中使用多个xaml文件的解决方案_第1页
Silverlight20中使用多个xaml文件的解决方案_第2页
Silverlight20中使用多个xaml文件的解决方案_第3页
Silverlight20中使用多个xaml文件的解决方案_第4页
Silverlight20中使用多个xaml文件的解决方案_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、silverlight中多个xaml之间的切换/调用/弹岀/传参数问题小结(附实例源码) 发布时间:2009年09月10日点击数:14文章来源:互联网文章作者:未知(实例代码)s订ver light +不存在flash +的场景,有的只是一个个xaml文 件,你要是愿意,也可以把它看做场景或窗口,刚开始接触si时,对于多 个xaml之间如何切换,调用,传递参数感到很棘手,下面是我总结的儿种方法:1、a. xaml 跳转到 b. xaml(a) 首先需要定义一个公用的接口 (interface),如下: using system. windows;namespace childwin. code

2、public interface icontent uielcment content get; set; (b) a. xaml与b. xaml都实现该接口,代码如下:public partial class a: usercontrol, 【content/ 实现 icontent 接口/public new uielcment content.getreturn base. content;setbase. content 二 value;public partial class b: uscrcontrol, icontcnt/ 实现 tcontent 接口/public new ul

3、element contentgetrcturn base.content;set base. content 二 value;(c) 需要跳转的地方,类似下面这样处理:private void btnchange click (object sender,system. wi ndows. routedeventargs e)(app. current. rootvisual as icontent). content = new window2();上面的的意思是按钮btnchangc点击后,当前场景将切换到window2. xaml对 应的场景2、主xamlz,中加载子xamlz,(类似

4、软件中的mdi窗口)这个比较容易,在主xaml中放置一个容器类的控件(比如scroll viewer之类), 然后指定content就行了,参考以下代码:"viewerl" canvas. top=40 width二400 height=258private void btnload click(object sender, system. windows. routedeventargs e)if (this, viewerl. content = null)this, viewerl. content 二 new subwin();3、主xand屮以模态窗口方式弹出子x

5、aml"这个要用到sl3. 0中的childwindow控件(a) 项目中先添加system. windows. controls的引用(b) xaml文件头部加二行代码: xmlns:controls=,clr-namcspocc:system. windows. controls;assembly二system windows. controls"xmlns: v sm=/z c 1 r-name space: system .windows; nssembly 二 system .windows"(c) 要弹出的地方,参考以下代码: private void

6、 btnshowclick(object sender, system. windows. routedeventargs e)chi1dwindow win 二 new chi1dwindow(); win. title = 测试弹出窗口; win .conte nt 二 new subw in(); win. hasclosebutton 二 true;win. 0vcrlaybrush 二 new solidcolorbrush(colors. gray);win. overlayopacity = 0.3;win. width = 205;win.height 二 205;win. s

7、how();4、调用xernil吋的参数传递问题可以利用构造函数来解决,参考以下代码namespace chi1dwinpublic partial class subwin : usercontrol public subwin()initializccomponcnt();public subwin(datetime dt):this()this.calendar1. displaydate = dt;这里我添加了一个带参数的构架函数做为测试,即public subwin(datetime dt):this(),这里接受一个日期型的参数,然后把日期控件的显示值设置为该参 数,而:this0

8、的作用是调用该构架函数前,先调用无参数的构造函数,即 subwin (),这种写法在本例屮等价于: public subwin(datetime dt)initializccomponcnt();this.calendar1. displaydate = dt;重点:调用该xaml时,上面都是xxx = new subwin()来处理的,现在我们可以 用 this, viewerl. content = new subwin(datetime. parse("1979-6-5");来传 递一个参数给subwin木文来自:.net中文社区(www. aspxcs. net)详

9、细ll1处参考:http:/www. aspxcs. net/html/162302845. htmlsilverlight中多个xaml (,z场景? or 窗口?)之间的切换/调用/弹出/传参数 问题小结2009-09-18 14:57silverlight中不存在flash中的场景,有的只是一个个xaml文件,你要是愿 意,也可以把它看做场景或窗口,刚开始接触si时,对于多个xaml之间如 何切换,调用,传递参数感到很棘手,下面是我总结的几种方法:1、a. xaml 跳转到 b. xaml(a) 首先需要定义一个公用的接口 (interface),如下:using system. win

10、dows;namespace childwin. codepublic interface 【contentulelement content get; set; (b) a. xaml与b. xaml都实现该接口,代码如下:public partial class a: uscrcontrol, 【content/ <summary>/ 实现 icontent 接口/ </summary>public new ulelement contentgetreturn base .content;setbase. content = value;public partial

11、 class b: usercontrol, content/ <summary> iii 实现 icontent 接口/ </suinmary> public new uielement contentgetreturn base. content;setbase.content = value;(c) 需要跳转的地方,类似下面这样处理:private void btnchange_click(object sender,system. windows. routedeventargs e)(app.current.rootvisual as tcontent).co

12、ntent 二 new window2();上面的的意思是按钮btnchange点击后,当前场景将切换到window2. xaml对 应的场景2、主xaml"中加载子xanil类似软件中的mdi窗口)这个比较容易,在主xaml屮放置一个容器类的控件(比如scrollvicwcr z类), 然后指定content就行了,参考以下代码:<scrollviewer x:name二viewer1 canvas.top二40 width二400 height=/258,x/scrollviewer>private void btnload_click(object sendor,s

13、ystem windows routedeventargs e)if (this, viewerl. content 二二 null)this.viewer 1. content = new subwin();3、主x3nil屮以模态窗口方式弹出子xanir这个要用到s!3. 0中的childwindow控件(a) 项目中先添加system. windows. controls的引用(b) xaml文件头部加二行代码:xmlns:controls=/zclr-namespace:system. windows. controls;assembly二system .wi ndows. contr

14、ol sxmlns: vsm=,clrnamespace:system windows;assembly二system windows"(c) 要弹出的地方,参考以下代码:private void btnshow_click(object sender,system. windows. routedeventargs e)childwindow win = new childwindowo ;win. title = 测试弹出窗口;win. conte nt 二 new subwin ();win. hasclosebutton 二 true;win. 0verlaybrush =

15、new solidcolorbrush (colors.gray);win .0 vcrlayopac ity = 0.3;win.width = 205;win.height 二 205;win. show();4、调用xaml时的参数传递问题可以利用构造函数来解决,参考以下代码 namespace childwinpublic partial class subwin : usercontrolpublic subwin()initializecomponent();public subwin(datetime dt):this()this.calendarl. displaydate =

16、 dt;这里我添加了一个带参数的构架函数做为测试,即public subwin(datetime dt):thiso,这里接受一个日期型的参数,然后把日期控件的显示值设置为该参 数,而:thiso的作用是调用该构架函数前,先调用无参数的构造函数,即 subwin (),这种写法在本例中等价于: public subwin(datetime dt)initializecomponent();this.calendarl. displaydate 二 dt;重点:调用该xaml时,上面都是xxx = new subwin()来处理的,现在我们可以 用 this, viewerl. content

17、= new subwin(datetime. parse( 1979-6-5);来传 递一个参数给subwin5月27日silverlight2. 0中使用多个xaml文件的解决方案我们在开发silverlight程序的时候,如果我们把所有的程序代码都放到 page, xaml和page. xaml. cs中,必然会是page, xaml文件过于庞大,查找代码 的时候也会十分的不方便,那么我们就应该把处理每一个业务逻辑的代码分开, 放到不同的xaml中。以下是本人总结internet上部分相关资源并自己修改得出 的一种解决方案,供大家参考,转载时请注明出处 http:/dufelitianyu

18、. spaces live com/1. 建立一个名为s订verlightapplicationxaml的解决方案,里面包含一个名为 sil verl ightappl icationxaml 的 si 1 verl ight2. 0 的项目和一个名为si ivor 1 ightappl icat ionxaml_wcb 的 web 应用程序项目。2. 在 si 1 ver 1 ightapp 1 icationxaml 项目中添加新项,选择 silverlight 中的 silverlightusercontrol (注意这里不是添加的 silverlightapplication),输 入

19、名称shenyang. xaml,再添加新项,同样的方法,命名为dal ian. xaml,这样 在silverlight项目屮就存在了三个xaml文件了,接下來要做的就是通过在 page, xaml中编写程序,控制事件触发后加载的xaml文件。3. 在shenyang. xaml中创建一个path图形,并增加一段动画(例如将这个path 变大)为这个path起名为shenyang4. 同样的方法用t* dalian. xaml中。5. 然后将shenyang. xaml和dalian. xaml中的path代码(不包括动画)复制并 粘贴到page, xaml文件中。注意这三个xaml文件的长

20、和宽要一致。6. 这样,我们要实现的效果是当鼠标点击path时触发鼠标左键按下事件,通过page. xaml. cs文件中的程序控制点击不同path后加载相应的xaml文件,并在加载完成后运行加载的xaml及其.xaml. cs文件的程序,从而达到将不同的业务逻辑分离以分别处理,使项目的逻辑性清晰。7. 鼠标点击 path 时触发 private void choosearea (object sender, mousebuttoneventargs e)方法。此方法的定义如下private void choosearea(object sender, mousebuttoneventargs

21、 e)path pathl = sender as path;string pathname = path 1 .getvalue(nameproperty)<tostringo; this>choosecityxaml(pathname); private void choosecityxaml(stting chosenpathname) string typename = string. format(cultureinfo. invariantculture, s订verlightapplicationxaml. 0", chosenpathname);type

22、type = typeof (page). assembly. gettype(typename, false);if (type 二二 null)showerror(string. format(culturelnfo. in variantculture, "failed to find 0!, typename);return;try uielement element = activator. createlnstance(type) as uielement;loadcityxaml(element);catch (exception ex)showerror(string. format(culturelnfo. invariantculture, “failed to load 0 ! (1: 2), typename,ex. get type (). name, ex. message); private void showerror(string errormessage) if (string. isnullorempty(errormessage) errormessage 二"error!; loadcityxaml(new textelock tex

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论