locked
Retrieving list of folders inside a folder asynchronously. RRS feed

  • Question

  • Here are the codes I have tried..

    StorageFolder folder = ApplicationData.Current.LocalFolder;
                List<StorageFolder> list = await folder.GetFoldersAsync();
                foreach (StorageFolder fldr in list)
                {
                    TextBlock1.Text += fldr.DisplayName;
                }


    StorageFolder folder = ApplicationData.Current.LocalFolder;
                foreach (var lst in await folder.GetFoldersAsync())
                {
                    TextBlock1.Text += lst;
                }


    But VS keep saying "

    Error 1 The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'."

    Whats the wrong with this code ? How can I get list of folders asynchronously ?

    Thursday, January 23, 2014 3:16 PM

Answers

  • There's nothing wrong with the code itself (at least your second version), but when you make use of the 'await' keyword within a method then you have to flag it with 'async':

    E.g. instead of

    protected override void OnNavigatedTo(NavigationEventArgs e)

    you should write

    protected async override void OnNavigatedTo(NavigationEventArgs e)

    Please bear in mind that 'async' cannot be set to constructors and property getters or setters.

    • Marked as answer by string Astro Friday, January 24, 2014 7:44 AM
    Thursday, January 23, 2014 3:39 PM