Martyn Ball
Martyn Ball

Reputation: 4885

Make window the only instance

Hey I want to make sure that I can only open ONE instance of this window, it doesn't seem to be working and not sure why.

I'm checking there is already a window open with the same name, and making sure im not detecting this current window attempting to open.

public new void Show()
{
    //Ensure new notifications are placed above older ones
    foreach (Window window in System.Windows.Application.Current.Windows)
    {
        string windowName = window.GetType().Name;
        if (!windowName.Equals("NotificationAll") && window != this)
        {
            this.Topmost = true;
            base.Show();

            this.Owner = System.Windows.Application.Current.MainWindow;

            //Position the Notification
            var workingArea = SystemParameters.WorkArea;
            this.Left = (workingArea.Width - this.ActualWidth) / 2;
            this.Top = workingArea.Bottom - this.ActualHeight;
        }
    }            
}

However more than one window is opening still!

Upvotes: 1

Views: 71

Answers (2)

TheVillageIdiot
TheVillageIdiot

Reputation: 40507

You are not doing anything to previous windows opened. Try this modification:

public new void Show()
{
    //Ensure new notifications are placed above older ones
    foreach (Window window in System.Windows.Application.Current.Windows)
    {
        string windowName = window.GetType().Name;
        //ALSO CHECK BY PLACING BREAKPOINT AT THIS if TO SEE WHAT WINDOW
        //NAME ARE YOU GETTING OR IF YOU ARE ENTRING THIS BLOCK
        if (windowName.Equals("NotificationAll") && window != this)
        {
           //IF YOU WANT TO CLOSE PREVIOUS WINDOWS
           window.Close();
        }
     }

      //NOW MANIPLUATE CURRENT WINDOW'S PROPERTIES AND SHOW IT
      this.Topmost = true;
      base.Show();
       ....
       ....
       this.Top = workingArea.Bottom - this.ActualHeight;
}

If you want to close current window and show previous one:

public new void Show()
{
    var hasOtherWindow=false;
    //Ensure new notifications are placed above older ones
    foreach (Window window in System.Windows.Application.Current.Windows)
    {
        string windowName = window.GetType().Name;
        if (!windowName.Equals("NotificationAll") && window != this)
        {
            hasOtherWindow=true;
            window.Topmost = true;
            //Position the Notification
            var workingArea = SystemParameters.WorkArea;
            window.Left = (workingArea.Width - window.ActualWidth) / 2;
            window.Top = workingArea.Bottom - window.ActualHeight;
            break;//GET OUT OF LOOP YOU WILL HAVE ONLY ONE WINDOW
        }
    }
    if(hasOtherWindow)
    Close();//CLOSE THIS WINDOW
}

Upvotes: 1

Flat Eric
Flat Eric

Reputation: 8111

To check, if there is no other Window with the same name you could use this Linq statement:

if (!Application.Current.Windows.Cast<Window>().Where(x => 
    x != this).Any(x => x.GetType().Name == "NotificationAll"))
{

}

Upvotes: 1

Related Questions