wpf 无法解析资源“x”。
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15224864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The resource "x" could not be resolved.
提问by Chrisjan Lodewyks
I have recently upgraded to VS 2012, I had to as I needed to start using the .net 4.5 but that is besides the point. My problem is the following:
我最近升级到了 VS 2012,我不得不开始使用 .net 4.5,但这不是重点。我的问题如下:
I have a ResourceDictionary
in my main project called AppStyles.xaml
, and in the App.Xaml
I have the following:
ResourceDictionary
我的主项目中有一个名为AppStyles.xaml
,并且在App.Xaml
我有以下内容:
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="AppStyles.xaml"/>
<ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
In my ResourceDictionary
there is a style, and I could then apply this style to any button in any of my projects by setting Style={StaticResource MyButton}
.
在我的ResourceDictionary
有一个样式,然后我可以通过设置Style={StaticResource MyButton}
.
After upgrading to VS 2012 I noticed I get the error in the title a lot, and sometimes it stops me from debugging and sometimes it doesn't!
升级到 VS 2012 后,我注意到标题中出现了很多错误,有时它会阻止我进行调试,有时则不会!
Is there a different way that I should be doing this or is there a problem in VS 2012?
是否有不同的方式我应该这样做,或者 VS 2012 中是否存在问题?
回答by WonkiDonk
WPF is unable to resolve the resource dictionary that you are trying to merge. When you create a merged dictionary, WPF follows a set of rules to attempt to resolve the URI. In your case, the URI is ambiguous, so WPF is unable to reliably resolve it.
WPF 无法解析您尝试合并的资源字典。创建合并字典时,WPF 会遵循一组规则来尝试解析 URI。在您的情况下,URI 不明确,因此 WPF 无法可靠地解析它。
Change the Source
URI in the App.xaml
to an absolute pack URI. For example, if you project is called MyProject
(i.e.: "MyProject" is the short assembly name), then you should change the Source
in App.xaml
to:
将 中的Source
URI更改为App.xaml
绝对包 URI。例如,如果您的项目被调用MyProject
(即:“MyProject”是短程序集名称),那么您应该将Source
in更改App.xaml
为:
<ResourceDictionary
Source="/MyProject;component/AppStyles.xaml"/>
This assumes AppStyles.xaml
is in the root of MyProject
. You can optionally specify the authority, assembly version and public key information of the signed assembly. However, you should be safe with the short assembly name (MyProject, in the above example).
这假设AppStyles.xaml
在 的根中MyProject
。您可以选择指定已签名程序集的权限、程序集版本和公钥信息。但是,使用简短的程序集名称(MyProject,在上面的示例中)应该是安全的。
See this page on MSDN for further details on Pack URIs in WPF: http://msdn.microsoft.com/en-us/library/aa970069(v=vs.110).aspx
有关 WPF 中 Pack URI 的更多详细信息,请参阅 MSDN 上的此页面:http: //msdn.microsoft.com/en-us/library/aa970069(v= vs.110).aspx
回答by hiHyman
I also have this problem time to time in VS2010. Sometimes the problem will solve if I make a "Clean Solution" and a "Rebuild Solution". If that do not work I usually restart VS2010.
我在 VS2010 中也时不时遇到这个问题。有时,如果我制作“清洁解决方案”和“重建解决方案”,问题就会解决。如果那不起作用,我通常会重新启动 VS2010。
I have also renamed the Style x:Key to something else and the problem was gone. But I dont find this solution to be perfect...
我还将 Style x:Key 重命名为其他东西,问题就消失了。但我不认为这个解决方案是完美的......
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- Load Infrastructure's Resource Dictionaries -->
<ResourceDictionary Source="/MyProject.Modules.Infrastructure;component/ResourceDictionaries/ResourceLibrary.xaml" />
</ResourceDictionary.MergedDictionaries>
<!-- Workaround for ResourceDictionary loading bug/optimization -->
<Style TargetType="{x:Type Rectangle}" />
</ResourceDictionary>
Reference to this question regarding the Workaround in my code sample: Trouble referencing a Resource Dictionary that contains a Merged Dictionary
在我的代码示例中参考有关解决方法的这个问题:Trouble reference a Resource Dictionary that contains a Merged Dictionary
回答by algowhiz
you can create a common or infrastructure project from which other projects will reference. Add your resouce resources. Then create a pack URI. then reference in your usercontrol or window resources within a resource dictionary
您可以创建一个公共或基础设施项目,其他项目将从中引用。添加您的资源。然后创建一个包 URI。然后在资源字典中引用您的用户控件或窗口资源
<...Resources>
<ResourceDictionary>
<!-- Resource Dictionaries -->
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="pack://application:,,,/Common;component/Dictionaries/Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
In the usercontrol or use Window.Resources in the case of a window. This works for me.
在用户控件或窗口的情况下使用Window.Resources。这对我有用。
回答by AMS
it is possible to use the relative path to your appStyles.xaml folder. hope this helps.
可以使用 appStyles.xaml 文件夹的相对路径。希望这可以帮助。
something like below
像下面这样的东西
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../Resources/Styles.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
<ResourceDictionary>
回答by TheGaMeR123
you can add the resource dictionary to your project then change its build action to resource and then try to add its uri in the code as follows:
您可以将资源字典添加到您的项目中,然后将其构建操作更改为资源,然后尝试在代码中添加其 uri,如下所示:
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="/Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
or simply let the vs2012 process it by using the properties tab...
或者干脆让 vs2012 使用属性选项卡处理它...
回答by Mikhail
Also you can check for styles with the same x:key
in AppStyles.xaml. It caused me a similar error.
您也可以x:key
在 AppStyles.xaml 中检查相同的样式。它给我带来了类似的错误。
回答by CoRe23
I had placed some Style in Window.Resources a this caused my problem
我在 Window.Resources 中放置了一些样式,这导致了我的问题
After deleting all styles from MainWindow problem disappeared.
从 MainWindow 中删除所有样式后问题消失了。
Found second problem:
发现第二个问题:
Problem was Platform Target set to x64.
问题是平台目标设置为 x64。
After changing it to AnyCPU the resources in design works..
将其更改为 AnyCPU 后,设计中的资源有效..