欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Windows Phone 7 开发探索笔记8——加载XAML文件中的对象

程序员文章站 2024-01-06 20:41:34
 介绍了如何在silverlight for windows phone中读取xml文件,本文来看一下如何从xaml文件中读取信息。 一.准备xaml文件   &...

 介绍了如何在silverlight for windows phone中读取xml文件,本文来看一下如何从xaml文件中读取信息。

一.准备xaml文件

    有时候我们需要加载一些来自文件,资源中的ui元素,例如保存在xaml文件中的ui元素。首先来准备一个待读取的xaml文件,我们可以通过创建一个文本文件并将其扩展名改为.xaml的方式来创建,但更好的方式是在visual studio中创建,方法如下:右击解决方案资源管理器中的项目选择添加新项,选择任意一个扩展名为.xaml的项均可,然后将自动生成的内容清空,添加如下的代码:

ellipse.xaml文件中的xaml代码:

代码
<ellipse xmlns=""
         xmlns:x=""
         verticalalignment="center"
         horizontalalignment="center"
         height="350" width="350" >
    <ellipse.fill>
        <radialgradientbrush  gradientorigin="0.4,0.4" center="0.4 0.4">
            <gradientstop color="white" offset="0"/>
            <gradientstop color="blue" offset="1"/>
        </radialgradientbrush>
    </ellipse.fill>
</ellipse>
rectangle.xaml文件中的xaml代码:

代码
<rectangle xmlns=""
         xmlns:x=""
         verticalalignment="center"
         horizontalalignment="center"
           height="300" width="350">
    <rectangle.fill>
        <lineargradientbrush startpoint="0 0.5" endpoint="1 0.5">
            <gradientstop color="azure" offset="0"/>
            <gradientstop color="red" offset="0.5"/>
            <gradientstop color="orange" offset="1"/>
        </lineargradientbrush  >
    </rectangle.fill>
</rectangle>
我分别创建了两个xaml文件,其中一个是圆形,一个是矩形,设置好它们相应的属性即可,注意要添加相应的xml名称空间(xmlns)。在visual studio中创建的好处是可以得到设计器的帮助,看到实时的效果,当然也可以使用expression blend来做。最后,要注意把visual studio自动创建的code-behind文件删掉。

二.解析xaml文件

    要解析xaml文件中的信息,需要借助于xamlreader类,它为分析xaml和创建相应的silverlight对象树提供xaml处理器引擎。在silverlight for windows phone中它只包含一个静态的load方法,此方法用来分析一段格式良好的xaml片段并创建相应的silverlight对象树,然后返回这个对象树的根。load方法接受一个string类型的参数,我们可以将xaml文件中的代码直接传给它,不过在实际项目中,通常不会硬编码,所以采用与上篇文章中相同的方法,application类的getresourcestream方法,代码如下:

代码
        t loadxamlandgetshape<t>(string url)
        {
            string xaml = string.empty;
            streamresourceinfo sri = application.getresourcestream(new uri(url, urikind.relative));
            using (streamreader sr = new streamreader(sri.stream))
            {
                xaml = sr.readtoend();
            }
            t shape = (t)xamlreader.load(xaml);
            return shape;
        }
这里我创建了一个泛型方法,以便返回不同类型的对象。在方法中,通过application.getresourcestream方法加载xaml文件,然后用streamreader将它读成字符串,最后将字符串传入xamlreader的load方法来构造相应的类型。

三.使用读取到的silverlight对象
读取到相应的对象后就可以进行*的操作了,我在程序中添加了2个按钮用来在页面中呈现两个不同的对象,完整代码如下:

代码
using system;
using system.io;
using system.windows;
using system.windows.markup;
using system.windows.resources;
using system.windows.shapes;
using microsoft.phone.controls;

namespace windowsphoneloadxaml
{
    public partial class mainpage : phoneapplicationpage
    {
        ellipse ellipse;

        rectangle rectangle;

        // constructor
        public mainpage()
        {
            initializecomponent();

            ellipse = loadxamlandgetshape<ellipse>("/windowsphoneloadxaml;component/ellipse.xaml");

            rectangle = loadxamlandgetshape<rectangle>("/windowsphoneloadxaml;component/rectangle.xaml");
        }

        t loadxamlandgetshape<t>(string url)
        {
            string xaml = string.empty;

            streamresourceinfo sri = application.getresourcestream(new uri(url, urikind.relative));

            using (streamreader sr = new streamreader(sri.stream))
            {
                xaml = sr.readtoend();
            }

            t shape = (t)xamlreader.load(xaml);

            return shape;
        }

        private void btnellipse_click(object sender, routedeventargs e)
        {
            if (contentpanel.children.contains(rectangle))
            {
                this.contentpanel.children.remove(rectangle);
            }
            if (ellipse != null && !contentpanel.children.contains(ellipse))
            {
                this.contentpanel.children.add(ellipse);
            }
        }

        private void btnrectangle_click(object sender, routedeventargs e)
        {
            if (contentpanel.children.contains(ellipse))
            {
                this.contentpanel.children.remove(ellipse);
            }
            if (rectangle != null && !contentpanel.children.contains(rectangle))
            {
                this.contentpanel.children.add(rectangle);
            }
        }
    }
}
下面是程序截图:

 

四.下载示例代码:

 
 

作者:金山崟霸