我有一個UWP項目需要來自XML文件的數據。我正在Blend中開發這個項目。在代碼中使用XDocument時出錯
當我添加一行:
XDocument xdoc = XDocument.Load("E:/VisualStudioRepo/ImportFromXML/ImportFromXML/Assets/XML/Book2.xml");
我得到一個錯誤:
An exception of type 'System.InvalidOperationException' occurred in System.IO.FileSystem.dll but was not handled in user code
Additional information: Synchronous operations should not be performed on the UI thread. Consider wrapping this method in Task.Run.
我可以開始一個新項目複製的只是這個錯誤,將一個XAML按鈕,下面的代碼後面:
public void GetSentence()
{
XDocument xdoc = XDocument.Load("E:/VisualStudioRepo/ImportFromXML/ImportFromXML/Assets/XML/Book2.xml");
}
private void button_Click(object sender, RoutedEventArgs e)
{
GetSentence();
}
我已經在Visual Studio控制檯應用程序中測試了我的完整Linq代碼,並且可以讀取完美的xml和輸出所有或部分xml文件的列表。所以XML文件是可以的,我的基本方法似乎工作。它只是在UWP環境中不起作用。
我真的不得不「在Task.Run中包裝這個方法嗎?如果是這樣,請告訴我該怎麼做。我感到驚訝的是,儘管經過了幾個小時的搜索,我仍然無法在網上看到任何其他人,所以我懷疑我做錯了什麼。
我是一個自認愛好編程愛好者(也是一位「老傢伙」),所以請幫助我一個明確的解釋,請不要告訴我回到學校,因爲這是一個真正很久以前!
我有點困惑..文件路徑是否在控制檯應用程序中用'/'s工作? – Slai
是的,與/ s完美配合。 – RichPro