Problem with async call like GetFilesAsync()

Jun 15, 2012 at 8:16 AM
Edited Jun 15, 2012 at 8:24 AM

Hi Andy,
do you have any idea, why the following overriden base mothod doesn't work?
The query never returns a value:

   public class PhotosDataListSource : SimpleDataListSource<PhotoInfo>
    {

        // *** Overriden Base Methods ***
        public async override Task<int> GetCountAsync()
        {
            var queryOptions = new QueryOptions(CommonFileQuery.OrderByName, new[] { ".jpg" });
            queryOptions.IndexerOption = IndexerOption.UseIndexerWhenAvailable;

            StorageFileQueryResult query =
                      KnownFolders.PicturesLibrary.CreateFileQueryWithOptions(queryOptions);

            var files = await query.GetItemCountAsync();
            return (int)files;
        }
}
If I use normal async method, I get a value back:

    public class PhotoService
    {
        public async Task<int> GetCountAsync()
        {
            var queryOptions = new QueryOptions(CommonFileQuery.OrderByName, new[] { ".jpg" });
            queryOptions.IndexerOption = IndexerOption.UseIndexerWhenAvailable;

            StorageFileQueryResult query =
                      KnownFolders.PicturesLibrary.CreateFileQueryWithOptions(queryOptions);

            var files = await query.GetItemCountAsync();
            return (int)files;
        }
    }
Thanks, really great little Framework
Dirk
Coordinator
Jun 16, 2012 at 5:34 PM

Hi Dirk,

I've just had a bit of a play and it may help you if wrap the calls into a try-catch block and put a breakpoint on the "throw;" call (see code at the bottom of this post). Due to the way that the VirtualizingDataList calls this method the resulting exception may not be picked up by the debugger. By deliberately catching the exception and using a debugger breakpoint you can see what the exception is.

One thing to check is that you have ticked the "Pictures Library Access" in the "Capabilities" tab of the package manifest. If you haven't done this then you will get an "Access denied" exception when you hit the breakpoint I suggested above. Once I had ticked this I was able to get your code to work fine (and you should then be able to remove the error catching code if you wish).

Let me know how you get on - if the exception is something different, or if the breakpoint is never hit I can investigate further.

Regards,

    Andy

 

public async override Task<int> GetCountAsync()
{
    var queryOptions = new QueryOptions(CommonFileQuery.OrderByName, new[] { ".jpg" });
    queryOptions.IndexerOption = IndexerOption.UseIndexerWhenAvailable;

    try
    {
        StorageFileQueryResult query =
                    KnownFolders.PicturesLibrary.CreateFileQueryWithOptions(queryOptions);

        var files = await query.GetItemCountAsync();
        return (int)files;
    }
    catch (Exception e)
    {
        throw;
    }            
}