今日看了Charles Petzold【美】著作的《Windows Phone 7程序设计》一书中的资源集合,特此做笔记。
在Page.xaml中添加资源集合时,
似下面这样
<phone:PhoneApplicationPage
x:Class="XamlTest.Page2"
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:system="clr-namespace:System;assembly=mscorlib"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
shell:SystemTray.IsVisible="True">
<!--Resource标签范围内的资源集合是一个资源片段,并且在这个特定的PhoneApplicationPage中的所有元素都可以使用这些资源-->
<phone:PhoneApplicationPage.Resources>
<!--共享画刷,只能在这个page中使用-->
<LinearGradientBrush x:Key="brush">
<GradientStop Offset="0" Color="Pink"/>
<GradientStop Offset="1" Color="SkyBlue"/>
</LinearGradientBrush>
<Thickness x:Key="margin">
12 96
</Thickness>
<!--声明一个XML命名空间 xmlns:system="clr-namespace:System;assembly=mscorlib"-->
<system:Double x:Key="fontsize">
48
</system:Double>
</phone:PhoneApplicationPage.Resources>
<!--LayoutRoot 是放置所有頁面的根資料格-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel 包含應用程式的名稱和頁面標題-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="我的應用程式" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="资源集合" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - 其他內容置於此-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Button Content="GoBack" Height="72" HorizontalAlignment="Left" Margin="290,6,0,0" Name="button1" VerticalAlignment="Top" Width="160" Click="button1_Click" >
</Button>
<!--FontSize="{StaticResource fontsize}"-->
<TextBlock Height="67" FontSize="{StaticResource fontsize}" HorizontalAlignment="Left" Margin="12,120,0,0" Name="textBlock1" Text="使用本地资源" VerticalAlignment="Top" Width="305" >
<TextBlock.Foreground>
<StaticResource ResourceKey="brush"/>
</TextBlock.Foreground>
</TextBlock>
<TextBlock Height="104" HorizontalAlignment="Left" Margin="26,250,0,0" Name="textBlock2" Text="使用App资源" VerticalAlignment="Top" Foreground="{StaticResource bb}" Width="291" />
</Grid>
</Grid>
<!--顯示 ApplicationBar 使用方式的程式碼範例-->
<!--<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
<shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="按鈕 1"/>
<shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="按鈕 2"/>
<shell:ApplicationBar.MenuItems>
<shell:ApplicationBarMenuItem Text="功能表項目 1"/>
<shell:ApplicationBarMenuItem Text="功能表項目 2"/>
</shell:ApplicationBar.MenuItems>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>-->
</phone:PhoneApplicationPage>
注意上面的 <phone:PhoneApplicationPage.Resources><!--**********--></phone:PhoneApplicationPage.Resources>是写在
<phone:PhoneApplicationPage
x:Class="XamlTest.Page2"
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:system="clr-namespace:System;assembly=mscorlib"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
shell:SystemTray.IsVisible="True">
之下的,如果写在XAML文件后面,像下面这样是不行的:
〈!-- 其它的XAML语句在这上面--〉
<!--Resource标签范围内的资源集合是一个资源片段,并且在这个特定的PhoneApplicationPage中的所有元素都可以使用这些资源-->
<phone:PhoneApplicationPage.Resources>
<!--共享画刷,只能在这个page中使用-->
<LinearGradientBrush x:Key="brush">
<GradientStop Offset="0" Color="Pink"/>
<GradientStop Offset="1" Color="SkyBlue"/>
</LinearGradientBrush>
<Thickness x:Key="margin">
12 96
</Thickness>
<!--声明一个XML命名空间 xmlns:system="clr-namespace:System;assembly=mscorlib"-->
<system:Double x:Key="fontsize">
48
</system:Double>
</phone:PhoneApplicationPage.Resources>
</phone:PhoneApplicationPage>
如果你运行一下程序,看看结果(报告出错了...)发生了 XamlParseException异常,因为解释器解释这个文件时,是从文件头开始读取的,一直到文件尾...所以如果把资源定义的代码写在文件尾时,而在这些代码之前就使用了这些资源的话,则会出错!!!(这些资源定义代码是在Page2.xaml中,所以在加载Page2.xaml页面时就出错了)
WP7手机操作系统发展好不好,且看明朝
本文纯属笔记文章,无什么技术含金量,是用来交流的