【问题标题】:Invalid il code System.IO.Compression.ZipFile.OpenRead() Method body is empty无效的 il 代码 System.IO.Compression.ZipFile.OpenRead() 方法体为空
【发布时间】:2014-01-16 15:37:02
【问题描述】:

我正在使用 Xamarin 和 MVVMCross 实现一个 android 应用程序。在我的 PCL 中,我有一个 ZipUtility 类和以下方法:

public NoDataResponse UnCompressZipFile(string path, string filename)
   {
       NoDataResponse response = new NoDataResponse();
       StringBuilder sb = new StringBuilder();
       sb.AppendLine(string.Format("\r\nUnzipping '{0}' in '{1}' folder...", filename, path));

       try
       {       


           using (ZipArchive archive = ZipFile.Open(filename, ZipArchiveMode.Read))
           {
               foreach (System.IO.Compression.ZipArchiveEntry entry in archive.Entries)
               {
                   if (entry.FullName.EndsWith(".txt", StringComparison.OrdinalIgnoreCase))
                   {
                       System.IO.Compression.ZipFile.ExtractToDirectory(filename, path);
                       break;
                   }
               }
           }


           response.IsCallSuccessful = true;
           sb.AppendLine(string.Format("\r\nFinished Unzipping file {0}.\r\n", filename));

           response.BusinessEntityMessage = sb.ToString();
       }

       catch (Exception ex)
       {
           response.IsCallSuccessful = false;
           response.BusinessEntityMessage = "\r\nUNZIPPING ERROR: " + ex.Message + "\r\n";
       }

       return response;
   }

我方法中的 ZipFile 类是 System.IO.Compression(System.IO.Compression.FileSystem 命名空间)的一部分。

当我执行该方法时,我收到以下错误:

System.InvalidProgramException:System.IO.Compression.ZipFile 中的 IL 代码无效:Open (string,System.IO.Compression.ZipArchiveMode):方法体为空。 在 C:\Sandbox\HHT-ANDROID\HHTApp\HHT.Core\Globals\ZipUtility.cs:32 中的 HHT.Core.Globals.ZipUtility.UnCompressZipFile(System.String 路径,System.String 文件名)[0x00033] >

我不确定缺少什么,因为之前在 PCL 中相同的代码运行良好。

我还尝试使用我的 DROIDUI 项目中的依赖注入 (N=31 slodge) 运行相同的类,但仍然遇到相同的错误。

我将不胜感激任何关于此的 cmets 或想法。

【问题讨论】:

    标签: c# xamarin.android xamarin mvvmcross portable-class-library


    【解决方案1】:

    听起来您正在将参考程序集部署到您的 Android 设备(参考程序集仅用于编译,因此不包含 IL)。

    您使用的是哪个版本的System.IO.Compression?我假设您使用的是Microsoft.Bcl.Compression NuGet 包?

    我们的 NuGet 包不提供适用于 Android 的实现。事实上,我们只为 Windows Phone 提供了一个实现——对于所有其他平台,我们使用该平台附带的那个。

    我不确定 Xamarin 是否支持 System.IO.Compression.dll(包括 ZipArchive)。它以前可能工作的原因是因为您在安装 NuGet 包后重新定位了 PCL。

    【讨论】:

    • 非常感谢您的 cmets。其实你解决了我的问题。正如您所推测的,我提供了一个参考程序集。一旦我从 C:\Windows\Microsoft.Net\Framework 引用 System.IO.Compression,它就起作用了。再次感谢您。
    • 不客气。不过,这听起来不像是正确的解决方法——您不应该直接添加对平台实现的引用,因为这可能会破坏其他场景。但是,如果这解决了您的问题,那么这听起来像是 Xamarin 方面的错误。更好的临时解决方法是继续使用 PCL 中的 NuGet 包并确保您的应用不部署 System.IO.Compression.dll。
    • 我昨天更新到最新的 Xamarin Android 版本,我的 ZipArchive 类让我 System.NotImplementedException:请求的功能未实现。我不知道我使用的以前版本的 Xamarin Android 是否存在错误,因为它允许类完全正常运行。当您说继续使用 Nuget 包时,您的意思是 Microsoft.Bcl.Compression 正确吗?老实说,我还没有使用过那个,但正如你所说,你只为 Windows Phone 提供实现,所以它甚至不允许我从 Nuget 安装它。
    • 我已经删除了所有平台特定的 dll,我将使用 pcl 提供的任何对象。不幸的是,现在我必须从头开始构建归档机制。
    • NuGet 包仅包含 Windows Phone 的实现,但它使您能够以可移植的方式使用特定于平台的实现。我们在两个平台(.NET 4.5 和 Windows Store)上发布 ZipArchive。使用设置的 NuGet 包扩展到 Windows Phone。一旦 Xamarin 实现了它,它也应该可以通过 NuGet 包在 PCL 中自动使用。让我 ping Miguel,看看他们对支持压缩有何想法。
    【解决方案2】:

    根据您目前的发现,我打赌您可以通过将Xamarin.Android System.IO.Compression.dll from the Mac installer 复制到以下位置来修复错误:

    C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\MonoAndroid\v1.0\

    Xamarin.Android Windows 安装程序当前(意外地)缺少此程序集。已经提交了一个关于此的错误,因此该程序集应该默认存在于未来的版本中。

    如果这不起作用,请务必报告该结果,以便我们进一步调查!

    【讨论】:

    • 嗨,布伦丹。我在我的 DroidUI 项目中引用了您在 Xamarin 开发人员站点中附加的 zip 文件的内容,但仍然是同样的问题。 System.NotImplementedException:请求的功能未实现。在 System.IO.Compression.ZipArchive..ctor(System.IO.Stream 流)[0x00000] 在 System.IO.Compression.ZipFile.Open(System.String archiveFileName,ZipArchiveMode mode, System.Text.Encoding entryNameEncoding) [0x00000] in :0 }
    • 天哪!确实是的。问题比我想象的要简单,Immo 完全正确:ZipArchive has not yet been implemented in Mono
    猜你喜欢
    • 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
    相关资源
    最近更新 更多