URI 正确但在 运行 时不正确?

URI correct but incorrect at run-time?

我的文件,位于 'C:/Users/Username/Desktop/sample.pdf' 在那里。我可以手动打开它并且加载正常。现在,如果我输入一个无效的 link,例如 file:///sample.pdf,显然无法找到,Hololens 应用程序将打开 Edge 浏览器尝试打开 pdf。所以,很明显代码正在运行。

那么为什么在 运行 时加载到 Hololens 时这个 uri 不正确?

    string uriToLaunch = "file:///C:/Users/Username/Desktop/sample.pdf";

    // Create a Uri object from a URI string 
    var uri = new Uri(uriToLaunch);

    Task task = new Task(

        async () =>
        {
            // Launch the URI
            var success = await Windows.System.Launcher.LaunchUriAsync(uri);

            if (success)
            {
                // URI launched
            }
            else
            {
                // URI launch failed
            }
        });

    task.Start();
    task.Wait();

这里是抛出的异常。

抛出异常:'System.ArgumentException' in Assembly-CSharp.dll

WinRT 信息: 无法启动提供的 URI 方案。 捕获到未处理的 'Platform.InvalidArgumentException' 异常! - 'The parameter is incorrect.'

参数不正确。 无法启动提供的 URI 方案。',发件人:'null'。缺少 try/catch 个方块。

Hololens 中这些异常的棘手之处在于,有时它们似乎与不起作用的东西无关。我尝试用谷歌搜索错误,但没有找到任何有用的信息。

编辑: string uriToLaunch = @"file:///Data/myFiles/sample.pdf"; returns WinRT 信息:无法启动提供的 URI 方案。 参数不正确。

string uriToLaunch = @"/Data/myFiles/sample.pdf"; returns UriFormatException:无效的 URI:无法确定 URI 的格式。

注意 我以为我可以从应用程序打开 Edge 浏览器,但我在复制此功能时遇到了问题。我们可以说,唯一做出积极回应的是

@"ms-appx:///Data/myFiles/sample.pdf";

这将打开一个单独的对话框,我将通过该对话框前往 MS 商店尝试购买可打开 ms-appx 文件的应用程序。

编辑 2:

    FileOpenPicker openPicker = new FileOpenPicker();
    openPicker.ViewMode = PickerViewMode.Thumbnail;
    openPicker.ViewMode = PickerViewMode.Thumbnail;
    openPicker.FileTypeFilter.Add(".jpg");
    openPicker.FileTypeFilter.Add(".jpeg");
    openPicker.FileTypeFilter.Add(".png");

    Task task = new Task(
    async () =>
    {
        StorageFile file = await openPicker.PickSingleFileAsync();
        if (file != null)
        {
            // Application now has read/write access to the picked file
            Debug.Log("Picked photo: " + file.Name);
        }
        else
        {
            Debug.Log("Cancelled");
        }
    });

    task.Start();
    task.Wait();

我已经尝试实现这个简单的 FileOpenPicker,只是为了测试看看我是否能让它工作,但没有成功。

它抛出这个:

抛出异常:mscorlib.ni.dll

中的 'System.Exception'

捕获到未处理的'Platform.COMException'异常! - '无效 window 句柄。

编辑 3: 这是我能找到的所有错误。

COM异常 如果异常的 ErrorCode 属性 的值为 0x80070578 (ERROR_INVALID_WINDOW_HANDLE),这表明该方法未在 UI 线程上调用。

编辑 4: 现在崩溃并返回:

抛出异常:mscorlib.ni.dll

中的 'System.Exception'

程序“[4084] hololensapplication.exe”已退出,代码为 -1073741189 (0xc000027b)。

正如@AVK 指出的那样,UWP 应用程序是沙盒化的。这意味着无法访问 Hololens 本地环境之外的文件。 HoloLens 上有本地已知文件夹,但解决此问题的唯一方法是使用第三方应用程序,如 OneDrive、Azure 等