C# app.xaml资源字符串中的XamlParseException
我在App.xaml中有以下代码:C# app.xaml资源字符串中的XamlParseException,c#,wpf,string,xaml,resources,C#,Wpf,String,Xaml,Resources,我在App.xaml中有以下代码: <Application x:Class="PcgTools.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:PcgTools.ViewMode
<Application x:Class="PcgTools.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:PcgTools.ViewModels"
xmlns:res="clr-namespace:PcgTools.Resources"
StartupUri="MainWindow.xaml"
Startup="Application_Startup">
<Application.Resources>
<ResourceDictionary>
...
<res:Strings x:Key="LocStrings"/>
</ResourceDictionary>
</Application.Resources>
</Application>
但是,它是生成的,所以我甚至无法更改它
顺便说一句,在不更改代码的情况下(大约一周前),代码已经工作了,但是调试时大约30%的时间出现了此错误,并且在重试后,此异常没有发生。您的
PcgTools.Resources.Strings
类是否有公共默认构造函数?我认为它在抱怨它无法创建对象,因为它找不到匹配的构造函数,而且由于您没有使用参数
,它正在寻找默认构造函数(没有参数的构造函数)。构造函数必须是公共的,而不是内部的或私有的
因为代码是生成的,所以您需要找到一些方法来解决这个问题。使用FactoryMethods可能会奏效。要做到这一点,建立另一个类,如
namespace PcgTools.Resources
{
public static class StringResourceHelper
{
public static strings GetStringResources()
{
return new Resources.Strings();
}
}
}
然后在XAML中,您可以执行以下操作:
<res:Strings x:FactoryMethod="res:StringResourceHelper.GetStringResources"
x:Key="LocStrings"/>
另请参见:
简而言之:不要命名名称空间
它不应该命名为Resources,而应该命名为其他名称,否则您将得到如上所述的错误
编辑:但是,在更改资源字符串后,被覆盖的文件(…Designer.cs)似乎仍然包含内部构造函数而不是公共构造函数,从而导致问题中的错误。是,它有一个默认构造函数。。。我已经把它添加到问题中。它也必须公开。我应该在最初的回答中这样说。我已经更新了我的答案来说明这一点。我已经添加了一个示例,说明由于代码是自动生成的,因此您可能能够绕过此问题。我将其公开,并运行了。。。谢谢但是,当我更改资源字符串时,文件将重新生成,并返回到内部。。。但是:即使这样,应用程序也会启动。不管怎样,你帮助了我,尽管真正的问题和解决办法可能在其他地方。我会接受这个答案。被问及xaml2009中的错误
<res:Strings x:FactoryMethod="res:StringResourceHelper.GetStringResources"
x:Key="LocStrings"/>