I have a very simple WPF project set up as such:
MyFoo.xaml looks like:
<UserControl xmlns:bar="clr-namespace:WpfApplication1.UserControls" ...>
<Grid>
<bar:MyUserControl />
</Grid>
</UserControl>
Styles.xaml looks like:
<Style TargetType="TextBlock">
<Setter Property="Foreground"
Value="Red" />
</Style>
MyUserControl.xaml looks like:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Styles/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Grid>
<TextBlock Text="Hello world!" />
</Grid>
My control won't render in design time, however it works fine at run time:
As shown in the screenshot, I get the error:
Cannot locate resource 'styles/styles.xaml'.
I've tried setting the build action of Styles.xaml to "Resource" and cleaning/building and it did not work.
Why is this happening? How do I fix it?
I was able to duplicate the problem.
The reasoning is behind:
xmlns:bar="clr-namespace:WpfApplication1.UserControls"
You're specifying the namespace specifically to UserControls. Since you're trying to merge the dictionary in MyUserControl
which specifies the source using a location (Source
) it can find the resource; however Foo.MyFoo doesn't have knowledge of where to look it seems. Whatever is happening when the designer tries to instantiate MyUserControl in MyFoo, it can't resolve the location of the Styles.xaml resource.
To fix it,
Styles
folder + Styles.xaml
into UserControls
folder. MyUserControl.xaml
fix your source path to <ResourceDictionary Source="Styles/Styles.xaml" />
You can now see your controls during design time.
Edit
I found a way to keep the project as is and get the same results.
Source
to /WpfApplication1;component/Styles/Styles.xaml
As far as the difference between Build Actions, here is a good post.
In VS2012 I get a StackTrace of the exceptions, and the lowest level seems like it may be Baml
related (where Baml is created when Build Action is set to Page). Here is the inner-most exception:
IOException: Cannot locate resource 'styles/styles.xaml'. at
MS.Internal.AppModel.ResourcePart.GetStreamCore(FileMode mode,
FileAccess access) at
System.IO.Packaging.PackagePart.GetStream(FileMode mode, FileAccess
access) at
System.IO.Packaging.PackWebResponse.CachedResponse.GetResponseStream()
at System.IO.Packaging.PackWebResponse.GetResponseStream() at
System.IO.Packaging.PackWebResponse.get_ContentType() at
MS.Internal.WpfWebRequestHelper.GetContentType(WebResponse response)
at MS.Internal.WpfWebRequestHelper.GetResponseStream(WebRequest
request, ContentType& contentType) at
System.Windows.ResourceDictionary.set_Source(Uri value) at
System.Windows.Baml2006.WpfSharedBamlSchemaContext. <Create_BamlProperty_ResourceDictionary_Source>b__1c4(Object
target, Object value) at
System.Windows.Baml2006.WpfKnownMemberInvoker.SetValue(Object
instance, Object value) at
MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(XamlMember member,
Object obj, Object value) at
MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(Object inst,
XamlMember property, Object value)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With