Janes question is actually related to this one, i have the same problem. i've created a subclass of ResourceDictionary like Foovanadil suggested, however at design time this throws a "Uri prefix not recodnized" exception
I get this error even if all the sub class does is assign the base.Source property of ResourceDictionary. If i replace my custom ResourceDictionary with the regular one, it works just fine, so im guessing the cider designer does someting special, perhaps replacing ResourceDictionary with something else
I havent tried this in blend but i do get it in VS2010 (sp1 beta)
Can someone confirm that the posted awnser works in vs2010?
-edit-
Its possible that the wpf based designer is tripping up the resource locating logic. I've read elsewhere that pack uris are based on the -executing- assembly, not neccecarily the -local- assembly. i'll report whats i finds
-edit2/solution-
Alright so i managed to get Foovanadil solution to work in VS2010, here is the deal. Turns out i was half right, presumably because the wpf designer in VS2010 is itself written in wpf or to provide a better design experience, ResourceDictionary (or some other class that VS2010 uses instead) behaves diffrently at design time.
It turns out that VS replaces the URI that is entered in the xaml. Attatcing to the VS instance and setting a breakpoint in the setter for Source
reveals that the actual value that is getting passed is diffrent from what we expect.
Given the xaml:
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<local:DesignTimeResourceDictionary Source="myxamlfile.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
Source
is set to
markup://1/file:///c:/PathtoMyapp/MainWindow.xaml#0/myxamlfile.xaml
instead of
/myxamlfile.xaml
This is why we get the exception for invalid uris.
Exactly when and where VS2010 edits URI
s this way at design time is not known to me, but it renaming the Source
property in DesignTimeResourceDictionary
to something else does not solve the problem.
Luckliy there is a workaround. If we change the type of Source
from Uri
to String
, the VS designer does not modify the value. In the setter we then create a Uri and pass it to the base property:
public new String Source {
get {
return base.Source.ToString();
}
set {
if( !IsInDesignMode )
return;
base.Source = new Uri( value, UriKind.RelativeOrAbsolute );
}
}
Note also that if you use this approach (or any other that ive found such as using x:class in the resource file and instancing it directly from xaml) will cause styles not to get updated until you build. Using the standard ResourceDictionary will cause the designer to update as soon as you edit the imported resource files (even before you save). This is also an indication that the designer treats ResrouceDictionary diffrently at design time.
Hope someone finds this useful :)