如何打开以前关闭的窗口的相同实例?

时间:2014-04-01 15:31:06

标签: c# wpf

我实现了Kinect for Windows工具包http://msdn.microsoft.com/en-us/library/dn188701.aspx中的控件基础样本,用户手控制光标,但是当我点击子窗口然后用手形光标重新打开主窗口时显示。

我的问题是如何在不关闭上一个窗口的情况下打开新窗口,然后导航回该窗口的同一个实例,而不是新实例?

这就是我在主窗口类中调用新窗口的方法:

private void trainingBtn_Click(object sender, RoutedEventArgs e)
{
     var newForm = new TrainingFrm(); //create your new form.
     newForm.Show(); //show the new form.
     this.Close(); //only if you want to close the current form. 
}

这就是我重新打开主窗口的方式,但它会创建一个我不想要的主窗口的新实例。

private void homeBtn_Click(object sender, RoutedEventArgs e)
{
     var newForm = new MainWindow(); //create your new form.
     newForm.Show(); //show the new form.
     this.Close(); //only if you want to close the current form.
}

2 个答案:

答案 0 :(得分:1)

只需隐藏,而不是关闭

如果您需要在show之后显示新信息,只需将新数据绑定到其视图模型。

答案 1 :(得分:1)

你需要的是作文

这里应该看看你的mainWindow类

 public partial class MainWindow : Window
{
    private trainingWindow _trainingWindow;   
    public MainWindow()
    {
        InitializeComponent();
    }

    private void buttonGoTraining_Click(object sender, RoutedEventArgs e)
    {
        if (_trainingWindow== null)
        {
         _trainingWindow=  new trainingWindow(this);   
        }

        this.Visibility = Visibility.Hidden;  
        _trainingWindow.Show();
        _trainingWindow.Visibility  = Visibility.Visible;
        this.Visibility = Visibility.Hidden; 
    }
}

这是你的培训班

public partial class trainingWindow : Window
    {
        private MainWindow _mainWindow; 
        public trainingWindow(MainWindow mainWindow )
        {
            InitializeComponent();
            _mainWindow = mainWindow;  
        }

        private void biuttonBack_Click(object sender, RoutedEventArgs e)
        {
            this.Visibility = Visibility.Hidden; 
            _mainWindow.Visibility = Visibility.Visible;

        }
    }

这是xaml

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Content="Button" Height="121" HorizontalAlignment="Left" Margin="112,38,0,0" Name="button1" VerticalAlignment="Top" Width="195" Click="buttonGoTraining_Click" />
    </Grid>
</Window>



<Window x:Class="WpfApplication2.trainingWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="trainingWindow" Height="300" Width="300">
    <Grid>
        <Button Content="Button" Height="36" HorizontalAlignment="Left" Margin="52,33,0,0" Name="button1" VerticalAlignment="Top" Width="97" Click="biuttonBack_Click" />
    </Grid>
</Window>