优点:
- 高效:通过资源定义对象,并在标记中的多个地方使用,精简代码
- 可维护性
资源字典
应用:多个项目之间共享资源,可创建资源字典。资源字典只是XAML文档,只存储希望使用的资源。
通常合并到Application中。定义一个或多个可重用的应用程序“皮肤”,可将“皮肤”应用到控件上。<Application x:Class="WpfPrj.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfPrj"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!--This put ResourceDictionary Source="xxx.xaml"-->
</ResourceDictionary.MergedDictionaries>
<!--This put self resource-->
</ResourceDictionary>
</Application.Resources>
</Application>
静态、动态资源
应用:重用 ```csharp<TextBlock x:Key="staticResource" Text="This is staticResouce"/>
<TextBlock x:Key="dynamicResource" Text="This is dynamicResource"/>
private void update_Click(object sender, RoutedEventArgs e) { this.Resources[“dynamicResource”] = new TextBlock() { Text = “this is update” }; }
//C#代码 string text = (string)this.FindResource(“staticResource”);
<a name="0pno6"></a>
# 二进制资源(应用程序的内嵌资源)
应用:程序的国际化、本地化
```csharp
xmlns:prop="clr-namespace:WpfPrj.Properties"
<StackPanel Margin="0,0,0,-137">
<TextBlock x:Name="textBlock" Text="{x:Static prop:Resources.Password}"/>
</StackPanel>
//C#代码
this.textBlock.Text=Properties.Resources.Password;