2016-11-24 82 views
-2

我试图同时打开一个新的关闭窗口,但this.close()不被系统识别this.close()不能被识别为法

private void Insert_Data_Click(object sender, RoutedEventArgs e) 
{ 
      MainWindow DataWindow = new MainWindow(); 
      DataWindow.Show(); 
      this.Close(); 
     } 

这是类声明

public partial class Home_autismo : MetroWindow 

和包含的库

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Shapes; 
using MahApps.Metro.Controls; 
using MahApps.Metro.Controls.Dialogs; 

任何想法,为什么this.Close()不承认?

+0

你试过'Application.Exit()'? – RandomStranger

+0

你能详细说明一下吗?它在运行时不起作用,甚至不能编译?我的应用程序中有完全相同的代码,这很简单。 'Application.Exit()'在尝试切换窗口时退出不方便的应用程序。 – SilentStorm

+0

还要确保你的xaml文件不是一个用户控件或页面,而是以'' SilentStorm

回答

0

尝试添加一个关闭事件,当它的收盘它会打开你的数据窗口 添加在您的XAML窗口的关闭事件

<Window x:Class="ParamCreator.PasswordWindow"  
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml 
Closed="Home_autismo_Closing"> 

然后在你的主类,做this.Close();应该识别它然后

public partial class Home_autismo : MetroWindow 
    { 

private void Home_autismo_Closing(object sender, EventArgs e) 
    { 
     MainWindow DataWindow = new MainWindow(); 
     DataWindow.Show(); 
    } 
    // Logic 
    If(...) 
    { 
    this.Close(); 
    } 
    } 
+0

之类的东西开始解释投票? – JohnChris

+0

Close()应该包含在MetroWindow类中。它不依赖于xaml代码中的临时事件。我可能错过了一些东西。 –

+0

这很好......但是你不在你的主类中做close(),你在一个xaml事件处理程序中做...看看我编辑的答案 – JohnChris