title: Access XAML Instance of Class from Code Behind
body: <p>I used to have an instance of a custom class define in <code>app.xaml.cs</code> so I could access it anywhere in my application. How ever I have now changed that so that an instance of my class is made within my <strong>Application Resources</strong>. </p>
  
  <p><strong>App.xaml</strong></p>
  
  <pre><code>&lt;Application x:Class="Duplicate_Deleter.App"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:local="clr-namespace:Duplicate_Deleter"&gt;
      &lt;Application.Resources&gt;
          &lt;local:runtimeObject x:Key="runtimeVariables" /&gt;
      &lt;/Application.Resources&gt;
  &lt;/Application&gt;
  </code></pre>
  
  <p><strong>App.xaml.cs</strong>
  This is the class.</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Configuration;
  using System.Data;
  using System.Linq;
  using System.Threading.Tasks;
  using System.Windows;
  
  namespace Duplicate_Deleter
      /// &lt;summary&gt;
      /// Global values for use during application runtime
      /// &lt;/summary&gt;
      public class runtimeObject
      {
          //Can the application be closed?
          private bool _inProgress = false;
          public bool inProgress
          {
              get { return _inProgress; }
              set { _inProgress = value; }
          }
  
          //Selected folder to search in
          private string _fromFolder = "testing string";
          public string fromFolder
          {
              get { return _fromFolder; }
              set { _fromFolder = value; }
          }
      }
  }
  </code></pre>
  
  <p>My problem is now, I need to be able to access this instance of the class within my code behind in my commands namespace. Below you can see one of the commands, the <code>App.runtime</code> used to work when the instance was within App.xaml.cs. </p>
  
  <p><strong>Classes > Commands.cs</strong></p>
  
  <pre><code>public static void CloseWindow_CanExecute(object sender,
                             CanExecuteRoutedEventArgs e)
          {
              if (App.runtime.inProgress == true)
              {
                  e.CanExecute = false;
              }
              else
              {
                  e.CanExecute = true;
              }
          }
  </code></pre>
  
  <p>How do I now reference my class instance from within my commands?</p>