如何关闭XAML Frame Source内容

时间:2018-02-27 19:30:20

标签: c# wpf xaml

我有一个带选项卡的WPF窗口视图,并且在选项卡中显示了不同的WPF页面。我在每个标签中的代码类似于:。 Pressure.xaml.cs定义哪个viewmodel用作datacontext。当我关闭包含选项卡的窗口时,每个“保持”选项卡上的页面的视图模型将继续运行。有没有办法阻止单独的视图模型运行?

主页XAML代码:

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

Pressure.xaml.cs代码:

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();
    }
}

1 个答案:

答案 0 :(得分:0)

请尝试以下代码段:

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;
   } 

}