Silverlight:无法解析资源 {0}(仅在设计时)
我在 VS2010 中创建了一个新的 silverlight 4 项目。
我的 App.xaml 文件如下:
<Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="ZCall.App">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Styles.xaml"/>
<ResourceDictionary Source="Resources/ObjectResourceDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
生成错误的示例 xaml 视图是:
<UserControl x:Class="ZCall.View.Control.CDetailsControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"
mc:Ignorable="d">
<Grid x:Name="LayoutRoot" Background="White">
<toolkit:BusyIndicator x:Name="biCDetails" BusyContent="Busy...">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="10"/>
<RowDefinition/>
<RowDefinition Height="10"/>
<RowDefinition/>
<RowDefinition Height="10"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="170"/>
</Grid.ColumnDefinitions>
<StackPanel Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="5" Orientation="Horizontal">
<TextBlock Text="Name:" Style="{StaticResource PatientLabel}"/>
<TextBlock x:Name="txtName" Style="{StaticResource PatientData}"/>
</StackPanel>
</Grid>
</toolkit:BusyIndicator>
</Grid>
</UserControl>
我在 PatientLabel 和 PatientData 资源上都收到错误。
我的 style.xaml 文件位于项目根目录的 Resources 文件夹中。
我的问题是,在设计时,styles.xaml 中定义的任何样式都无法被识别,并且对于使用的每种样式,我都会收到错误“资源无法解析”,而在运行时,所有样式都被解析并显示为应该是的。
有什么建议吗?
提前致谢, 克鲁维
I've created a new silverlight 4 project in VS2010.
My App.xaml file is as follows:
<Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="ZCall.App">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Styles.xaml"/>
<ResourceDictionary Source="Resources/ObjectResourceDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
a sample xaml view that generates the error is:
<UserControl x:Class="ZCall.View.Control.CDetailsControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"
mc:Ignorable="d">
<Grid x:Name="LayoutRoot" Background="White">
<toolkit:BusyIndicator x:Name="biCDetails" BusyContent="Busy...">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="10"/>
<RowDefinition/>
<RowDefinition Height="10"/>
<RowDefinition/>
<RowDefinition Height="10"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="170"/>
</Grid.ColumnDefinitions>
<StackPanel Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="5" Orientation="Horizontal">
<TextBlock Text="Name:" Style="{StaticResource PatientLabel}"/>
<TextBlock x:Name="txtName" Style="{StaticResource PatientData}"/>
</StackPanel>
</Grid>
</toolkit:BusyIndicator>
</Grid>
</UserControl>
I get the errors on both PatientLabel and PatientData resources.
My style.xaml file is located in Resources folder in the root of the project.
My problem is that in design time, none of my styles defined in styles.xaml are recognized, and i get the error "The resource could not be resolved" for every style used, while in run time all the styles are resolved and displayed as it should be.
Any suggestions?
Thanks in advance,
kruvi
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
将 App.xaml 的构建操作设置为 ApplicationDefinition
当您将 App.xaml 从一个项目复制到另一个项目时,经常会发生此错误。
Set Build Action of your App.xaml to ApplicationDefinition
the Error often occurs when you copy your App.xaml from one project to an other.
我已经尝试了很多次一些解决方案,但我现在找到了一个可行的解决方案。首先创建 xaml 文件来保存合并的资源字典,如下所示:
然后在 App.xaml 中声明它以使用资源文件:
现在构建您的应用程序,它将在设计器和设备或模拟器中运行。
最好的问候
恩里科
i have tried many times some solutions, but i found now a working one. First create xaml file for holding the merged resource dictionaries, like this:
Then declare it in you App.xaml to use the resource file:
Now build your app and it would work in the designer and on the device or emulator.
Best regards
Enrico
我遇到了完全相同的错误,但附加了我也在使用 MVVM-Light 的警告。事实证明,我遇到的问题源于全局视图模型定位器的位置不正确< /a> 我的 app.xaml 文件中的定义。
这就是我的 app.xaml 的配置方式。请注意 Application.Resources 标记中全局视图模型定位器的位置。这是不正确的,导致我在 Visual Studio 中出现设计时错误。
下面的代码是正确的。请注意,全局视图模型定位器位于 ResourceDictionary 标记内。一旦我将全局视图模型定位器移至 resourceDictionary 标记内,我的设计时错误就消失了。
I had the exact same error with the added caveat that I was also using MVVM-Light. It turns out that the problems I was having stemmed from an incorrect location of the Global View Model Locator definition in my app.xaml file.
This is how my app.xaml was configured. Notice the location of the Global View Model Locator within the Application.Resources tag. This is incorrect and caused me to have design time errors within Visual Studios.
The code below is correct. Notice that the Global View Model Locator is inside the ResourceDictionary tag. Once I moved the Global View Model Locator inside the resourceDictionary tag my design time errors went away.
在模块项目中添加对 Microsoft.Practices.Unity.Silverlight 程序集的引用。它提供了设计时魔法来查找 shell 项目中定义的资源。
Add a reference to the Microsoft.Practices.Unity.Silverlight assembly in your module projects. It provides the design-time magic to find resources defined in your shell project.
我在 WPF 类库项目中遇到了同样的问题。这种项目中没有 App.xaml,所以显然原因在其他地方。我们发现它是一个损坏的 VisualStudio 项目文件:表示资源文件的项目中缺少
Designer
标记:当
被带回来,VS设计师开始工作。I've faced with the same problem in a WPF class library project. There is no App.xaml in this kind of projecst, so obviously the reason was somewhere else. And it was found that it is a broken VisualStudio project file: a
<SubType>Designer</SubType>
tag was missing in the item representing the resource file:When the
<SubType>
was brought back, VS designer started working.当我删除初始 App.xaml 并添加受操作的 App.xaml 时,我遇到了同样的问题。
请确保 App.xaml 属性如下所示:
I had the same issue when I deleted initial App.xaml and added a manipulated App.xaml
Make sure that App.xaml Properties look like:
我找到了一种在设计时加载资源字典的解决方法,而无需在运行时多次重新加载相同的资源。我在使用所包含资源的每个 UserControl 中重复调用所需的 ResourceDictionary,并且在该 UserControl 的 .xaml.cs 中,我添加了这一行:
以避免在运行时使用相同的资源来更新应用程序。当然,您需要在主项目的其他位置(例如 App.xaml)调用 ResourceDictionary。
希望这也适用于您的情况。
I found a workaround for loading resource dictionaries in design time without re-loading the same resource multiple times at runtime. I repeated the call of the needed ResourceDictionary in every UserControl that uses the included resources, and in the .xaml.cs of that UserControl, I added this line:
to avoid inondating the application at runtime with the same resources. Of course you need to call the ResourceDictionary somewere else in the main project (App.xaml for instance).
Hope this works for your case too.
由于设计器处理样式定义的方式,样式文件(例如app.xaml)中的任何错误都将导致设计器无法解析该文件中定义的任何样式的样式。
我们遇到的这个问题是由样式文件中的两个小错误引起的。一旦我们修复了这些错误并对整个解决方案进行了完全重建,整个解决方案中的所有样式都将得到正确解析。
因此,检查包含资源的文件并修复设计人员认为错误的任何内容(下面的蓝色波浪线),然后进行完全重建。
Because of the way the designer handles style definitions, any errors in a style file (eg app.xaml) will result in the designer not being able to resolve styling for any style that is defined in that file.
We had this problem that was caused by 2 minor errors in a style file. Once we fixed those errors and did a full rebuild on the entire solution all the styles were being resolved correctly throughout the entire solution.
So, go through the file that contains the resource and fix anything that the designer thinks is an error (blue squiggly line underneath) and then do a full rebuild.