Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8134589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:05:23+00:00 2026-06-06T10:05:23+00:00

Below is a sample implementation that uses metro API and data binding (using MVVM)

  • 0

Below is a sample implementation that uses metro API and data binding (using MVVM) to populate list of folders in a drop down list.

The View model‘s constructor uses SetFolders method (private async), which calls an awaitable method fileService.GetFoldersAsync() to get list of folders. The folders list is then gets assigned to the property called “FoldersList”. XAML uses this property to populate a drop down list using the data binding.

I wonder is there a better way to set the FoldersList property without having to set it in the constructor as below. I would prefer to call the GetFilesAsync method and set the FilesList property value, when the actual data binding occurs (not during the class init). Since the properties do not support async/await modifiers (as far as I know) I’m struggling to implement a proper solution. Any ideas greatly appreciated.

The code is below.

ViewModel

public class FileViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private readonly IFileService fileService;

    public FileDataViewModel(IFileService fileService)
    {
        this.fileService = fileService;
        SetFolders();
    }

    private async void SetFolders ()
    {
        FoldersList = await fileService.GetFoldersAsync();
    }

    private IEnumerable< IStorageFolder > foldersList;
    public IEnumerable<StorageFolder> FoldersList
    {
        get { return foldersList; }
        private set
        {
            foldersList = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("FoldersList"));
            }
        }
    }
}

IFileService and implementation

public interface IFileService    {
    Task<IEnumerable<IStorageFolder>> GetFilesAsync();
  }

public class FileService : IFileService
{
    public async Task<IEnumerable<IStorageFolder>> GetFoldersAsync()
    {
        var folder = KnownFolders.DocumentsLibrary;
        return await folder.GetFoldersAsync();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T10:05:24+00:00Added an answer on June 6, 2026 at 10:05 am

    I would implement it as a lazy property and use ObservableCollection<T> rather than IEnumerable<T>. We are doing it in several projects and it works well. This way you can guarantee that you are loading data only when needed. Furthermore, if you need to prefetch it, you can always call the load method in the constructor or elsewhere.

    As a side note, I personnaly wouldn’t expose IStorageFolder directly from my ViewModels.

    private async Task LoadData()
    {
       if(!IsLoading)
      {
        IsLoading = true;
         Folders = new ObservableCollection<Folder>(await fileService.GetFolderAsync());
    
      }
      IsLoading = false;
    }
    
    private ObservableCollection<Folder> _folders;
    
    public  ObservableCollection<Folder> Folders
    {
      get
      {
        if(_folders == null)
        {
          LoadData();//Don't await...
        }
        return _folders;
    
      }
      private set
      {
        SetProperty(ref _folders,value);
      }
    
    }
    private bool _isLoading;
    public bool IsLoading
    {
      get
      {
        return _isLoading;
      }
      private set
      {
        SetProperty(ref _isLoading,value);
      }
    }
    

    Note that you can use the IsLoading property to display a progress ring for instance. after that the observable collection is loaded, you will be able to refresh it without recreating it. (_folders.Add, _folders.Remove, _folders.Clear…)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have below sample data. AID Date Title ----- ---------- ------ 1 2011-12-12 test1
I am trying to binary serialize the data of vector. In this sample below
I have a web service that returns a raw string of JSON data (sample
I am working on a Google Custom Search implementation that uses the option to
I've put together a small code-sample below (Currently in C# 3.5 but would also
I have a IObservable [named rows in the sample below] from Reactive extensions framework
Below is some sample code. The window does not resize (python3.2, on a Mac,
below is a sample class: AAAA.java 1 package tp.domain; 2 3 import org.springframework.beans.factory.annotation.Value; 4
In the sample code below I am dividing by zero which when I step
When I run the sample code below the width of JTextArea is fixed (100px)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.