2014-09-30 49 views
3

我正在开发其使用embedded images feature管理在共享项目中的一些资产Xamarin.Forms应用。现在我有了特定于平台的渲染器,这些渲染器也需要访问这些图像。如何使用Xamarin将嵌入资源加载到Android位图?

在iOS我可以简单地使用

UIImage.FromResource(typeof(CustomMap).Assembly, "my_graphic"); 

加载的嵌入图像,并在的UIImageView或类似的使用。如何在Android特定的代码中完成相同的操作?

回答

0

不完全干净并经过测试,但您可能需要创建ImageLoaderSourceHandler类的实例并调用其LoadImageAsync方法,并将其传递给ImageSource,context和CancellationToken(可选)的实例。 你最终会得到一个Android.Graphics.Bitmap的实例。

或者,如果你想绕过表单,你可能希望创建在Android项目链接到这些图像,通常使用它们。或加载byte stream from embedded resource

+0

使用ImageLoaderSourceHandler某种方式加载其他图像干扰...但https://github.com/xamarin/mobile-samples/blob/master/EmbeddedResources/SharedLib/ResourceLoader.cs做了诀窍:var stream = EmbeddedResourceLoader.GetEmbeddedResourceStream(“my_graphic”); var bitmap = await BitmapFactory.DecodeStreamAsync(stream); – Rodja 2014-10-02 04:34:30

1

在机器人渲染您可以将图像设置ImageView的这个样子。在使用平台特定的代码时,在平台的文件结构中使用图像。在android中将图像包含在Drawable文件夹中。

 global::Android.Widget.ImageView imageview = new global::Android.Widget.ImageView(); 
     imageview.SetBackgroundResource (Resource.Drawable.my_graphic); 
+1

我问过在Android上使用嵌入式资源,而不是Android资源机制。 – Rodja 2014-10-02 04:09:14

0

这是我正在做它:

var key = "my_graphic"; 
using (var imageStream = Assembly.GetAssembly (typeof(YOURNAMESPACE.App)).GetManifestResourceStream (key)) 
       { 
        var bitmap = await BitmapFactory.DecodeStreamAsync (imageStream); 
       } 

我的嵌入式图像是在XamarinForms项目(PCL)

相关问题