首页 文章

移植.NET库以便在UWP App中重用

提问于
浏览
1

我正在开发一个项目来重用UWP App中的一个旧.NET库 . 我阅读了几篇关于.NET 2.0标准库的文章 .

https://docs.microsoft.com/en-us/windows/uwp/porting/desktop-to-uwp-migrate

我按照那篇文章中的步骤创建了一个新的.NET Standard 2.0库,将我的旧源文件复制到新的库项目中并安装了缺少的NuGet包并编译了项目而没有任何错误 .

下一步是创建一个UWP应用程序并添加库项目作为参考 .

我的图书馆正在将文件从一种格式转换为另一种格式 . 因此它使用System.IO和System.Xml.Serialization命名空间 . 我知道在UWP和Windows应用商店应用中更改了文件读取和写入的安全设置,这会导致一些问题 .

我正在打开这样的文件

FileOpenPicker fileOpenPicker = new FileOpenPicker();

fileOpenPicker.SuggestedStartLocation = PickerLocationId.Desktop;

fileOpenPicker.FileTypeFilter.Clear();

fileOpenPicker.FileTypeFilter.Add(".txt");

StorageFile file = await fileOpenPicker.PickSingleFileAsync();

// and here I am calling my Library

var doc = ParserFactory.Parse(file.Path);

// and that's what my library is trying to do

....
var myfile = File.OpenRead(filename)

// File.OpenRead throws a Security Exception.

我以这种方式设计了我的库的接口,你必须传递文件路径,库本身将关注其余部分 .

那么有人知道避免安全例外的解决方案吗?我真的很感激不要重写我的库和/或用Windows.Storage命名空间“污染”我的库 .

提前致谢

1 回答

  • 1

    我的建议是为 ParserFactory.Parse(Stream stream) 创建一个重载并使用 StorageFile 打开 Stream

    var file = await fileOpenerPicker.PickSingleFileAsync();
    var stream = await file.OpenStreamForReadAsync();
    var doc = ParserFactory.Parse(stream);
    

    不确定你的 Parse 函数是如何工作的,但希望你所关心的只是 Stream 实现,而不是 FileStream .

相关问题