title: UWP - I can't edit a field on the Application class from the UI thread
body: <p>I have a property <code>Files</code> of type <code>ObservableCollection&lt;Element&gt;</code> in my <code>Application</code> class.</p>
  <pre class="lang-cs prettyprint-override"><code>public sealed partial class App : Application
  {
      public string CurrentPath;
  
      public ObservableCollection&lt;Element&gt; Files;
  
      public async void RefreshFilesAsync()
      {
          Files.Clear();
  
          var folder = await StorageFolder.GetFolderFromPathAsync(CurrentPath);
          var foldersRequest = folder.GetFoldersAsync();
          var filesRequest = folder.GetFilesAsync();
  
          var folders = await foldersRequest;
          var files = await filesRequest;
  
          foreach (StorageFolder directory in folders)
          {
              Files.Add(new FolderElement(directory));
          }
  
          foreach (StorageFile file in files)
          {
              Files.Add(new FileElement(file));
          }
      }
  
      // ...
  
  }
  </code></pre>
  <p><code>Element</code> is a wrapper class of mine for <code>StorageFile</code> objects. This class is made so that setting a new value to the Name property calls the API to rename the actual file.</p>
  <pre class="lang-cs prettyprint-override"><code>public class Element
  {
      public string Name
      {
          get =&gt; StorageFile.Name;
          set =&gt;
          {
              StorageFolder.RenameAsync(value, NameCollisionOption.GenerateUniqueName).Completed = (IAsyncAction act, AsyncStatus status) =&gt;
              {
                  (App.Current as App).RefreshFilesAsync();
              };
          }
      }
  
      public StorageFile StorageFile { get; }
  
      //...
  
  }
  </code></pre>
  <p>I have a <code>DataGrid</code> linked to the Application property <code>Files</code>, so you can deduce that whenever I rename the column for the file name, the API is called to perform the real renaming.</p>
  <p>Here's when my problem occur. The API call succeeds and the file is renamed, but inside <code>RefreshFilesAsync()</code>, when <code>Files.Clear();</code> is called, I get an exception <code>The application called an interface that was marshalled for a different thread</code>.</p>
  <p>What am I getting wrong?</p>