光会到来 发表于 2024-2-25 12:07:36

WPF资源管理:窥探外部、窗体、全局和动态资源的秘密!

概述:WPF中的资源管理机制包括外部资源、窗体资源、全局资源和动态资源。通过这些资源,开发者能够在应用程序中有效地组织、重用样式和模板,实现灵活的设计和运行时更改。这四种资源类型分别适用于不同的应用场景,为WPF应用程序提供了强大的扩展性和可维护性。
在WPF(Windows Presentation Foundation)中,资源是一种重要的机制,用于管理和重用在应用程序中使用的元素。这些资源可以分为外部资源、窗体资源、全局资源和动态资源。
1. 外部资源

外部资源是存储在独立的XAML文件中的资源,可以在应用程序中引用和重用。使用外部资源的主要步骤如下:
步骤:


[*]创建外部资源文件(例如,ExternalResource.xaml):

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="ExternalBackgroundBrush" Color="LightGray"/>
</ResourceDictionary>
[*]在应用程序中引用外部资源:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="ExternalBackgroundBrush" Color="LightGray"/>
</ResourceDictionary>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="ExternalBackgroundBrush" Color="LightGray"/>
</ResourceDictionary> 2. 窗体资源

窗体资源是在窗体内部定义的资源,仅在该窗体中可用。这对于特定窗体的样式和模板非常有用。
示例:


<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="ExternalBackgroundBrush" Color="LightGray"/>
</ResourceDictionary>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="ExternalBackgroundBrush" Color="LightGray"/>
</ResourceDictionary>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="ExternalBackgroundBrush" Color="LightGray"/>
</ResourceDictionary>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="ExternalBackgroundBrush" Color="LightGray"/>
</ResourceDictionary>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="ExternalBackgroundBrush" Color="LightGray"/>
</ResourceDictionary>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="ExternalBackgroundBrush" Color="LightGray"/>
</ResourceDictionary>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="ExternalBackgroundBrush" Color="LightGray"/>
</ResourceDictionary>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="ExternalBackgroundBrush" Color="LightGray"/>
</ResourceDictionary>3. 全局资源

全局资源是在App.xaml文件中定义的资源,可在整个应用程序中共享。通常用于定义全局样式和模板。
示例:


<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="ExternalBackgroundBrush" Color="LightGray"/>
</ResourceDictionary>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="ExternalBackgroundBrush" Color="LightGray"/>
</ResourceDictionary>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="ExternalBackgroundBrush" Color="LightGray"/>
</ResourceDictionary>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="ExternalBackgroundBrush" Color="LightGray"/>
</ResourceDictionary>    4. 动态资源

动态资源允许在运行时更改资源的值,使应用程序更加灵活。这通常用于实现主题切换等功能。
示例:


<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="ExternalBackgroundBrush" Color="LightGray"/>
</ResourceDictionary>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="ExternalBackgroundBrush" Color="LightGray"/>
</ResourceDictionary>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="ExternalBackgroundBrush" Color="LightGray"/>
</ResourceDictionary>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="ExternalBackgroundBrush" Color="LightGray"/>
</ResourceDictionary>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="ExternalBackgroundBrush" Color="LightGray"/>
</ResourceDictionary>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="ExternalBackgroundBrush" Color="LightGray"/>
</ResourceDictionary>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="ExternalBackgroundBrush" Color="LightGray"/>
</ResourceDictionary>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="ExternalBackgroundBrush" Color="LightGray"/>
</ResourceDictionary>在代码中通过C#修改动态资源:
private void ChangeBackground_Click(object sender, RoutedEventArgs e)
{
    // 获取动态资源并修改其值
    SolidColorBrush dynamicBrush = (SolidColorBrush)FindResource("DynamicBackgroundBrush");
    dynamicBrush.Color = Colors.Green;
}以上是WPF中资源的四个主要类型,它们共同为开发者提供了一种强大而灵活的方式来管理和重用应用程序中的元素。
 


来源:https://www.cnblogs.com/hanbing81868164/p/18031930
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: WPF资源管理:窥探外部、窗体、全局和动态资源的秘密!