-2

I'm trying to close a window while opening a new one, but this.close() is not recognised by the system

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

Here it is the class declaration

public partial class Home_autismo : MetroWindow

and the included libraries

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;

Any idea why this.Close() is not recognised?

Alex Gimondi
  • 84
  • 1
  • 11
  • Have you tried `Application.Exit()`? –  Nov 24 '16 at 13:50
  • 1
    Can you elaborate? It doesn't work on runtime or it doesn't even compile? I have the exact same code in my application and this simply works. `Application.Exit()` quits the application which is not handy when trying to switch windows. – SilentStorm Nov 28 '16 at 14:23
  • Also make sure your xaml file is not a usercontrol or page but starts with something like ` – SilentStorm Nov 28 '16 at 14:29

1 Answers1

0

Try add a closing event that when It's closing it will open your data window Add in XAML on your window the closing event

 <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">

Then in your main class, do this.Close(); should recognise it then

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();
   }
  }
JohnChris
  • 1,360
  • 15
  • 29
  • Close() should be included in MetroWindow Class. It's not depending on ad hoc events in the xaml code. I'm am probably missing something. – Alex Gimondi Nov 24 '16 at 14:21
  • That's fine... But your not doing close() in your main class, your doing it in a xaml event handler... Look at my edited answer – JohnChris Nov 24 '16 at 14:26
  • yea, but there is no close event whether I'm not able to close the window. – Alex Gimondi Nov 24 '16 at 14:31
  • 2
    http://stackoverflow.com/help/privileges/vote-down , this is when you should vote down an answer, not just cause it wasn't correct, people won't try and help you then – JohnChris Nov 24 '16 at 14:51