app.xaml 资源字符串中的 XamlParseException [英] XamlParseException in app.xaml resource strings

查看:32
本文介绍了app.xaml 资源字符串中的 XamlParseException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 App.xaml 中有以下代码:

I have the following code in 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.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>

(... 是我为了使示例更清晰而删除的一些行).

(The ... are some lines which I removed to make the example cleaner).

当我运行应用程序时,我收到以下错误(在尝试运行/调试后直接出现):(从荷兰语翻译,所以可能不是 100% 字面上相等):

When I run the application I get the following error (directly after trying to run/debug): (translated from Dutch so might not be 100% literally equal):

在类型 PcgTools.Resources.Strings 中没有找到匹配的构造函数.您可以使用指令 Arguments 或 FactoryMethod 来创建此类型.

There has not been found a matching constructor in type PcgTools.Resources.Strings. You can use the instruction Arguments or FactoryMethod, to create this type.

文件 Strings.Designer.cs 中有一个构造函数:

There is a constructor in file Strings.Designer.cs:

namespace PcgTools.Resources {
...
public class Strings {
...

[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute
   ("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
internal Strings() {
    }

但是,它是生成的,所以我什至无法更改它.

However, it is generated so I cannot even change it.

顺便说一句,没有更改代码就可以工作(大约一周前),但是大约 30% 的时间在调试时出现了这个错误,并且在重试后没有发生这个异常.

Btw, without changing the code has worked (about a week ago), but then about 30% of the time when debugging it gave this error and after trying again this exception did not occur.

推荐答案

您的 PcgTools.Resources.Strings 类是否具有公共默认构造函数?我认为它在抱怨它无法创建对象,因为它找不到匹配的构造函数,并且由于您没有使用 Arguments,它正在寻找默认构造函数(没有参数的构造函数).构造函数必须是公共的,而不是内部的或私有的.

Does your PcgTools.Resources.Strings class have a public default constructor? I think it's complaining that it can't create the object because it can't find a matching constructor and since you're not using Arguments, it's looking for a default constructor (one with no arguments). The constructor needs to be public, not internal or private.

由于生成了代码,您需要找到解决方法.使用 FactoryMethods 的东西可能会奏效.为此,请设置另一个类,如

Since the code is generated, you'll need to find some way around this. Using the FactoryMethods thing might work. To do that, set up another class like

namespace PcgTools.Resources
{
    public static class StringResourceHelper
    {
        public static strings GetStringResources()
        {
            return new Resources.Strings();
        }
    }
}

然后在您的 XAML 中您可以执行以下操作:

then in your XAML you can do:

<res:Strings x:FactoryMethod="res:StringResourceHelper.GetStringResources" 
             x:Key="LocStrings"/>

这篇关于app.xaml 资源字符串中的 XamlParseException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆