从 UWP 访问作为资源嵌入到 Net Standard 2.0 class 库中的 StorageFile
Access a StorageFile embedded as a resource in a Net Standard 2.0 class libary from UWP
我们如何从 UWP 访问作为资源嵌入到 .Net Standard 2 class 库中的文件?问题 return 是一个流。我们如何 return 存储文件?
该文件位于名为 Shared 的 .Net Standard 2 项目中,该项目与 UWP 项目位于同一解决方案中。该文件位于 Shared/Reports 中并设置为复制以作为 EmbeddedResource 输出。
我试过了
Uri resourcePath = new Uri("ms-appx:///Shared/Reports/MyReport");
StorageFile myFile = await StorageFile.GetFileFromApplicationUriAsync(resourcePath);
和
Uri resourcePath = new Uri("Shared/Reports/MyReport");
StorageFile myFile = await StorageFile.GetFileFromApplicationUriAsync(resourcePath);
两者都 return FileNotFoundException
.
StorageFile.GetFileFromApplicationUriAsync
是合适的方法吗?如果是这样,我该如何正确格式化 URI?
Yes but the example given A: doesn't work and B: uses methods specific to pdfiles.
没关系。我在上面评论中提到的 link 只是一个类似的问题。我的目的是告诉您将流保存到 StorageFile 中。不过你好像不太熟悉。
我做了一个代码示例供你参考。
public class Class1
{
/// <summary>
/// This is the method in my .Net Standard library
/// </summary>
/// <returns></returns>
public static Stream GetImage()
{
var assembly = typeof(Class1).GetTypeInfo().Assembly;
Stream stream = assembly.GetManifestResourceStream("ClassLibrary1.Assets.dog.jpg");
return stream;
}
}
/// <summary>
/// This method is used to create a StorageFile and save the stream into it, then return this StorageFile
/// </summary>
/// <returns></returns>
private async Task<StorageFile> GetFile()
{
StorageFile storageFile = await ApplicationData.Current.TemporaryFolder.CreateFileAsync("Temp.jpg",CreationCollisionOption.ReplaceExisting);
using (var Srcstream = ClassLibrary1.Class1.GetImage())
{
using (var targetStream = await storageFile.OpenAsync(FileAccessMode.ReadWrite))
{
using (var reader = new DataReader(Srcstream.AsInputStream()))
{
var outpustream = targetStream.GetOutputStreamAt(0);
await reader.LoadAsync((uint)Srcstream.Length);
while (reader.UnconsumedBufferLength >0)
{
uint datatoend = reader.UnconsumedBufferLength > 128 ? 128 : reader.UnconsumedBufferLength;
IBuffer buffer = reader.ReadBuffer(datatoend);
await outpustream.WriteAsync(buffer);
}
await outpustream.FlushAsync();
}
}
}
return storageFile;
}
我们如何从 UWP 访问作为资源嵌入到 .Net Standard 2 class 库中的文件?问题
该文件位于名为 Shared 的 .Net Standard 2 项目中,该项目与 UWP 项目位于同一解决方案中。该文件位于 Shared/Reports 中并设置为复制以作为 EmbeddedResource 输出。
我试过了
Uri resourcePath = new Uri("ms-appx:///Shared/Reports/MyReport");
StorageFile myFile = await StorageFile.GetFileFromApplicationUriAsync(resourcePath);
和
Uri resourcePath = new Uri("Shared/Reports/MyReport");
StorageFile myFile = await StorageFile.GetFileFromApplicationUriAsync(resourcePath);
两者都 return FileNotFoundException
.
StorageFile.GetFileFromApplicationUriAsync
是合适的方法吗?如果是这样,我该如何正确格式化 URI?
Yes but the example given A: doesn't work and B: uses methods specific to pdfiles.
没关系。我在上面评论中提到的 link 只是一个类似的问题。我的目的是告诉您将流保存到 StorageFile 中。不过你好像不太熟悉。
我做了一个代码示例供你参考。
public class Class1
{
/// <summary>
/// This is the method in my .Net Standard library
/// </summary>
/// <returns></returns>
public static Stream GetImage()
{
var assembly = typeof(Class1).GetTypeInfo().Assembly;
Stream stream = assembly.GetManifestResourceStream("ClassLibrary1.Assets.dog.jpg");
return stream;
}
}
/// <summary>
/// This method is used to create a StorageFile and save the stream into it, then return this StorageFile
/// </summary>
/// <returns></returns>
private async Task<StorageFile> GetFile()
{
StorageFile storageFile = await ApplicationData.Current.TemporaryFolder.CreateFileAsync("Temp.jpg",CreationCollisionOption.ReplaceExisting);
using (var Srcstream = ClassLibrary1.Class1.GetImage())
{
using (var targetStream = await storageFile.OpenAsync(FileAccessMode.ReadWrite))
{
using (var reader = new DataReader(Srcstream.AsInputStream()))
{
var outpustream = targetStream.GetOutputStreamAt(0);
await reader.LoadAsync((uint)Srcstream.Length);
while (reader.UnconsumedBufferLength >0)
{
uint datatoend = reader.UnconsumedBufferLength > 128 ? 128 : reader.UnconsumedBufferLength;
IBuffer buffer = reader.ReadBuffer(datatoend);
await outpustream.WriteAsync(buffer);
}
await outpustream.FlushAsync();
}
}
}
return storageFile;
}