有時候,開發者想把資源字典定義在單獨的XAML文件中,在網上找了找,沒找到比較詳細的,自己弄了許久。
1. 新建xaml資源字典文件,如在根目錄下“ResourceDictionary1.xaml”,也可以建在文件夾中,只要路徑對應就可以了。
<ResourceDictionary xmlns="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生成字典文件。