Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UserControl won't render at design time because of inability to find resource?

Tags:

wpf

xaml

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 hereenter 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?

like image 328
Daniel Avatar asked Oct 02 '22 06:10

Daniel


1 Answers

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)
like image 113
Kcvin Avatar answered Oct 13 '22 12:10

Kcvin