我有一个简单的任务应用程序。我有 taskTextBox、addButton 和 tasksListView,其中显示了任务。我以这种方式将新项目添加到 XML 文件中:
private async void addButton_Click_1(object sender, RoutedEventArgs e)
{
try
{
var folder = ApplicationData.Current.LocalFolder;
var file = await folder.GetFileAsync("tasks.xml");
var readStream = await FileIO.ReadTextAsync(file);
var writeStream = await file.OpenStreamForWriteAsync();
XElement xElem = XElement.Parse(readStream);
xElem.Add(new XElement("Task",
new XElement("Id", Guid.NewGuid()),
new XElement("Name", QuickTaskTextBox.Text.ToString())
));
xElem.Save(writeStream);
taskTextBox.Text = "";
}
catch (Exception ex)
{
new MessageDialog(ex.Message).ShowAsync();
}
finally
{
this.Frame.Navigate(typeof(MyTasks));
}
}
但是当我在 addButton 上单击时,应用程序会抛出异常访问被拒绝 HRESULT: 0x80070005 (E_ACCESSDENIED)
它是由 this.Frame.Navigate(typeof(MyTasks)); 当我删除此代码时,一切看起来都不错,只有 ListView 不包含我新添加的任务。