WP独立文件资源字典

来源:互联网 发布:c语言书籍排行 编辑:程序博客网 时间:2024/06/05 06:33

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

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

<ResourceDictionaryxmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">  <!-- 应该在此定义资源字典条目。-->    <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生成字典文件。











0 0
原创粉丝点击