2017-06-29 62 views
0

我的文件位於'C:/Users/Username/Desktop/sample.pdf'。我可以手動打開它,並加載正常。現在,如果我放置了一個無效的鏈接,如file:///sample.pdf,顯然無法找到,Hololens應用程序將打開試圖打開PDF的Edge瀏覽器。所以,很明顯代碼正在運行。URI正確但在運行時不正確?

那麼,爲什麼在運行時在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(); 

這是拋出的異常。

拋出異常:在大會-CSharp.dll

WinRT的信息 'System.ArgumentException': 提供的URI方案無法啓動。 未處理的「Platform.InvalidArgumentException」異常被捕獲! - '參數不正確。'

該參數不正確。 提供的URI方案無法啓動',發件人:'null'。缺少try/catch塊。

Hololens中這些例外的棘手問題有時似乎與什麼不起作用有關。我試着用Google搜索這個錯誤,並且沒有任何有用的信息出現。

編輯:string uriToLaunch = @"file:///Data/myFiles/sample.pdf"; 返回WinRT信息:提供的URI方案無法啓動。 該參數不正確。

string uriToLaunch = @"/Data/myFiles/sample.pdf"; 返回UriFormatException:無效URI:無法確定URI的格式。

注意我以爲我可以從應用程序打開邊緣瀏覽器,但我無法複製此功能。即響應,積極的,我們應該說的唯一的事情,是

@"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,只是爲了測試,看看我能得到一個工作,沒有成功。

它拋出這個:

拋出異常: 'System.Exception的' 在mscorlib.ni.dll

未處理 'Platform.COMException' 異常捕獲! - '窗口句柄無效。

編輯3:這是我所能找到的錯誤。

COMException 如果異常的ErrorCode屬性的值爲0x80070578(ERROR_INVALID_WINDOW_HANDLE),則表明該方法未在UI線程上調用。

編輯4:現在,它的崩潰和恢復:

拋出異常: 'System.Exception的' 在mscorlib.ni.dll

程序 '[4084] hololensapplication.exe' 已與退出代碼-1073741189(0xc000027b)。

+0

當你說「在運行時」這是什麼樣的應用程序? WinForms,瀏覽器,控制檯? – PhillipH

+0

您添加「file:///」的任何原因? Uri處理通過它沒有它就好了。例如:https://stackoverflow.com/questions/1546419/convert-file-path-to-a-file-uri –

+0

聽起來像你可能想要使用相對路徑鏈接到您的文檔。如果應用程序無法訪問您的C:/目錄,則它不知道在哪裏查找文件。這看起來像一個ASP.NET應用程序的問題,因爲提到了一個.dll文件。嘗試把你的文件放到你的項目文件夾中並鏈接到那裏。 –

回答

0

正如@AVK指出的,UWP應用程序確實是沙盒。這意味着它們的本地環境以外的文件不可訪問。 HoloLens上有本地已知的文件夾,但解決此問題的唯一方法是使用第三方應用程序,如OneDrive。抱歉,他們不是一個更簡單的答案!