【问题标题】:Assembly.GetManifestResourceStream not working with Xamarin on iOSAssembly.GetManifestResourceStream 不适用于 iOS 上的 Xamarin
【发布时间】:2013-08-08 14:28:34
【问题描述】:

以下代码在 Windows 中运行良好,但是,当使用 Xamarin 并以 iOS 为目标时,GetManifestResourceStream() 返回 null。

Assembly assembly = Assembly.GetExecutingAssembly();
Stream stream = assembly.GetManifestResourceStream("CommunicationModel.XmlSchemas.DeviceCommon.xsd");

我已将文件“DeviceCommon.xsd”设置为嵌入式资源。不知道为什么它没有返回有效的流。

有人知道为什么这在使用 Xamarin 的 iOS 中不起作用吗?

更新:

好的,所以我按照 cmets 中的建议将文件设置为 Content。

我现在可以检测到文件,但无法打开它:

if (File.Exists("DeviceCommon.xsd"))
{
  try
  {
    myStream = new FileStream("DeviceCommon.xsd", FileMode.Open);
  }
....
}

当我运行上面的代码时,'File.Exists()' 调用有效,但是当我尝试打开它时,我得到以下异常:

Access to the path "/private/var/mobile/Applications/8BD48D1F-F8E8-4A80-A446-F807C6728805/UpnpUI_iOS.app/DeviceCommon.xsd" is denied.

任何人都知道我该如何解决这个问题???

谢谢, 柯蒂斯

【问题讨论】:

  • 我不希望这会起作用,TBH。
  • 我通常只是将文件作为内容包含在我的项目中。由于所有内容都打包成一个包含嵌入式资源文件的包,这对我来说不是什么大问题。
  • 这应该可以 - 我猜该文件由于某种原因没有正确嵌入到程序集中。您可以在最终程序集上使用 Reflector 来检查资源是否存在(以及名称是什么)。

标签: c# ios cross-platform xamarin embedded-resource


【解决方案1】:

好的,我终于让它工作了。就我而言,我对 windows .dll 和 Xamarin.iOS.dll 使用了相同的文件。尽管名称空间相同,但我对 .dll 项目的命名不同。不幸的是,微软文档说他们使用命名空间作为文件名的一部分。这不是真的.. 他们使用 .dll 名称作为命名空间的一部分。只是细微的差别,但意义重大。

所以,重点是.. 我将文件属性设置为:“嵌入式资源”和“不要复制”。我需要处理的资源都是带有 .xsd 扩展名的文件,所以我只是遍历所有资源名称并使用以 .xsd 结尾的那些。这样,无论他们使用什么操作系统,名称都是正确的,因为我以编程方式检索它并且没有对其进行硬编码:

        Assembly assembly = Assembly.GetExecutingAssembly();
        string[] resources = assembly.GetManifestResourceNames();
        foreach (string resource in resources)
        {
            if(resource.EndsWith(".xsd"))
            {
                Stream stream = assembly.GetManifestResourceStream(resource);
                if (stream != null)
                {
                    XmlSchema schema = XmlSchema.Read(stream, null);
                    _schemas.Add(schema);
                }
            }
        }

【讨论】:

    【解决方案2】:

    对我来说,我要做的就是正确地为其添加前缀。不要寻找“fooBar.baz”,而是寻找“The.Name.Of.The.Assembly.The.Folder.Inside.fooBar.baz”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-20
      • 2017-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多