简体   繁体   中英

Visual Studio 2019 WPF designer won't display custom controls

In a WPF (.NET Core) application project I have defined a folder to contain all custom controls (both class definitions and UI style.xaml files, each custom control has a pair of these files). When using these custom controls in the same assembly, I associate the default style to the custom control by merging all UI styles in the /Themes/Generic.xaml shown below

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="/CustomControl/SomeCustomControlStyle.xaml" />
    <!-- Similarly list all other custom control xaml files -->
</ResourceDictionary.MergedDictionaries>

For each custom control the corresponding xaml defines its style without x:Key as follows

<Style TargetType="{x:Type SomeCustomControl}" >
    <Setter Parameter="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type SomeCustomControl}">
                <!-- UI Definitions -->
            </<ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

All custom control derives from Control and contains a static constructor with

DefaultStyleKeyProperty.OverrideMetadata(typeof(SomeCustomControl), 
                new FrameworkPropertyMetadata(typeof(SomeCustomControl)));

The project build successfully and the custom controls are shown when application is running. However Visual Studio designer is unable to display any custom controls and reporting an error

XDG0062 Cannot locate resource 'customcontrols/somecustomcontrol.xaml'.

I have tried to move the default style from individual.xaml files to Generic.xaml and the problem can be solved. However I have quite a few custom controls and using a single Generic.xaml to define all the UI is not very ideal. But not be able to see UI elements in designer is also very inconvenient. Am I missing something? Is it a Visual Studio bug? Are there any walkarounds?

Thanks!

Problem solved!

For whatever reason I need to reference to the individual style.xaml files using pack URI and it has to contain assembly name. The following will NOT work

<ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="pack://application:,,,/CustomControls/SomeCustomControlStyle.xaml"/>
    </ResourceDictionary.MergedDictionaries>

But the following works

<ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="pack://application:,,,/AppAssemblyName;component/CustomControls/SomeCustomControlStyle.xaml"/>
    </ResourceDictionary.MergedDictionaries>

This behavior is the same no matter the custom control in created in a WPF application assembly or in a separate Custom Control Library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM