WPF整理-使用ResourceDictionary管理Logical Resources

“Logical resources may be of various types, such as brushes, geometries, styles, and templates.
Placing all those resources in a single file such as App.xaml hinders maintainability. A better
approach would be to separate resources of different types (or based on some other criteria) to
their own files. Still, they must be referenced somehow from within a common file such as App.
xaml so they are recognized.”

为了增加资源文件的可维护性,我们应该使用ResourceDictionary对资源进行:分类、汇总。

如何实现呢?举个例子

1.新建一个WPF Application,在Application中添加一个New Item,选择ResourceDictionary。

譬如,命名为Brushes.xaml,我们用它来存放一些笔刷。打开,我们添加一个笔刷如下:

Brushes.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <LinearGradientBrush EndPoint="1,0" x:Key="brush1">
        <GradientStop Color="Violet" Offset="0" />
        <GradientStop Color="Orange" Offset=".7" />
        <GradientStop Color="Brown" Offset="1" />
    </LinearGradientBrush>
</ResourceDictionary>

2.在App.xaml中Merge则个Resource。
“Open App.xaml. We need to merge external resource dictionaries into the main
application dictionary.

打开App.xaml,添加如下内容:

复制代码
<Application x:Class="ManagingLogicalResources.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Brushes.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>
复制代码

3.这样我们就可以在页面中正常使用了。

<Window x:Class="ManagingLogicalResources.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Ellipse Fill="{StaticResource brush1}"/>
    </Grid>
</Window>

效果如下:

 -----------------------------------

在实际开发中更常用的做法是:直接在使用的View内部Merge。

复制代码
<Window x:Class="WPFMergedDicitonary.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Brushes.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
    <Grid>
        <Ellipse Fill="{StaticResource brush1}"/>
    </Grid>
</Window>
复制代码

效果同上,如下:

posted @   DebugLZQ  阅读(14954)  评论(8编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示