c# - Assembly.GetManifestResourceStream 不适用于 iOS 上的 Xamarin

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

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

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

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

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

更新:

好的,所以我按照评论中的建议将文件设置为 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.

有人知道我该如何解决这个问题吗???

谢谢, 柯蒂斯

最佳答案

好的,我终于让它工作了。就我而言,我对 Windows .dll 和 Xamarin.iOS.dll 使用了相同的文件。尽管 namespace 相同,但我已将 .dll 项目命名为不同的名称。不幸的是,微软文档说他们使用 namespace 作为文件名的一部分。那不是真的。他们使用 .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);
                }
            }
        }

关于c# - Assembly.GetManifestResourceStream 不适用于 iOS 上的 Xamarin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18128681/

相关文章:

ios - 如何设置 UIContextualAction 的圆角半径?

c++ - 具有 float 的 C++ 程序的输出是否可预测?

c++ - 在 visual studio 和 eclipse 中打印 wchar_t 会给出不同的结果

c# - Json.Net 在序列化时弄乱了 DateTimeOffset 的时区

c# - 使用 IEnumerable<IEnumerable<string>> 的更好方法

c# - XNA/Monogame 检测 Circle 和 Rectangle 之间的碰撞不起作用

c# - 在字符串属性的 xml 序列化中缩进文本?

iphone - 创建自定义 IOS 表情符号的指南?

ios - 如何在 segue 之后设置 UILabel 的值?

javascript - NativeScript 中的 SOAP Web 服务