WP独立文件资源字典

 


 

有时候,开发者想把资源字典定义在单独的XAML文件中,在网上找了找,没找到比较详细的,自己弄了许久。

 

1. 新建xaml资源字典文件,如在根目录下“ResourceDictionary1.xaml”,也可以建在文件夹中,只要路径对应就可以了。 

 

<ResourceDictionary
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/TextStyle;component/ResourceDictionary1.xaml"/>
  </ResourceDictionary.MergedDictionaries>
  <!-- 应该在此定义资源字典条目。-->
    <Style x:Key="TextBlockStyle"
           TargetType="TextBlock">
        <Setter Property="Foreground"
                        Value="#474747" />
        <Setter Property="FontSize"
                        Value="40" />
        <Setter Property="FontFamily"
                        Value="{StaticResource PhoneFontFamilyNormal}" />
        <Setter Property="HorizontalAlignment"
                        Value="Left" />
        <Setter Property="VerticalAlignment"
                        Value="Top" />
    </Style>

</ResourceDictionary>



2. 在"App.xaml"下添加以下代码。

 

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="ResourceDictionary1.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>


这样就可以用的地方加上

Style="{StaticResource TextBlockStyle}"

就可以了。

 


当然,大家也可以用Blend4生成字典文件。











 

posted on 2013-12-11 10:32  我的小人生  阅读(218)  评论(0编辑  收藏  举报