5

I have a very simple WPF project set up as such:

enter image description here

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:

enter image description here enter image description here

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?

Daniel
  • 10,864
  • 22
  • 84
  • 115

2 Answers2

5

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,

  1. drag your Styles folder + Styles.xaml into UserControls folder.
  2. In 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.

  1. Set your Styles.xaml Build Action to Resource.
  2. Change the Source to /WpfApplication1;component/Styles/Styles.xaml
  3. Rebuild

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)
Community
  • 1
  • 1
Kcvin
  • 5,073
  • 2
  • 32
  • 54
0

This is quite old, but I had a similar problem but the no type of pack URI solved my problem. UserControl worked in run time and in designer when editing it, but it couldn't be initialized in another window, but only in design mode.

I was able to fix the problem by making all the file names / path lowercase. I don't understand how this can fix the problem in a windows environment, but it did. And even source string does not have to be case sensitive, only the file and the filepath has to be all lowercase.

The accepted answer is valid but in my odd case, it didn't fix the problem. Maybe my experience helps someone else.