简体   繁体   English

Xamarin本机应用程序+ PCL:在PCL项目中共享图像

[英]Xamarin native apps + PCL: Share images in PCL project

I'm developing an app for multiple platforms using Xamarin. 我正在使用Xamarin开发适用于多个平台的应用程序。 Shared code is hosted in a separate PCL project. 共享代码托管在单独的PCL项目中。 The project contains a large collection of images, which I would also like to share via the PCL project. 该项目包含大量图像,我也想通过PCL项目共享这些图像。

Is that possible? 那可能吗? How can I access the image data from my platform-specific projects? 如何从特定于平台的项目中访问图像数据?

You should put your images in each separate project, as each platform has different images resolutions conventions, and in order to optimize and display properly the images, you'll need to save the images in different projects with the OS specific convention. 您应该将图像放在每个单独的项目中,因为每个平台具有不同的图像分辨率约定,并且为了优化和正确显示图像,您需要使用操作系统特定的约定将图像保存在不同的项目中。

You can refer the image file in the PCL project like this: 您可以像这样在PCL项目中引用图像文件:

beachImage.Source = "waterfront.png"; where waterfront is defined in each OS project by the following conventions: 在每个OS项目中按照以下约定定义了滨水区:

Android images should be placed in Resources\\Drawable\\{specificResolution} Android图片应放置在Resources \\ Drawable \\ {specificResolution}中

Android has these resolutions (taken from Android developer site ): Android具有以下解决方案(来自Android开发者网站 ):

 ldpi (low) ~120dpi mdpi (medium) ~160dpi hdpi (high) ~240dpi xhdpi (extra-high) ~320dpi xxhdpi (extra-extra-high) ~480dpi xxxhdpi (extra-extra-extra-high) ~640dpi 

Note that each resolution has it's own folder for images. 请注意,每种分辨率都有自己的图像文件夹。

In iOS you have these resolutions (taken from iOS Human Interface Guidelines ): 在iOS中,您具有以下解决方案(摘自iOS Human Interface Guidelines ): iOS解析度

You should separate your images by adding a '@2x' or '@3x' in your Resources folder. 您应该通过在“资源”文件夹中添加“ @ 2x”或“ @ 3x”来分离图像。

The ResourceManager class supported by PCL supports only a limited set of data types and Images are not one of them. PCL支持的ResourceManager类仅支持一组有限的数据类型,而Image不是其中之一。

According to the MSDN article here the best way to deal with image data types is to encode the images as Base64 encoded string resources and then decode the data as a ByteArray and then load that byte array into the Image. 根据此处的MSDN文章 ,处理图像数据类型的最佳方法是将图像编码为Base64编码的字符串资源,然后将数据解码为ByteArray ,然后将该字节数组加载到Image中。

You will have to pre-process the images to generate the Base64 strings since that can't be done at runtime. 您将不得不预处理图像以生成Base64字符串,因为在运行时无法完成。

What I finally did (until now, I could only verify on an iOS and Droid project, Windows Phone is still in the works): 我终于做了什么(到现在为止,我只能在iOS和Droid项目上进行验证,Windows Phone仍在开发中):

I return the image data from my shared project as Stream , as in: 我将共享项目中的图像数据作为Stream ,如下所示:

public Stream GetImageStream () {
    return assembly.GetManifestResourceStream (nameOfResource);
}

Images' properties are set to EmbeddedResource . 图像的属性设置为EmbeddedResource In my iOS project, I can then access my image like so: 在我的iOS项目中,我可以像这样访问我的图像:

using (var stream = myClass.GetImageStream ()) {
    var image = new UIImage (NSData.FromStream (stream));
    // do stuff with image
}

And on Android I use the following code: 在Android上,我使用以下代码:

using (var stream = myClass.GetImageStream ()) {
    var bitmap = BitmapFactory.DecodeStream (stream);
    // do stuff with bitmap
}

Feedback whether there any gotchas with this practice are welcome! 欢迎反馈是否有这种做法的陷阱! (beside the obvious sizing-issue, which I can live with for now, I could imagine that this approach circumvents any platform-specific caching mechanisms?) (除了我现在可以使用的明显的大小调整问题之外,我还可以想象这种方法绕过了任何特定于平台的缓存机制吗?)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM