1

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!

7
  • 1
    Are you trying to make sure only 1 instance of your application itself is running? If so, look here. Commented Nov 16, 2015 at 20:52
  • What do you want to do with previously opened window(s)? Close them or close current one? Commented Nov 16, 2015 at 20:52
  • 1
    In that case, a lot of people like to use the singleton pattern for things like this. Many would also argue against its use and call it an "antipattern", but it's up to you really. Commented Nov 16, 2015 at 20:57
  • 1
    If you look at your code carefully, you'll see that what it's really doing is this: it'll show the new window for each window it finds that is not a NotificationAll window. Remember, that code is inside a foreach loop. Step through it with a debugger and you'll see what I mean. Commented Nov 16, 2015 at 21:13
  • 1
    Personally I'd make the constructors private and then have a static "Create" method that would either create a new instance if there is none, or activate the existing instance. The Show function seems like it is way too late in the process to enforce a single instance. You can use a static field to track if there is an instance or not, or Lazy<T>. Commented Nov 16, 2015 at 21:20

2 Answers 2

1

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")) { } 
Sign up to request clarification or add additional context in comments.

Comments

1

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 } 

1 Comment

if you want to close newly opened window if anyother was there then then second code sample will work

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.