title: WinRT ViewModel DataBind to async method
body: <p>I am deserializing a list of objects from an XML file, and would like to bind to the actual content of those objects in my View, passing over a ViewModel. The problem is that file operations are <code>async</code> and this bubbles all the way up to the ViewModel, where Property getters cannot be marked as such...</p>
  
  <h2>Problem</h2>
  
  <ul>
  <li><p>I deserialize all XML files in a folder to <code>Profile</code> objects and store them in a <code>List&lt;Profile&gt;</code>. This method (has to be) marked <code>async</code>.</p>
  
  <pre><code>    public static async Task&lt;List&lt;Profile&gt;&gt; GetAllProfiles()
      {
          DataContractSerializer ser = new DataContractSerializer(typeof(Profile));
          StorageFolder folder = await ApplicationData.Current.RoamingFolder.CreateFolderAsync("Profiles", CreationCollisionOption.OpenIfExists);
  
          List&lt;Profile&gt; profiles = new List&lt;Profile&gt;();
          foreach (var f in await folder.GetFilesAsync())
          {
              var fs = await f.OpenStreamForReadAsync();
              profiles.Add((Profile)ser.ReadObject(fs));
             fs.Dispose();
          }
  
          return profiles;
      }
  </code></pre></li>
  </ul>
  
  <h2>Ideal solution 1</h2>
  
  <ul>
  <li><p>The binding property in my ViewModel would then <em>ideally</em> call that static method like this</p>
  
  <pre><code>public async Task&lt;ObservableCollection&lt;string&gt;&gt; Lists
  {
      get 
      {
          return new ObservableCollection&lt;string&gt;(GetAllProfiles().Select(p =&gt; p.Name));
      }
  }
  </code></pre></li>
  <li><p>BUT Properties cannot be marked <code>async</code></p></li>
  </ul>
  
  <h2>Ideal solution 2</h2>
  
  <pre><code>    public ObservableCollection&lt;string&gt; Lists
      {
          get 
          {
              return new ObservableCollection&lt;string&gt;((GetAllProfiles().Result).Select(p =&gt; p.Name));
          }
      }
  </code></pre>
  
  <ul>
  <li>BUT this never executes (it blocks in the <code>await folder.GetFilesAsync()</code> call for some reason)</li>
  </ul>
  
  <h2>Current solution</h2>
  
  <p>Calls an <code>async</code> Initialize() method that loads the result of the <code>GetProfiles()</code> function in a variable, and then makes a <code>NotifyPropertyChanged("Lists")</code> call:</p>
  
  <pre><code>    public ViewModel()
      {
          Initialize();
      }
  
      public async void Initialize()
      {
          _profiles = await Profile.GetAllProfiles();
          NotifyPropertyChanged("Lists");
      }
  
      private List&lt;Profile&gt; _profiles;
      public ObservableCollection&lt;string&gt; Lists
      {
          get
          {
              if (_profiles != null)
                  return new ObservableCollection&lt;string&gt;(_profiles.Select(p =&gt; p.Name));
              else
                  return null;
          }
      }
  </code></pre>
  
  <h2>Question</h2>
  
  <p>Is there a better way?
  Is there a pattern/method that I haven't yet discovered?</p>
  
  <h1><em>Edit</em></h1>
  
  <p>The root of the problem appears when doing non-UI code, and you cannot rely on the NotifyPropertyChanged to do some thread-synchronization stuff. -- The method Initialize has to be awaited and ctors cannot be async, so essentialy this is pattern is useless.</p>
  
  <pre><code>    public MyClass()
      {
          Initialize();
      }
  
      public async void Initialize()
      {
          _profiles = await Profile.GetAllProfiles();
      }
  
      private ObservableCollection&lt;Profile&gt; _profiles;
      public ObservableCollection&lt;string&gt; Lists
      {
          get
          {
              return _profiles; // this will always be null
          }
      }
  </code></pre>