title: Destroy the instance of ViewModel and Page as soon as frame Navigates to another Page
body: <p>Suppose I have 3 pages namely Page1.xaml, Page2.xaml, Page3.xaml.</p>
  
  <p>I have a window and a Frame inside it. Frame's declaration looks like:</p>
  
  <pre><code>&lt;Frame Source="{Binding SourcePage}" NavigationUIVisibility="Hidden" /&gt;
  </code></pre>
  
  <p>I am doing some work like increasing global counter variable inside constructor and decreasing global counter inside destructor of each page.</p>
  
  <p>Lets name this global counter as x. So the code should look like below:</p>
  
  <pre><code>public int x = 0;
  </code></pre>
  
  <p><strong><em>Page1.xaml</strong>
  (ViewModel associated to Page1 is ViewModel1)</em></p>
  
  <pre><code>public ViewModel1()
  {
      x++;
  }
  ~ViewModel1()
  {
      x--;
  }
  </code></pre>
  
  <p>Similarly:</p>
  
  <p><strong><em>Page2.xaml</strong>
  (ViewModel associated to Page2 is ViewModel2)</em></p>
  
  <pre><code>public ViewModel2()
  {
      x++;
  }
  ~ViewModel2()
  {
      x--;
  }
  </code></pre>
  
  <p><strong><em>Page3.xaml</strong>
  (ViewModel associated to Page3 is ViewModel3)</em></p>
  
  <pre><code>public ViewModel3()
  {
      x++;
  }
  ~ViewModel3()
  {
      x--;
  }
  </code></pre>
  
  <p>Now, I change the source of the frame at runtime.</p>
  
  <p>Initially, source of frame is Page1.xaml. So, constructor of the ViewModel1 is called. So value of x becomes 1.</p>
  
  <p>Then I change the source of frame with the help of buttons at runtime to Page2.xaml. So, constructor of the ViewModel2 is called. So, value of x becomes 2. Now, immediately I expect to call the destructor of ViewModel1. So that x should become 1 again. But Destructor of ViewModel1 is never called.</p>
  
  <p>Then again I change the source of frame with the help of buttons at runtime to Page3.xaml. So, constructor of the ViewModel3 is called. So, value of x becomes 3. Now, immediately I expect to call the destructor of ViewModel2. So that x should become 2 again. But Destructor of ViewModel2 is never called.</p>
  
  <p>Then again I change the source of frame with the help of buttons at runtime to Page1.xaml. So, constructor of the ViewModel1 is called. So, value of x becomes 4. Now, immediately I expect to call the destructor of ViewModel3. So that x should become 3 again. But Destructor of ViewModel3 is never called.</p>
  
  <p>When I close the program at that time destructors of these ViewModels are called. I don't want this behavior. I always want to destroy the instance of ViewModel and Page as soon as frame navigates to another page.</p>
  
  <p>Any help will be greatly appreciated.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>Let me clarify the issue here.</p>
  
  <p>The main point here is I want to get current value of a Property in one ViewModel from another ViewModel.</p>
  
  <p>I have a SessionViewModel as follows:</p>
  
  <pre><code>public sealed class SessionViewModel : ViewModelBase, IModule
  {
      private static readonly SessionViewModel instance = new SessionViewModel();
      public static SessionViewModel Instance
      {
          get
          {
              return instance;
          }
      }
  
      private List&lt;IModule&gt; modulesOpen;
      public List&lt;IModule&gt; ModulesOpen
      {
          get
          {
              return modulesOpen;
          }
          set
          {
              modulesOpen = value;
              NotifyPropertyChanged("ModulesOpen");
          }
      }
  
      public static IModule GetModuleInstance(string moduleName, string finalName)
      {
          IModule moduleToOpen = null;
          if (Instance.ModulesOpen != null)
          {
              moduleToOpen = Instance.ModulesOpen.SingleOrDefault(mod =&gt; mod.ModuleName == moduleName);
          }
          else
          {
              Instance.ModulesOpen = new List&lt;IModule&gt;();
          }
          if (moduleToOpen != null) return moduleToOpen;
          Type module = Type.GetType(finalName);
          moduleToOpen = (IModule)Activator.CreateInstance(module);
          Instance.ModulesOpen.Add(moduleToOpen);
          return moduleToOpen;
      }
  
      public string ModuleFriendlyName
      {
          get { return "SessionViewModel"; }
      }
  
      public string ModuleName
      {
          get { return "Session"; }
      }
  }
  </code></pre>
  
  <p>Interface I used in above code is IModule whose declaration is as follows:</p>
  
  <pre><code>public interface IModule
  {
      string ModuleFriendlyName { get; }
      string ModuleName { get; }
  }
  </code></pre>
  
  <p>When my Program runs I want to collect the current instances of all the Pages in a List and then I can access the current Instance of any ViewModel from another ViewModel. From that I can get the current value of a Property in another ViewModel. But the instances are not automatically added to the List, so I need to add them to the List in the constructor of the ViewModel and remove them from the List in Destructor of the ViewModel.</p>
  
  <p>Lets see the code for MainWindowViewModel:</p>
  
  <pre><code>public class MainWindowViewModel : IModule, ViewModelBase
  {
      public MainWindowViewModel() //Constructor
      {
          SessionViewModel.Instance.ModulesOpen = new List&lt;IModule&gt;();
          SessionViewModel.Instance.ModulesOpen.Add((IModule)this);
      }
  
      ~MainWindowViewModel()
      {
          SessionViewModel.Instance.ModulesOpen.Remove((IModule)this);
      }
      ..
      ..
      ..
  }
  </code></pre>
  
  <p>In ViewModel1.cs</p>
  
  <pre><code>public class ViewModel1 : IModule, ViewModelBase
  {
      public ViewModel1()
      {
          SessionViewModel.Instance.ModulesOpen.Add((IModule)this);
      }
  
      ~ViewModel1()
      {
          SessionViewModel.Instance.ModulesOpen.Remove((IModule)this);
      }
      ......
      ......
      ......
  }
  </code></pre>
  
  <p>In ViewModel2.cs</p>
  
  <pre><code>public class ViewModel2 : IModule, ViewModelBase
  {
      public ViewModel2()
      {
          SessionViewModel.Instance.ModulesOpen.Add((IModule)this);
      }
  
      ~ViewModel2()
      {
          SessionViewModel.Instance.ModulesOpen.Remove((IModule)this);
      }
      ......
      ......
      ......
  }
  </code></pre>
  
  <p>In ViewModel3.cs</p>
  
  <pre><code>public class ViewModel3 : IModule, ViewModelBase
  {
      public ViewModel3()
      {
          SessionViewModel.Instance.ModulesOpen.Add((IModule)this);
      }
  
      ~ViewModel3()
      {
          SessionViewModel.Instance.ModulesOpen.Remove((IModule)this);
      }
      ......
      ......
      ......
  }
  </code></pre>