我正在尝试在我的 Windows 8 应用程序中加载保存的数据。所有方法都是异步的。当await GetStoredActivitiesInFolder(groupFolder);
第二次在 for 循环中运行时,我得到一个错误,因为线程仍在处理第一个(我猜)。
错误(当我删除上面的代码行时它消失了):
mscorlib.dll 中出现“System.Runtime.InteropServices.COMException”类型的异常,但未在用户代码中处理
附加信息:应用程序调用了为不同线程编组的接口。(来自 HRESULT 的异常:0x8001010E (RPC_E_WRONG_THREAD))
我的代码:
public async void LoadActivities()
{
StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
StorageFolder activityFolder = await storageFolder.CreateFolderAsync(App.AppSettings.ActivityDirectory, CreationCollisionOption.OpenIfExists);
IReadOnlyList<StorageFolder> groupFolders = await activityFolder.GetFoldersAsync();
ObservableCollection<ActivityDataGroup> groups = new ObservableCollection<ActivityDataGroup>();
foreach (var groupFolder in groupFolders)
{
ActivityDataGroup group = new ActivityDataGroup();
group.GroupName = groupFolder.Name;
ICollection<ActivityViewModel> activities = await GetStoredActivitiesInFolder(groupFolder);
foreach (var activity in activities)
{
group.Items.Add(activity);
}
AllGroups.Add(group);
}
this.IsDataLoaded = true;
}