【问题标题】:How to get Image.Source as string如何获取 Image.Source 作为字符串
【发布时间】:2014-12-26 20:24:21
【问题描述】:

It's easy to set a source for an image in Xamarin:

using Xamarin.Forms;
Image image = new Image;
image.Source = "someImage.jpg";

但是我不能做反向操作。
例如:给定已设置来源的图像,打印来源。

Console.WriteLine ("Print Image source ==> {0}",image.Source);
Console.WriteLine ("Print Image source ==> {0}",image.Source.ToString());

... 还有一些不连贯的组合。

谁能告诉我如何从图像中获取源(带字符串)。

【问题讨论】:

  • Image 是什么,以什么方式读取属性不起作用?我还看到您使用source 设置它并使用Source 读取它 - 它们实际上是单独的属性,还是这是一个错字,在这种情况下,您可以显示您实际尝试编译的代码吗?
  • 第一个 sn-p 也不起作用。假设您正在谈论 System.Drawing.Image,则没有公共 source 属性。 BCL 公共成员名称从不以小写开头。
  • @Asad:注意 xamarin 标签。 iosapi.xamarin.com/?link=T%3aXamarin.Forms.Image
  • C# 中有几个Image 类型。你说的是哪一个?
  • Image 是一个抽象类。你的意思是Bitmap

标签: c# image xamarin xamarin.forms


【解决方案1】:

Xamarin.Forms Image.Source 属性的类型为 ImageSource

Xamarin.Forms 中的

ImageSource 有一些继承此类的类,例如:-

  • 文件图像源
  • StreamImageSource
  • UriImageSource

您可以检查 Image.Source 以查看 Image.Source 中使用了什么实现,然后进行转换它,并访问铸造对象的属性。

例如(假设 ImageSource 是一个 FileImageSource)你会得到类似的东西:-

Xamarin.Forms.Image objImage;
..
..

..
if (objImage.Source is Xamarin.Forms.FileImageSource)
{
    Xamarin.Forms.FileImageSource objFileImageSource = (Xamarin.Forms.FileImageSource)objImage.Source;
    //
    // Access the file that was specified:-
    string strFileName = objFileImageSource.File;
}

【讨论】:

    【解决方案2】:

    看起来Xamarin.Forms.Image 有一个Source 类型为Xamarin.Forms.ImageSource 的属性,它具有来自string 的隐式转换。这就是为什么你可以做Image.Source = "someImage.jpg",但它没有办法返回,可能是因为它只使用字符串来查找文件并加载它。如果您需要您加载的文件的名称,您必须自己跟踪它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-30
      • 2012-02-09
      • 1970-01-01
      • 1970-01-01
      • 2022-07-25
      • 1970-01-01
      • 2015-07-19
      相关资源
      最近更新 更多