当前位置:编程学习 > wap >>

Windows phone 7之XAML

就想Android开发中,界面用xml来渲染一样,WP7继承XAML渲染界面,XAML遵循xml规范,所以具体规范,请大家参考xml官方文档就可以了
现在我主要说一下XAML在wp7中的用法
新建一个wp7工程,前面已经介绍过了,默认程序的主页面是MainPage.xaml,大家知道,每个xaml页面对已一个xaml.cs为后缀的文件,这是一个类文件,xaml页面与xaml.cs类结合,就想aspx页面与sapx.cs类结合一样,这就是所谓的代码后置,默认生成的类名和页面的名字是一样的,但这不是必须的,因为xaml页面和类文件的关联不是靠声明方式实现的,每个page类型的xaml文件开头处都很类似的有如下代码
<phone:PhoneApplicationPage     x:Class="Xaml.MainPage"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
看到了吗,x:Class="Xaml.MainPage" 这就是xaml与xaml.cs类关联的关键标示 x是引入的命名空间,连接地址是http://schemas.microsoft.com/winfx/2006/xaml(一会在说命名空间的用法)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="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    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,用来设置字号,上述代码的运行效果如下

 &nb

补充:移动开发 , Windows Phone ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,