Lance
Lance

Reputation: 1

How to close XAML Frame Source content

I have a WPF window view with tabs, and in the tabs there are different WPF pages being shown. My code in each tab is similar to: . The Pressure.xaml.cs defines which viewmodel is used as the datacontext. When I close my window that has the tabs, my viewmodels for the pages on each keep tab keep running. Is there a way to stop the separate viewmodels from running?

Main page XAML code:

<TabItem>
    <TabItem.Header>
        <Label Content="Pressure" HorizontalAlignment="Center"/>
    </TabItem.Header>
    <Frame Source="Pressure.xaml"/>
</TabItem>

Pressure.xaml.cs code:

public partial class Pressure : Page
{
    Machine _machine;
    PressureVM _viewModel;

    public Pressure()
    {
        _machine = new Machine();
        _viewModel = new PressureVM(_machine);
        DataContext = _viewModel;
        InitializeComponent();
    }

    private void OpenAddGaugeWinBtn_Click(object sender, RoutedEventArgs e)
    {
        var win = new AddPressureGauge();
        win.DataContext = this.DataContext;
        win.Show();
    }
}

Upvotes: 0

Views: 89

Answers (1)

Atanu Biswas
Atanu Biswas

Reputation: 26

Try the below code snippet:

public partial class Pressure : Page, IDisposable
    {
       // Flag: Has Dispose already been called?
       bool disposed = false;
       // Instantiate a SafeHandle instance.
       SafeHandle handle = new SafeFileHandle(IntPtr.Zero, true);

       //do your usual code

   // Protected implementation of Dispose pattern.
   protected virtual void Dispose(bool disposing)
   {
      if (disposed)
         return; 

      if (disposing) {
         handle.Dispose();
         // Free any other managed objects here.
         //
      }

      // Free any unmanaged objects here.
      //
      disposed = true;
   } 

}

Upvotes: 0

Related Questions