我创建了一个外部控件库,其中包含一些资源字典等。我的问题是当我尝试在 Window
元素上应用样式时。样式的更改仅在运行时可见,而不是在 Visual Studio 设计器中可见。
我的控件库中的资源字典示例:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="Window_Style" TargetType="Window">
<Setter Property="Background" Value="#FF272727"/>
</Style>
</ResourceDictionary>
这就是我将外部资源字典包含到我的应用程序中的方式:
<Application x:Class="SigmaLibMaster.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SigmaLibMaster"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/SigmaLib;component/Resources/Styles/Window.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
以及如何将其应用到我的窗口元素:
<Window x:Class="SigmaLibMaster.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:SigmaLibMaster"
mc:Ignorable="d"
Title="MainWindow" Height="480" Width="840"
Style="{DynamicResource Window_Style}">
<Grid>
</Grid>
</Window>
知道为什么会发生这种情况吗?
最佳答案
有时您会发现库中的资源在设计时不起作用。
在我看来,这是一个错误。
我使用的工作循环是设计时资源。
这是一种最初用于混合的机制。但是Visual Studio中的wpf设计器和现在的blend是同一个设计器。
我有一个名为 uilib 的库。
在其属性中,我添加了一个名为 DesignTimeResources.xaml 的资源字典。一定是这个名字。
在 csproj 中,我有以下内容:
<ItemGroup>
<Page Include="Properties\DesignTimeResources.xaml" Condition="'$(DesignTime)'=='true' OR ('$(SolutionPath)'!='' AND Exists('$(SolutionPath)') AND '$(BuildingInsideVisualStudio)'!='true' AND '$(BuildingInsideExpressionBlend)'!='true')">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
<ContainsDesignTimeResources>true</ContainsDesignTimeResources>
</Page>
特别注意 ContainsDesignTimeResources 标记。
它合并了我在 uilib 中的一堆资源字典:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:UILib">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/UILib;component/Resources/Geometries.xaml"/>
<ResourceDictionary Source="pack://application:,,,/UILib;component/Resources/ControlTemplates.xaml"/>
<ResourceDictionary Source="pack://application:,,,/UILib;component/Resources/FontResources.xaml"/>
<ResourceDictionary Source="pack://application:,,,/UILib;component/Resources/UILibResources.xaml"/>
<ResourceDictionary Source="pack://application:,,,/UILib;component/Resources/HypsoColoursRD.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
当你构建的时候,它不会额外的时间合并这些资源。标签中的条件仅表示设计时间。 现在您知道它存在,通过搜索可能会找到更多相关信息。
https://dennymichael.net/2016/07/28/wpf-design-time-resources-dictionary/
编辑
使用提供的代码尝试过此操作后,发现 x:Type Window 的使用变得复杂。
MainWindow 不是一个窗口,它是一个 MainWindow。
设计者似乎不想弄清楚MainWindow继承自Window。
如果我将该样式更改为:
<Style x:Key="MainWindow_Style" TargetType="{x:Type Control}">
<Setter Property="Background" Value="Green"/>
</Style>
这对我的设计师很有用。
关于c# - 外部 (dll) 资源字典中的窗口样式不适用于设计模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75420161/