0

here on Button click to close mordenwindow..thats doesn't work

private void Button_Click_1(object sender, RoutedEventArgs e)
    {
     // this MainWindow is like this  --> <mui:ModernWindow  x:Class="FirstFloor.ModernUI.App.MainWindow1" ....>

        MainWindow1 mw = new MainWindow1();

        // this is my  login Page..
        Login lg = new Login();
        lg.Show();
        mw.Close();  //here code is not working
    }
Machavity
  • 30,841
  • 27
  • 92
  • 100
Krunalsinh
  • 240
  • 1
  • 13
  • 1
    This looks like you are create a new instance of MainWindow1 here and expecting to close the window which is now shown till now. – Ramashankar Jan 24 '14 at 11:59
  • here i want to logoff from MainWindow1 and redirect to Login.xaml..than what should I do? – Krunalsinh Jan 24 '14 at 12:05
  • you have not shown window by calling `mw.show()` then how are you closing? – Mujahid Daud Khan Jan 24 '14 at 12:06
  • from app.xaml ...its open... – Krunalsinh Jan 24 '14 at 12:09
  • @Krunalsinh: Could you post the block of code which demonstrate that where the button_click is method is present. if this Button_Click_1(...) method is in your mainwindow1 codebehind, then simply you can use this.close(). – Ramashankar Jan 24 '14 at 12:10
  • simple Quetion I ask.... how can I close ModernWindow from other page by code?? – Krunalsinh Jan 24 '14 at 12:19
  • To close ModernWindow from other page you need actual object of ModernWindow in other page which is closing form on button click. As per you code in question you are just creating new object of MainWindow1 and calling close method without show. To close ModernWindow you need actual object via ModeenWindow shown/Opened. – Jignesh Thakker Jan 24 '14 at 12:51

1 Answers1

0

What you did in that Button_Click_1 event is you created a new ModernWindow1 then you closed that newly created ModernWindow1, . Now, you technically have two ModernWindow1 in the start of that event. What you need is to close the currently running ModernWindow1, and not the newly created ModernWindow1. to do that, you need to reference the old ModernWindow1 before going to another window.

This is the Second ModernWindow

public partial class ModernWindow2 : ModernWindow
    {
        public dynamic ReferencedWindow2; //you will put the original Window here

        public ModernWindow2()
        {
            InitializeComponent();
        }

        public ModernWindow2(dynamic referencedWindow) // second constructor with a parameter
        {
            InitializeComponent();
            ReferencedWindow2 = referencedWindow; // the original modernwindow being put in here
        }

        private void Button_OnClick(object sender, RoutedEventArgs e)
        {
            ReferencedWindow2.Close();
        }
    }

THIS IS THE ORIGINAL OR PRIMARY MODERNWINDOW

public partial class ModernWindow1 : ModernWindow 
    {

        public ModernWindow1()
        {
            InitializeComponent();


        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            /*
            this will show the second modernwindow using the second constructor with parameter
            */
            ModernWindow2 newWindow2 = new ModernWindow2(this);  
            newWindow2.Show();
        }
    }