.net - Bug in the Visual Studio WPF designer? -


check following scenario (others may apply well) [you can create project copy pasting code here on right file]:

a - create resourcedictionary basic stuff (resources.xaml):

<resourcedictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">      <solidcolorbrush color="red" x:key="test" />      <style targettype="{x:type groupbox}" x:key="test2" >         <setter property="background" value="blue" />     </style>      <style targettype="{x:type textblock}" >         <setter property="foreground" value="green" />     </style> </resourcedictionary> 

b - create user control base others inherit containing basic resources (usercontrolbase.cs):

using system.windows.controls; using system; using system.windows;  namespace resourcetest {     public class usercontrolbase : usercontrol     {         public usercontrolbase()         {             this.resources.mergeddictionaries.add(new resourcedictionary() { source = new uri("resourcetest;component/resources.xaml", urikind.relativeorabsolute) });         }     } } 

c - create usercontrol inheriting base (usercontrol1.xaml):

<resourcetest:usercontrolbase x:class="resourcetest.usercontrol1"               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"               xmlns:d="http://schemas.microsoft.com/expression/blend/2008"              xmlns:resourcetest="clr-namespace:resourcetest"               mc:ignorable="d"                d:designheight="300"               d:designwidth="300" >     <grid>         <groupbox borderbrush="{staticresource test}" margin="3" header="test" style="{dynamicresource test2}" >             <textblock text="testtest" />         </groupbox>      </grid> </resourcetest:usercontrolbase> 

results: staticresources not resolved (and test borderbrush not loaded). dynamicresources resolved (the background blue) designer says cannot find resource anyway (the first time works ok when open/close designer resource cannot resolved). non named resources textblock style work ok.

is designer bug or doing wrong? ok have declare resources dynamic in scenario resources never change?

enter image description here

thanks in advance.

it appears designer has trouble resolving mergeddictionaries defined in code-behind @ design-time.

an worse example can seen moving resourcedictionary before initialize.

public usercontrol1() {     this.resources.mergeddictionaries.add(new resourcedictionary() { source = new uri("tempproject;component/resources.xaml", urikind.relativeorabsolute) });     initializecomponent(); } 

in case, dynamicresource fails resolve @ design-time, indicating design-time view not calling constructors may expect. tested visual studio 2012, behavior has not changed since 2010.

in terms of original test code, note staticresource bind expected @ run-time (the red border appears), regardless of "error" thrown , lack of red border design-time view.

i see 2 options:

  1. use dynamicresource necessary resolve these @ design-time. while can use staticresource, associated "errors" , lack of design-time view problem. other answers seem indicate there may not performance difference between 2 now.

  2. simply instantiate resourcedictionary in usercontrol.resources, , don't inherit base class. while you're condensing bit of code using base class, you're not being more efficient, since new resourcedictionary instance going created everytime. since can't (afaik) extend base class xaml front-end, potentially argue against having unreferenced mergeddictionary @ level of abstraction.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -