Question

In my code i am trying to get folders from skydrive using its API for window phone 7. I am using listbox to get the folders. Folders load successfully but it hangs UI but when i press back button and again open the page it works fine. Here is my listbox

<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>

and here is my function which is called async to get skydrive data

    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));
        }
    }
Was it helpful?

Solution

Try to put JsonConvert.DeserializeObject into TaskEx.Run and await it

var parsedResult = await TaskEx.Run(() => JsonConvert.DeserializeObject<MyObject>(resultString));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top