1

在我的代码中,我试图使用其用于 windows phone 7 的 API 从 skydrive 获取文件夹。我正在使用列表框来获取文件夹。文件夹加载成功,但它挂起 UI,但是当我按下后退按钮并再次打开页面时,它工作正常。这是我的列表框

<ListBox Grid.Row="1" x:Name="lbFolders"
              Margin="0" SelectionChanged="lbFolders_SelectionChanged">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                    <Border Background="#007ccf" Margin="0,0,15,4">
                        <Image Source="/DataHub;component/images/icon_folder.png" Width="48" Height="48" />
                    </Border>
                    <TextBlock Text="{Binding Name}" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

这是我的函数,称为 async 以获取 skydrive 数据

    private void GetMetaDataAsyncSkyDriveCompleted(object sender, LiveOperationCompletedEventArgs e)
    {
        progressBar1.IsLoading = false;

        if (!String.IsNullOrEmpty(e.RawResult))
        {

            var metaData = JsonConvert.DeserializeObject<SkyDriveFolderDataContainer>(e.RawResult);

            if (metaData.data.Count > 0)
                metaData.data.ForEach(x => x.Name = x.name);

            if (SkyDriveHashSet == null) SkyDriveHashSet = new Dictionary<string, SkyDriveFolderDataContainer>();

            if (SkyDriveHashSet.ContainsKey(TargetPath))
            {
                SkyDriveHashSet[TargetPath] = metaData;
            }
            else
            {
                SkyDriveHashSet.Add(TargetPath, metaData);
            }

            if (!HasSchemaInIt(metaData.data, TargetPath))
            {
                SkyDriveDisplayData.Clear();
                if (metaData != null && metaData.data != null && metaData.data.Count > 0)
                {
                    var folderList = metaData.data.Where(x => x.type == FileConstants.SkyDriveFolderKeyword && x.shared_with.access == FileConstants.SkyDriveMyFiles).ToList();

                    if (folderList.Any())
                    {
                        folderList.ForEach(x => x.Name = x.name.Length > 15 ? x.name.Substring(0, 12) + "..." : x.name);

                        folderList.ForEach(x => SkyDriveDisplayData.Add(x));
                    }
                }
                CurrentPath.Text = TargetPath;
                SetVisibilityAfterGetMetaData();
            }

            if (App.ViewModel.SkyDriveSelectedFolderDictionary.ContainsKey(TargetPath))
                SkyDriveParentFolderId = App.ViewModel.SkyDriveSelectedFolderDictionary[TargetPath];
        }
        else
        {
            MessageBox.Show(String.Format("SkyDrive replied: {0}", e.Error.Message));
        }
    }
4

1 回答 1

1

尝试将 JsonConvert.DeserializeObject 放入 TaskEx.Run 并等待它

var parsedResult = await TaskEx.Run(() => JsonConvert.DeserializeObject<MyObject>(resultString));
于 2013-10-23T11:32:06.190 回答