WPF进阶技巧和实战04-资源

系列文章链接

资源集合

每个元素都有Resources属性,该属性存储了一个资源字典集合(它是ResourceDictionary类的实例)。资源集合可以包含任意类型的对象,并根据字符串编写索引。

每个元素既可以访问自己的资源集合中的资源,也可以访问所有父元素 的资源集合中的资源,所以一般将资源集合放在窗口级别或者应用程序级别。

资源的引用分为动态、静态。静态资源在首次创建窗口时一次性地设置完毕。动态资源,如果发生了改变,就会重新应用资源。一般在下列情况下才需要使用动态资源属性:

  • 资源具有依赖系统设置的属性(如当前Windows系统的颜色或者字体)
  • 准备通过编程方式替换资源对象(实现几类动态皮肤)

非共享资源

通常,在多个地方使用资源时,使用的是同一个对象实例。这种行为称为:共享。也可以在每次使用时创建单独的对象实例,对资源设置属性:

x:Shared=“False”

一般情况下,很少使用非共享资源,而是使用多个资源切换的形式来展示不同的资源

资源字典

如果希望在多个项目之间共享资源,可以使用资源字典。资源字典只包含XAML文档,除了希望存储的资源外,不做任何事情。

资源字典的XAML文件,务必将生成操作设置为Page(页),这样才能保证获得最佳性能将资源字典编译为BAML。

为了使用资源字典,需要将其合并到应用程序某些位置的资源集合中,可以在特定的窗口中执行,也可以合并到应用程序的资源集合中。

<Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary>
                    <ResourceDictionary.MergedDictionaries>
                        <ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/SkinDefault.xaml" />
                        <ResourceDictionary Source="Resources/Themes/SkinDefault.xaml" />
                    </ResourceDictionary.MergedDictionaries>
                </ResourceDictionary>
                <ResourceDictionary>
                    <ResourceDictionary.MergedDictionaries>
                        <ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/Theme.xaml" />
                        <ResourceDictionary Source="Resources/Themes/Theme.xaml" />
                    </ResourceDictionary.MergedDictionaries>
                </ResourceDictionary>

                <ResourceDictionary>
                    <ResourceDictionary.MergedDictionaries>
                        <ResourceDictionary Source="pack://application:,,,/LpbPrj.Client.UI;component/Resources/Themes/MyStyles.xaml" />
                    </ResourceDictionary.MergedDictionaries>
                </ResourceDictionary>
                <!--<ResourceDictionary Source="pack://application:,,,/LpbPrj.Client.UI;component/Resources/Themes/MyStyles.xaml" />-->
            </ResourceDictionary.MergedDictionaries>
            <!--<ObjectDataProvider
                x:Key="ExamTypes"
                MethodName="GetValues"
                ObjectType="sys:Enum">
                <ObjectDataProvider.MethodParameters>
                    <x:Type Type="data:ExamType" />
                </ObjectDataProvider.MethodParameters>
            </ObjectDataProvider>
            <ObjectDataProvider
                x:Key="ShowAnimations"
                MethodName="GetValues"
                ObjectType="sys:Enum">
                <ObjectDataProvider.MethodParameters>
                    <x:Type TypeName="hc:ShowAnimation" />
                </ObjectDataProvider.MethodParameters>
            </ObjectDataProvider>-->
            <!--<ObjectDataProvider
                x:Key="HatchStyles"
                MethodName="GetValues"
                ObjectType="sys:Enum">
                <ObjectDataProvider.MethodParameters>
                    <x:Type Type="hc:HatchStyle" />
                </ObjectDataProvider.MethodParameters>
            </ObjectDataProvider>-->
            <!--<data:EnumDataProvider x:Key="ExamTypes" Type="dataCore:ExamType" />
            <data:EnumDataProvider x:Key="DeviceTypes" Type="dataCore:DeviceType" />
            <data:EnumDataProvider x:Key="ShowAnimations" Type="hc:ShowAnimation" />
            <data:EnumDataProvider x:Key="HatchStyles" Type="hc:HatchStyle" />-->
            <!--<data:EnumDataProvider x:Key="SW6000InkCanvas" Type="dataCore:SW6000EditMode" />-->
        </ResourceDictionary>
    </Application.Resources>

程序集资源共享,可以使用pack URI语法:

<ResourceDictionary Source="pack://application:,,,/LpbPrj.Client.UI;component/Resources/Themes/MyStyles.xaml" />

注意:

对与音视频文件,一般不会打包到应用程序内部,会采用输出到程序执行目录的形式进行加载。会将音视频资源的属性进行设置:

  • 生成操作:内容
  • 复制到输出目录:始终复制 或者 如有较新则复制
posted @ 2021-07-19 15:02  蜗牛的希望  阅读(249)  评论(0编辑  收藏  举报