BASE 中的 WPF 样式 class

WPF Styles in a BASE class

我在 WPF .Net 6 中工作 我有一个 Master.exe 应用程序和许多 DLL,每个 DLL 都包含每个实体的功能:Customers.dll | Bookings.dll | Vehicles.dll 等。在每个 DLL 中,我有 WPF/XAML windows、视图模型、模型等,就像一个迷你的完整和独立的应用程序。

我还创建了一个 Base.dll,它具有模型和视图模型继承的抽象 类。这适用于所有 dll 之间的一些共享代码。

同样,我想要一个统一样式的一站式商店 XAML windows。

现在的问题是;我可以在 Base.dll 中拥有被其他 dll 中的所有 XAML windows 使用的 XAML 资源和样式吗?

当然可以。创建一个WPF Custom Control Library项目,并在该项目的一个或多个资源字典中定义您常用的XAML资源,例如:

<!-- Dictionary1.xaml in WpfControlLibrary1.dll: -->
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <SolidColorBrush x:Key="CommonBrush" Color="Red" />
</ResourceDictionary>

然后从你打算使用公共资源的项目中添加对这个项目的引用并合并资源字典:

<ResourceDictionary>
    <Style x:Key="ButtonStyle" TargetType="Button">
        <Setter Property="Foreground" Value="{StaticResource CommonBrush}" />
    </Style>
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="pack://application:,,,/WpfControlLibrary1;component/Dictionary1.xaml" />
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>