【问题标题】:NodaTime TypeInitializationException Device Only, Release OnlyNodaTime TypeInitializationException 仅限设备,仅限发布
【发布时间】:2014-10-11 17:44:59
【问题描述】:

我在使用 NodaTime 时遇到了 TypeInitializationException,但仅在 Release 中且仅在设备上。

这是堆栈跟踪:

System.TypeInitializationException: The type initializer for 'Patterns' threw an exception. ---> System.TypeInitializationException: The type initializer for 'NodaTime.Text.ZonedDateTimePattern' threw an exception. ---> System.TypeInitializationException: The type initializer for 'NodaTime.DateTimeZone' threw an exception. ---> System.TypeInitializationException: The type initializer for 'NodaTime.Text.OffsetPattern' threw an exception. ---> System.Resources.MissingManifestResourceException: Exception of type 'System.Resources.MissingManifestResourceException' was thrown.
   at System.Resources.ResourceManager.GetString(String name, CultureInfo culture)
   at NodaTime.Globalization.NodaFormatInfo.get_OffsetPatternLong()
   at NodaTime.Text.OffsetPatternParser.ParsePartialPattern(String patternText, NodaFormatInfo formatInfo)
   at NodaTime.Text.OffsetPatternParser.CreateGeneralPattern(NodaFormatInfo formatInfo)
   at NodaTime.Text.OffsetPatternParser.ParsePartialPattern(String patternText, NodaFormatInfo formatInfo)
   at NodaTime.Text.OffsetPatternParser.ParsePattern(String patternText, NodaFormatInfo formatInfo)
   at NodaTime.Text.FixedFormatInfoPatternParser`1.<>c__DisplayClass0.<.ctor>b__2(String patternText)
   at NodaTime.Utility.Cache`2.GetOrAdd(TKey key)
   at NodaTime.Text.OffsetPattern.Create(String patternText, NodaFormatInfo formatInfo)
   at NodaTime.Text.OffsetPattern..cctor()
   --- End of inner exception stack trace ---
   at NodaTime.TimeZones.FixedDateTimeZone.MakeId(Offset offset)
   at NodaTime.DateTimeZone.BuildFixedZoneCache()
   at NodaTime.DateTimeZone..cctor()
   --- End of inner exception stack trace ---
   at NodaTime.LocalDateTime.InUtc()
   at NodaTime.Text.ZonedDateTimePattern..cctor()
   --- End of inner exception stack trace ---
   at NodaTime.Text.ZonedDateTimePattern.CreateWithInvariantCulture(String patternText, IDateTimeZoneProvider zoneProvider)
   at NodaTime.Text.ZonedDateTimePattern.Patterns..cctor()
   --- End of inner exception stack trace ---
   at NodaTime.ZonedDateTime.ToString(String patternText, IFormatProvider formatProvider)
   at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args)
   at System.Text.StringBuilder.AppendFormat(String format, Object[] args)
   at Models.EventSummary.get_DisplayDate()

【问题讨论】:

  • 嗯。有趣的。你的文化是什么?我希望它会退回到我指定为大会的任何文化......这不像有任何其他文化具有该资源。嗯。
  • 当您说它“仅在发布中”时 - 大概您在两种情况下都使用相同的 Noda Time 程序集,安装了 bu NuGet?
  • 是的,一切都是通过 NuGet 设置的。
  • 好的,所以 NodaTime.dll 在发布和调试时应该是相同的。听起来这很可能是 stackoverflow.com/questions/26140155 的副本
  • 根据 Twitter 上的其他 cmets,听起来 WP8.1 不支持 resx,而 resw 支持。我需要进一步调查一下……如果需要物理设备,这将是一件痛苦的事情:(

标签: c# windows-phone-8 windows-phone-8.1 nodatime


【解决方案1】:

workaround by Phil Hoff 为我们解决了这个问题。创建以下WindowsRuntimeResourceManager 类:

/// <summary>
/// from http://blogs.msdn.com/b/philliphoff/archive/2014/11/19/missingmanifestresourceexception-when-using-portable-class-libraries-in-winrt.aspx
/// </summary>
public class WindowsRuntimeResourceManager : ResourceManager
{
    private readonly ResourceLoader _resourceLoader;

    private WindowsRuntimeResourceManager(string baseName, Assembly assembly)
        : base(baseName, assembly)
    {
        _resourceLoader = ResourceLoader.GetForViewIndependentUse(baseName);
    }

    public static void InjectIntoResxGeneratedApplicationResourcesClass(Type resxGeneratedApplicationResourcesClass)
    {
        resxGeneratedApplicationResourcesClass.GetRuntimeFields()
          .First(m => m.Name == "resourceMan")
          .SetValue(null, new WindowsRuntimeResourceManager(resxGeneratedApplicationResourcesClass.FullName, resxGeneratedApplicationResourcesClass.GetTypeInfo().Assembly));
    }

    public override string GetString(string name, CultureInfo culture)
    {
        return _resourceLoader.GetString(name);
    }
}

在使用 NodaTime 之前(例如,在您的 App 构造函数中)替换 NodaTime 程序集的资源管理器:

        Assembly nodaTimeAssembly = typeof(LocalDate).GetTypeInfo().Assembly;

        Type messagesResource = nodaTimeAssembly.GetType("NodaTime.Properties.Messages");
        Type patternResource = nodaTimeAssembly.GetType("NodaTime.Properties.PatternResources");

        WindowsRuntimeResourceManager.InjectIntoResxGeneratedApplicationResourcesClass(messagesResource);
        WindowsRuntimeResourceManager.InjectIntoResxGeneratedApplicationResourcesClass(patternResource);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多