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,用来设置字号,上述代码的运行效果如下

Windows phone 7之XAML

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

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

关于xaml的命名空间

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

xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"

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

项目结构如下

Windows phone 7之XAML

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="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"
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>

运行结果如下

Windows phone 7之XAML

看到,我们没有在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>使用资源

 

我的新浪微博昵称是”@马蔬菜“,多多关注,谢谢

转载于:https://www.cnblogs.com/xiaogeer/archive/2012/04/05/2433574.html