Cur*_*tis 5 c# cross-platform embedded-resource ios xamarin
以下代码在 Windows 中运行良好,但是,当使用 Xamarin 并面向 iOS 时,GetManifestResourceStream() 返回 null。
Assembly assembly = Assembly.GetExecutingAssembly();
Stream stream = assembly.GetManifestResourceStream("CommunicationModel.XmlSchemas.DeviceCommon.xsd");
Run Code Online (Sandbox Code Playgroud)
我已将文件“DeviceCommon.xsd”设置为嵌入式资源。不确定为什么它没有返回有效的流。
有谁知道为什么这在使用 Xamarin 的 iOS 中不起作用?
更新:
好的,所以我按照评论中的建议并将文件设置为 Content。
我现在可以检测到该文件,但无法打开它:
if (File.Exists("DeviceCommon.xsd"))
{
try
{
myStream = new FileStream("DeviceCommon.xsd", FileMode.Open);
}
....
}
Run Code Online (Sandbox Code Playgroud)
当我运行上面的代码时,“File.Exists()”调用有效,但是当我尝试打开它时,出现以下异常:
Access to the path "/private/var/mobile/Applications/8BD48D1F-F8E8-4A80-A446-F807C6728805/UpnpUI_iOS.app/DeviceCommon.xsd" is denied.
Run Code Online (Sandbox Code Playgroud)
有人有一些想法我可以如何解决这个问题吗?
谢谢,柯蒂斯
好吧,我终于开始工作了。就我而言,我对 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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
对我来说,我要做的就是正确地添加前缀。不要查找“fooBar.baz”,而是查找“The.Name.Of.The.Assembly.The.Folder.Inside.fooBar.baz”。