资源无法解析(VS 2010 RC)
我有一个 WPF 4 应用程序,它有许多用户控件,它们在其子控件中共享样式。因此,我在 App.xaml 中创建了多种样式,如下所示。例如:
<Style x:Key="ViewTitle" TargetType="{x:Type Border}">
<Setter Property="BorderBrush" Value="LightGray" />
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="VerticalAlignment" Value="Top" />
</Style>
然后在我的 UserControls 中,我使用 StaticResource 来引用它们,例如:
<Border Style="{StaticResource ViewTitle}">
<TextBlock Margin="6,3,4,5" FontSize="18" FontWeight="Bold" Foreground="White" HorizontalAlignment="Left" Text="Host Management" />
</Border>
这一切都工作正常。问题是 Visual Studio 2010 RC 在 StaticResource 引用下放置了一个蓝色波浪线并表示:无法解析资源“ViewTitle”。
这是怎么回事?这是 Visual Studio 的问题还是我做错了什么?
I've got WPF 4 app that has a number of UserControls that share styles in their child controls. So I'm creating a number of styles in my App.xaml as . For example:
<Style x:Key="ViewTitle" TargetType="{x:Type Border}">
<Setter Property="BorderBrush" Value="LightGray" />
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="VerticalAlignment" Value="Top" />
</Style>
Then in my UserControls I use StaticResource to reference them, for example:
<Border Style="{StaticResource ViewTitle}">
<TextBlock Margin="6,3,4,5" FontSize="18" FontWeight="Bold" Foreground="White" HorizontalAlignment="Left" Text="Host Management" />
</Border>
This all works fine. The problem is the Visual Studio 2010 RC is putting a blue squiggly under the StaticResource reference and saying: The resource "ViewTitle" could not be resolved.
What's up with that? Is this a problem with Visual Studio or am I doing something wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从您在评论中的注释“它构建并运行良好”来看,这几乎肯定是 Visual Studio 的问题。不幸的是,VS WPF 设计器存在着臭名昭著的错误,并且经常无法解析或处理完全合法的 XAML。因此,唯一真正的测试是编译和运行:虽然很烦人,但只是人们学会忍受的东西。
您可能发现Expression Blend设计器可以更好地处理这种情况。 Blend 的目标更多地是成为一个视觉设计师(图形设计师是主要目标用户),但 Blend 3 显着改进了对编辑 XAML 源代码的支持。也许值得一试。
From your note in the comments that "it builds and runs fine," this is almost certainly a problem with Visual Studio. Unfortunately, the VS WPF designer is infamously buggy, and will often fail to resolve or handle perfectly legal XAML. The only true test, therefore, is to compile and run: annoying, but just something one learns to live with.
You may find that the Expression Blend designer handles this situation better. Blend aims more at being a visual designer (graphic designers are the main target users) but Blend 3 has significantly improved support for editing XAML source code. It may be worth giving that a try.
此问题已在 Expression Blend 4 中“解决”。
如果您有无法解决的资源,Blend 4 会自动为您提供定位设计时资源的选项。
然后 Blend 将创建一个 DesignTimeResourceDictionary,从中访问您的资源。
但是,如果 VS2010 是您的主要编辑器,这对您没有多大帮助:(
Blend 4 的解决方案:
http://adamkinney.wordpress.com/2010/05/04/design-time-resources-in-expression-blend-4-rc/
This problem has been "solved" in Expression Blend 4.
Blend 4 automatically gives you the option of locating design time resources if you have a resource that could nor be resolved.
Blend will then make a DesignTimeResourceDictionary from which it will access your resources.
However, this will not help you much if VS2010 is your main editor :(
The solution with Blend 4:
http://adamkinney.wordpress.com/2010/05/04/design-time-resources-in-expression-blend-4-rc/