首页 > 解决方案 > 窗口样式在设计器中不可见

问题描述

我创建了一个 Window 样式 (WPF) 并将其作为 dll 添加到我的项目中,当我运行程序时该样式正确显示,但未显示在设计器中。

我已经用谷歌搜索了,但没有一个解决方案有效

测试1:

// Window //
Style="{DynamicResource HVE_Window}"

// Window.Resources //
<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="pack://application:,,,/GlobalHive.Styles;component/HiveWindow.xaml"/>
</ResourceDictionary.MergedDictionaries>

结果:

Error: 'Window' TargetType doesn not match type of element 'WindowInstance'

-> But it runs and display correctly there

测试 2:

// Window //
Style="{DynamicResource MyWindow}"

// Window.Resources //
<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="pack://application:,,,/GlobalHive.Styles;component/HiveWindow.xaml"/>
</ResourceDictionary.MergedDictionaries>

<Style x:Key="MyWindow" TargetType="{x:Type Window}" BasedOn="{StaticResource HVE_Window}" />

结果:

No Error:
Still doesn't display in the designer, still shows up if i run the program

测试 3:

Both versions but added to application resources

它应该看起来如何: 运行设计

它在设计器内部的外观: 设计师设计

标签: c#wpfwindowwindow-style

解决方案


有时您会发现控件库中的资源在设计时并未加载,尽管您在 app.xaml 中放置了任何内容以尝试加载这些内容。

MS 为 Blend 创建了一种机制,您可以在 Visual Studio 中使用它,因为它是混合设计器。

这使用了一个名为 DesignTimeResources.xaml 的“特殊”资源字典

这只会在设计时使用。

在问题 exe 项目的属性中添加一个。

正是这个名字。

把你所有的合并都放进去。

例如,这是我的 MapEditor 项目中的一个,它使用了 UILib 中的大量资源。UILib 是一个包含各种 UI 内容的控件库。

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        xmlns:local="clr-namespace:MapEditor">
        <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="/Views/Drawing/Terrain/Resources/CityResources.xaml"/>
            <ResourceDictionary  Source="/Resources/MapEditorResources.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

卸载您的 csproj(在解决方案资源管理器中右键单击),对其进行编辑并找到该资源字典的节点。

将其更改为:

<Page Include="Properties\DesignTimeResources.xaml">
  <SubType>Designer</SubType>
  <Generator>MSBuild:Compile</Generator>
  <ContainsDesignTimeResources>true</ContainsDesignTimeResources>
</Page>

重新加载项目,关闭并重新打开 Visual Studio。

您的样式现在应该适用。


推荐阅读