当前位置: 移动技术网 > IT编程>移动开发>WP > Windows phone 7之XAML

Windows phone 7之XAML

2018年10月03日  | 移动技术网IT编程  | 我要评论

娇宠少爷小乖妹,管仲论坛,植物大战僵尸智慧树

就想android开发中,界面用xml来渲染一样,wp7继承xaml渲染界面,xaml遵循xml规范,所以具体规范,请大家参考xml官方文档就可以了
现在我主要说一下xaml在wp7中的用法
新建一个wp7工程,前面已经介绍过了,默认程序的主页面是mainpage.xaml,大家知道,每个xaml页面对已一个xaml.cs为后缀的文件,这是一个类文件,xaml页面与xaml.cs类结合,就想x页面与sapx.cs类结合一样,这就是所谓的代码后置,默认生成的类名和页面的名字是一样的,但这不是必须的,因为xaml页面和类文件的关联不是靠声明方式实现的,每个page类型的xaml文件开头处都很类似的有如下代码
<phone:phoneapplicationpage     x:class="xaml.mainpage"     xmlns=""     xmlns:x=""
看到了吗,x:class="xaml.mainpage" 这就是xaml与xaml.cs类关联的关键标示 x是引入的命名空间,连接地址是(一会在说命名空间的用法)class属性的功能就是制定后置代码的类
标签是构成xaml的元素,标签有开始和结束两种,一般有子控件的标签,需要结束标签,没有子控件的标签,有没有结束标签都行,如果没有,使用”/>“结束,看下面的代码
<stackpanel x:name="titlepanel" grid.row="0" margin="12,17,0,28">
            <textblock x:name="applicationtitle" text="my application" style="{staticresource phonetextnormalstyle}"/>
            <textblock x:name="pagetitle" text="page name" margin="9,-7,0,0" style="{staticresource phonetexttitle1style}"/>
        </stackpanel>

这是在页面中添加一个stackpanel,里面有2个textblock,name,grid.row和margin都是属性,textblock是子控件,也称之为孩子,或者内容,这2个textblock就是stackpanel的内容,name表示名字,跟asp.net控件中的id性质一样,后台代码获取该控件的标示,margin是编剧,上一章介绍过了。从上面的代码看出来,子控件写在父控件两个标签(开始结束)的中间,属性写在标签内部(不是只能这样,属性也是可以写在父控件的开始结束标签之间的)每个控件的属性表示什么,可以查官方文档,不过多介绍了,这里数字要说一下style,这是样式,类似于html中的css,style一般也是以xaml的形式定义在单独的资源文件,或者以resource为后缀的表标签下,比如定义在本页的resource,看一下完整代码
<phone:phoneapplicationpage
    x:class="xaml.mainpage"
    xmlns=""
    xmlns:x=""
    xmlns:phone="clr-namespace:microsoft.phone.controls;assembly=microsoft.phone"
    xmlns:shell="clr-namespace:microsoft.phone.shell;assembly=microsoft.phone"
    xmlns:d=""
    xmlns:mc=""
    mc:ignorable="d" d:designwidth="480" d:designheight="768"
    fontfamily="{staticresource phonefontfamilynormal}"
    fontsize="{staticresource phonefontsizenormal}"
    foreground="{staticresource phoneforegroundbrush}"
    supportedorientations="portrait" orientation="portrait"
    shell:systemtray.isvisible="true">
    <phone:phoneapplicationpage.resources>
        <style x:key="mytb" targettype="textblock">
            <setter property="fontsize" value="50"/>
        </style>
    </phone:phoneapplicationpage.resources>
    <!--layoutroot is the root grid where all page content is placed-->
    <grid x:name="layoutroot" background="transparent">
        <grid.rowdefinitions>
            <rowdefinition height="auto"/>
            <rowdefinition height="*"/>
        </grid.rowdefinitions>

        <!--titlepanel contains the name of the application and page title-->
        <stackpanel x:name="titlepanel" grid.row="0" margin="12,17,0,28">
            <textblock x:name="applicationtitle" text="my application" style="{staticresource phonetextnormalstyle}"/>
            <textblock x:name="pagetitle" text="page name" margin="9,-7,0,0" style="{staticresource phonetexttitle1style}"/>
        </stackpanel>

        <!--contentpanel - place additional content here-->
        <grid x:name="contentpanel" grid.row="1" margin="12,0,12,0">
            <textblock style="{staticresource mytb}" text="hello my size 50"></textblock>
        </grid>
    </grid>
 
    <!--sample code showing usage of applicationbar-->
    <!--<phone:phoneapplicationpage.applicationbar>
        <shell:applicationbar isvisible="true" ismenuenabled="true">
            <shell:applicationbariconbutton iconuri="/images/appbar_button1.png" text="button 1"/>
            <shell:applicationbariconbutton iconuri="/images/appbar_button2.png" text="button 2"/>
            <shell:applicationbar.menuitems>
                <shell:applicationbarmenuitem text="menuitem 1"/>
                <shell:applicationbarmenuitem text="menuitem 2"/>
            </shell:applicationbar.menuitems>
        </shell:applicationbar>
    </phone:phoneapplicationpage.applicationbar>-->

</phone:phoneapplicationpage>
 

我在resource标签下定义了一个key="mytb" targettype="textblock"的style,表示这个style是应用的textblock上的,引用时格式是<textblock style="{staticresource key}" />  ,setter标签用来定义属性及其值,每个属性用一个setter标签,多个属性则需要多个setter标签完成,也就是一个style可以拥有多个setter标签,mytb只声明了一个setter,用来设置字号,上述代码的运行效果如下

  \


 

看到,定义的字体大小为50,已经起作用了

关于style的高级应用和,引用外部资源,的方式会在以后的章节中做详细的描述

关于xaml的命名空间

一般自定义xaml的命名空间格式如下

xmlns:phone="clr-namespace:microsoft.phone.controls;assembly=microsoft.phone"

标示引入microsoft.phone.controls命名空间(cs类库),改命名空间属于microsoft.phone程序集(cs类库),如果不知道的命名空间,请先自学c#,phone是一个别名,随便取,上述是生成的内容,下面我们来使用自己的命名空间

项目结构如下

  \

 

testclass代码如下
using system;
using system.net;
using system.windows;
using system.windows.controls;
using system.windows.documents;
using system.windows.ink;
using system.windows.input;
using system.windows.media;
using system.windows.media.animation;
using system.windows.shapes;

namespace testclasslibrary
{
    public class testclass
    {
        public testclass()
        {
            _testtext = "hello, i'm testtext!";
        }
        private string _testtext;
        public string testtext
        {
            get
            {
                return _testtext;
            }
        }
    }
}

在xaml项目中添加testclasslibrary项目的引用,在mainpage.xaml文件中加入命名空间,并声明资源,声明什么资源就可是使用什么资源,mainpage.xaml的完整代码如下
<phone:phoneapplicationpage
    x:class="xaml.mainpage"
    xmlns=""
    xmlns:x=""
    xmlns:phone="clr-namespace:microsoft.phone.controls;assembly=microsoft.phone"
    xmlns:shell="clr-namespace:microsoft.phone.shell;assembly=microsoft.phone"
    xmlns:d=""
    xmlns:mc=""
    xmlns:test="clr-namespace:testclasslibrary;assembly=testclasslibrary"
    mc:ignorable="d" d:designwidth="480" d:designheight="768"
    fontfamily="{staticresource phonefontfamilynormal}"
    fontsize="{staticresource phonefontsizenormal}"
    foreground="{staticresource phoneforegroundbrush}"
    supportedorientations="portrait" orientation="portrait"
    shell:systemtray.isvisible="true">
    <phone:phoneapplicationpage.resources>
        <style x:key="mytb" targettype="textblock">
            <setter property="fontsize" value="50"/>
        </style>
        <test:testclass x:key="testclass"></test:testclass>
    </phone:phoneapplicationpage.resources>
    <!--layoutroot is the root grid where all page content is placed-->
    <grid x:name="layoutroot" background="transparent">
        <grid.rowdefinitions>
            <rowdefinition height="auto"/>
            <rowdefinition height="*"/>
        </grid.rowdefinitions>

        <!--titlepanel contains the name of the application and page title-->
        <stackpanel x:name="titlepanel" grid.row="0" margin="12,17,0,28">
            <textblock x:name="applicationtitle" text="my application" style="{staticresource phonetextnormalstyle}"/>
            <textblock x:name="pagetitle" text="page name" margin="9,-7,0,0" style="{staticresource phonetexttitle1style}"/>
        </stackpanel>

        <!--contentpanel - place additional content here-->
        <grid x:name="contentpanel" grid.row="1" margin="12,0,12,0">
            <textblock style="{staticresource mytb}" text="{binding path=testtext,source={staticresource testclass}}"></textblock>
        </grid>
    </grid>
 
    <!--sample code showing usage of applicationbar-->
    <!--<phone:phoneapplicationpage.applicationbar>
        <shell:applicationbar isvisible="true" ismenuenabled="true">
            <shell:applicationbariconbutton iconuri="/images/appbar_button1.png" text="button 1"/>
            <shell:applicationbariconbutton iconuri="/images/appbar_button2.png" text="button 2"/>
            <shell:applicationbar.menuitems>
                <shell:applicationbarmenuitem text="menuitem 1"/>
                <shell:applicationbarmenuitem text="menuitem 2"/>
            </shell:applicationbar.menuitems>
        </shell:applicationbar>
    </phone:phoneapplicationpage.applicationbar>-->

</phone:phoneapplicationpage>

运行结果如下

  \


 

看到,我们没有在mainpage.cs文件中写逻辑,只是使用资源绑定的方式就将testclass中的testtext显示出来了,这前提就是要声明命名空间,至于绑定的bind以后的章节会做详细的讲解 xmlns:test="clr-namespace:testclasslibrary;assembly=testclasslibrary"声明命名空间,<test:testclass x:key="testclass"></test:testclass>声明资源,<textblock style="{staticresource mytb}" text="{binding path=testtext,source={staticresource testclass}}"></textblock>使用资源

 

 

作者  董贺超

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网