3

I created a Window style (WPF) and added it as a dll to my project this style shows corretly when i run the program but doesn't show up in the designer.

I googled already but none of the solutions there are working

Test 1:

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

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

Result:

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

-> But it runs and display correctly there

Test 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}" />

Result:

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

Test 3:

Both versions but added to application resources

How it should look: running design

How it looks inside the designer: designer design

Sascha
  • 63
  • 8
  • did you try to change from DynamicResource to StaticResource? – na th May 12 '19 at 17:11
  • Yep, in the designer it tells me the same error as on test 1 And it's not running anymore - Exception: Cannot find resource named 'HVE_Window'. – Sascha May 12 '19 at 17:15
  • @Sascha: What version of the Visual Studio you are using? – Jackdaw May 12 '19 at 21:28
  • @Jackdaw Visual Studio Community 2019 16.0.2 – Sascha May 12 '19 at 21:38
  • @Sascha: Can you place a little more sources to simulate the problem? Actually this scenario is working for me... – Jackdaw May 12 '19 at 21:51
  • Yep, there you have basically everthing whats going on to change the window style -> [Pastebin](https://pastebin.com/hj2e0zxv) First the main window xml stuff ----------------------- The style packed in a dll – Sascha May 12 '19 at 22:38
  • 1
    This seems to be a bug that has been in Visual Studio for quite a while... I've found references to it at least as far back as *2010*! It appears that the XAML designer creates a sub-class of `Window` called `WindowInstance` to render your control, which results in the error message you mention. There is a workaround for the error, as you noted, but that apparently does not fix the underlying problem of the type still being mismatched and thus the style doesn't render. I don't think there is any way to fix it other than yelling at Microsoft until they do something about it. – Herohtar Sep 04 '19 at 04:02

1 Answers1

3

You can sometimes find that resources from a control library are not loaded at design time, despite whatever you put in app.xaml to try and load the things.

MS created a mechanism for Blend which you can use in visual studio since it's the blend designer.

This uses a "special" resource dictionary called DesignTimeResources.xaml

This will only be used at design time.

Add one to the Properties of your problem exe project.

With exactly that name.

Put all your merges into that.

eg this is one of mine from my MapEditor project that uses numerous resources from UILib. UILib is a control library with all sorts of UI stuff in it.

    <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>

Unload your csproj ( right click in solution explorer), edit it and find the node for that resource dictionary.

Change it to:

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

Reload the project, close and re-open visual studio.

Your styles should now apply.

Andy
  • 11,864
  • 2
  • 17
  • 20
  • 1
    This solved the Xaml designer problems I was having for years with projects that are a control library and don't have a directly referenced App.Xaml. More info here: https://github.com/jbe2277/waf/wiki/WPF-Design-Time-Support-(Part-2) – Ed Bayiates Sep 26 '21 at 17:44
  • @Andy many thanks for provided answer. This issue sucks, I allways ended up by creating new project from scratch. Now finally something that works. – Lucy82 Nov 10 '22 at 12:26