Mar*_*cia 4 c# xamarin xamarin.forms
我的目标是获取保存在每个项目的图像文件夹中的嵌入图像,将其流式传输,将其转换为字节数组,并使用 PCLStorage 将其保存到设备的本地存储文件系统。我无法弄清楚的部分是如何从嵌入的图像中流式传输。
var embeddedImage = new Image { Aspect = Aspect.AspectFit };
embeddedImage.Source = ImageSource.FromResource("applicationIcon.png");
Run Code Online (Sandbox Code Playgroud)
然后我可以在有路径的情况下流式传输它,或者byte []如果我有流,我可以将它转换为 a 。下面的代码不起作用,因为找不到文件源(显然)。
string localFileUri = string.Empty;
// Get hold of the file system.
IFolder localFolder = FileSystem.Current.LocalStorage;
IFile file = await FileSystem.Current.GetFileFromPathAsync("applicationIcon.png");
using (Stream stream = await file.OpenAsync(FileAccess.Read))
{
using (var ms = new MemoryStream())
{
var byteArray = ms.ToArray();
var storeragePath = await iStorageService.SaveBinaryObjectToStorageAsync(string.Format(FileNames.ApplicationIcon, app.ApplicationId), byteArray);
app.IconURLLocal = storeragePath;
}
}
Run Code Online (Sandbox Code Playgroud)
唯一的选择似乎是使用某种资源定位器,然后为您添加的每种类型的项目维护该代码。不是很优雅。还有其他方法吗?
首先,感谢 deckertron_9000 让我走上正确的道路来解决这个问题,其次,这两个链接:
Xamarin Forms:如何在图像的 PCL 项目中使用嵌入式资源
最后,这对我有用:
首先,我将图像添加到我的 PCL 中的文件夹中。然后我确保将图像的构建操作更改为嵌入式资源。
其次,我添加了使用 System.Reflection;
第三,这段代码对我有用:
string imagePath = "NameOfProject.Assets.applicationIcon.png";
Assembly assembly = typeof(NameOfClass).GetTypeInfo().Assembly;
byte[] buffer;
using (Stream stream = assembly.GetManifestResourceStream(imagePath))
{
long length = stream.Length;
buffer = new byte[length];
stream.Read(buffer, 0, (int)length);
var storeragePath = await iStorageService.SaveBinaryObjectToStorageAsync(string.Format(FileNames.ApplicationIcon, app.ApplicationId), buffer);
app.IconURLLocal = storeragePath;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16723 次 |
| 最近记录: |