【问题标题】:Xamarin Forms Load image from folder inside projectXamarin Forms 从项目内的文件夹加载图像
【发布时间】:2019-12-21 06:38:28
【问题描述】:

我有问题。我正在尝试将图像保存到我的项目中的文件夹(不是资源文件夹!)并将图像从该文件夹加载到图像持有者作为源。我希望将图像保存在一个名为 TempImages 的文件夹中,我的应用名称是 MyApp。这是我现在拥有的代码:

保存

using (var image = args.Surface.Snapshot())
using (var data = image.Encode(SKEncodedImageFormat.Png, 80))
using (var stream = File.OpenWrite(Path.Combine("MyApp.TempImages", "CreatedImage.png")))
{
    data.SaveTo(stream);
}

开幕

string resourceID = string.Format("MyApp.TempImages.CreatedImage.png");
Assembly assembly = GetType().GetTypeInfo().Assembly;
Stream stream = assembly.GetManifestResourceStream(resourceID);
imgCanvas.Source = ImageSource.FromFile(resourceID);

但我认为File.OpenWrite 我电脑上的本地文件意味着,但我不确定。因此我不确定我是否正确打开了文件。现在我得到了保存路径不存在的错误。

我该如何解决这个问题?

【问题讨论】:

  • 应用程序包是只读的。阅读有关文件处理的文档:docs.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/data/…
  • 好的,要打开图像,我需要这样做:imgCanvas.Source = ImageSource.FromResource(path, typeof(EmbeddedImages).GetTypeInfo().Assembly); 但这给了我在EmbeddedImages 上的错误,因为在当前上下文中不存在?
  • EmbeddedImages - 这应该是项目中任何类的名称。但是您仍然无法对其进行写入。
  • 我没有名为 EmbeddedImages 的类?以及如何将图像保存为 EmbeddedResource?
  • 将“EmbeddedImages”替换为项目中.a 类的名称。而且您不能在运行时将图像保存为 EmbeddedResource。您只需将其保存为常规文件即可。

标签: c# xamarin xamarin.forms xamarin.android xamarin.ios


【解决方案1】:

您应该能够在应用程序可写路径之一中创建所需的任何文件夹结构

var path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

var folder = Path.Combine(path,"MySpecialFolder");

Directory.CreateDirectory(folder);

var file = Path.Combine(folder,"MyImage.png");

File.WriteAllBytes(file,data);

var image = File.ReadAllBytes(file);

【讨论】:

  • 谢谢,我将根据您的指示创建的代码与您的遮阳篷结合在一起!!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多