Application category - interactive multi-window

We placed out of the response code in the application event derived from the Application class, you can also put some code to complete other tasks.

Prior to know:

How to get Application Object application:

// App is a class that inherits from Application 
App app = Application.Current;

For example: when the interactions between multiple windows, we have another window in order to operate a window, sometimes access will be part of a control or field set to public. This enhances the coupling between the windows. With the enhancement of the interaction between form, we will face great hardship.

So, we can customize the application class save in reference to the window so that you can access a window to another window:

  • Define a generic collection of App.xaml.cs
  • When you create a new window when a new window object is added to the collection
  • Anywhere in the code window can be operated by traversing the set of
private List<Window> documents = new List<Window>();
public List<Window> Docunmets
{
    get { return documents; }
    set { documents = value; }
}
public App()
{
    this.ShutdownMode = ShutdownMode.OnExplicitShutdown;
    MainWindow window = new MainWindow();
    this.documents.Add(window);
    this.StartupUri = new Uri("MainWindow.xaml",UriKind.Relative);
}

In another window:

// Get the application of Application Object 
Application file application = Application.Current; 
List <the Window> Windows = ((the App) Application.Current) .Docunmets;
 for ( int I = 0 ; I <Windows.Count; I ++ ) 
{ 
    the MessageBox. show ( " already has the active window: " + Windows [I] .Title); 
}

Guess you like

Origin www.cnblogs.com/bigbosscyb/p/11326698.html