【问题标题】:Porting .NET Library for reuse in UWP App移植 .NET 库以在 UWP 应用程序中重用
【发布时间】:2018-05-11 15:06:40
【问题描述】:

我正在开展一个项目,以在 UWP 应用程序中重用我的一个旧 .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 命名空间“污染”我的库。

提前致谢

【问题讨论】:

    标签: c# .net uwp .net-standard


    【解决方案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

    【讨论】:

      猜你喜欢
      • 2018-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-09
      • 2016-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多