首页 > 解决方案 > 如何从 Xamarin.Forms 中的流中获取图像的路径

问题描述

我将图像转换为流并设置为 Xamarin.Forms.Image.Source,在单击按钮时,我试图访问相同的图像,但在后面的代码中,我无法将图像作为 StreamImagesSource 获取访问获取流。

Assembly _assembly = this.GetType().Assembly;
Stream stream = _assembly.GetManifestResourceStream("DataGrithWithImagePdfExport.Resources.Jk.png");
ImageSource source = ImageSource.FromStream(() => stream);
///Added the image to the ImageSource property in Model

Persons.Add(new Person() { Name = "John Smith", Photo = source });

请帮助我克服这个问题。谢谢。

标签: c#xamarin.forms

解决方案


@Bala Malarkodi,您为什么要访问流?您可以简单地获取文件路径,假设如果您在图像文件夹下有 sample.png 然后像这样设置您的属性,

public class Person
{
   public string Name { get; set; }
   public string ImageURL { get; set; }
}

像这样准备你的模型,

Persons.Add(new Person() 
 {
   Name = "John Smith",
   ImageURL = "Images/sample.png" 
 });

并在您的页面 (xaml) 中绑定此 ImageURL。


推荐阅读