XDocument.Load() 导致 System.InvalidOperationException XAML C#
XDocument.Load() causes System.InvalidOperationException XAML C#
要从用户指定的文件中解析一些 XML,我有以下 C# 代码:
private async void AskUserForFile()
{
var FilePicker = new Windows.Storage.Pickers.FileOpenPicker();
FilePicker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
FilePicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.ComputerFolder;
FilePicker.FileTypeFilter.Add(".pcs");
FilePicker.FileTypeFilter.Add(".pcp");
XMLFile = await FilePicker.PickSingleFileAsync();
ParseXML();
}
private void ParseXML()
{
XDocument Files = XDocument.Load(XMLFile.Path);
}
其中returns出现以下错误:
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.
谁能帮我弄清楚为什么会发生这种情况以及如何解决它?提前致谢。
注意:我正在为 Universal Windows 编程。
它在错误中给你答案。 "Synchronous operations should not be performed on the UI thread. Consider wrapping this method in Task.Run."
您不应在 UI 线程上同步加载文件。尝试将您的电话包装在 Task.Run 中。您可以使用 Lambda 符号来简化操作。
https://msdn.microsoft.com/en-us/library/hh195051(v=vs.110).aspx
类似于下面的代码应该可以让您克服错误,但它有点违背了异步方法的目的。
我不能给你一个关于你的项目的具体例子,但也许 'askuserforfile' 操作应该触发一个事件来构建你的 xml 以支持整个 'non-blocking-thread' 概念的现代windows 应用开发。或者挂接到模型上的 'onpropertynofitychange',该模型要求用户提供文件以在异步操作完成后构建 xml。
注意:在下面的代码片段中,删除我为调试而插入的 Task.Wait() ...除非您需要在下一个操作继续之前构建的 xml。
public class Ans34558541
{
public StorageFile XMLFile { get; private set; }
public XDocument Files { get; private set; }
private async void AskUserForFile()
{
var FilePicker = new Windows.Storage.Pickers.FileOpenPicker();
FilePicker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
FilePicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.ComputerFolder;
FilePicker.FileTypeFilter.Add(".pcs");
FilePicker.FileTypeFilter.Add(".pcp");
XMLFile = await FilePicker.PickSingleFileAsync();
Action Act = new Action(ParseXML);
Task Tsk = new Task(Act);
Tsk.Start();
Tsk.Wait();
}
private async void ParseXML()
{
String xml = await FileIO.ReadTextAsync(XMLFile);
File = XDocument.Parse(xml);
}
}
要从用户指定的文件中解析一些 XML,我有以下 C# 代码:
private async void AskUserForFile()
{
var FilePicker = new Windows.Storage.Pickers.FileOpenPicker();
FilePicker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
FilePicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.ComputerFolder;
FilePicker.FileTypeFilter.Add(".pcs");
FilePicker.FileTypeFilter.Add(".pcp");
XMLFile = await FilePicker.PickSingleFileAsync();
ParseXML();
}
private void ParseXML()
{
XDocument Files = XDocument.Load(XMLFile.Path);
}
其中returns出现以下错误:
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.
谁能帮我弄清楚为什么会发生这种情况以及如何解决它?提前致谢。
注意:我正在为 Universal Windows 编程。
它在错误中给你答案。 "Synchronous operations should not be performed on the UI thread. Consider wrapping this method in Task.Run."
您不应在 UI 线程上同步加载文件。尝试将您的电话包装在 Task.Run 中。您可以使用 Lambda 符号来简化操作。
https://msdn.microsoft.com/en-us/library/hh195051(v=vs.110).aspx
类似于下面的代码应该可以让您克服错误,但它有点违背了异步方法的目的。
我不能给你一个关于你的项目的具体例子,但也许 'askuserforfile' 操作应该触发一个事件来构建你的 xml 以支持整个 'non-blocking-thread' 概念的现代windows 应用开发。或者挂接到模型上的 'onpropertynofitychange',该模型要求用户提供文件以在异步操作完成后构建 xml。
注意:在下面的代码片段中,删除我为调试而插入的 Task.Wait() ...除非您需要在下一个操作继续之前构建的 xml。
public class Ans34558541
{
public StorageFile XMLFile { get; private set; }
public XDocument Files { get; private set; }
private async void AskUserForFile()
{
var FilePicker = new Windows.Storage.Pickers.FileOpenPicker();
FilePicker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
FilePicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.ComputerFolder;
FilePicker.FileTypeFilter.Add(".pcs");
FilePicker.FileTypeFilter.Add(".pcp");
XMLFile = await FilePicker.PickSingleFileAsync();
Action Act = new Action(ParseXML);
Task Tsk = new Task(Act);
Tsk.Start();
Tsk.Wait();
}
private async void ParseXML()
{
String xml = await FileIO.ReadTextAsync(XMLFile);
File = XDocument.Parse(xml);
}
}