我成功复制了这个问题。
原因是:
xmlns:bar="clr-namespace:WpfApplication1.UserControls"
您指定了命名空间,仅限于UserControls。由于您正在尝试合并在MyUserControl中使用位置(Source)查找资源的字典,它可以找到该资源;但是,似乎Foo.MyFoo不知道要查找的位置。当设计器尝试在MyFoo中实例化MyUserControl时发生了什么,它无法解析Styles.xaml资源的位置。
为了修复这个问题:
1.将Styles文件夹+Styles.xaml拖动到UserControls文件夹中。
2.在MyUserControl.xaml中将源路径修正为<ResourceDictionary Source="Styles/Styles.xaml" />。
现在您可以在设计时看到控件。
编辑:
我找到了一种方法来保持项目不变,并获得相同的结果。
1.将Styles.xaml生成操作设置为资源。
2.将源更改为/WpfApplication1;component/Styles/Styles.xaml。
3.重新生成。
就生成操作之间的区别而言,在
此处是一个很好的帖子。
在VS2012中,我获取异常的StackTrace,最低级别似乎与Baml有关(当生成操作设置为Page时创建Baml)。这是内部异常:
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)
Build Action
设置为Page
。 - Federico Berasategui