tags:

views:

23

answers:

2

In my application there are only 2 windows — win_a & win_b, on each of these windows there is button that call another window, e.g. click on btn1 of win_a will call win_b, click on btn2 of win_b will show win_a.

Desired behaviour: 1. Only one instance of object is premitted at the same time, e.g. situation, where 2 instances of win_a running at the same time is not permitted.

  1. When you click on button that calls windows that already exist this action will only change a focus to needed window.

  2. If you call a window that previously had been created, but after this has been closed this action will create a new instance of this window. E.g. there are 2 running windows. you close one of them and after try to call this window back, so related button will create it.

How to write it in WPF (XAML + C#). For the moment I wrote a version that can create a lot of instances of the same window (no number of instances control implemented), but I want to see only one instance of the same window, as we can see it in a lot of applications.

Example of my code:

Window win = new Window();
win.Show();

Thanks.

A: 

first you need 2 references on each other window. on button click you need to check one reference. say in win_a

//win_b is a member on Windows_a class
if(_win_b.IsVisible())
{
// set focus on it
}
else
{
//show win_b
}

make the same for windows_b

Arseny
A: 

I would suggest a different approach:

  1. make a singleton class, that holds a list of tuples List>

  2. when creating windows you can check if the window is in the collection or not.

    if the collection holds a window you can set it activ win.Activate(), else you can create it and add a reference to the collection list.add(tuple(win,"windowA"))

3.finally on the windows that you can add to the collection, on closing you need to remove the window from the singletons list, you can do this handling the Close event of the window

i don't have the code i wrote here, but i hope it helps.

SwissCoder