- 1 year ago
Check if window from loaded assembly is open
To check if a window from a loaded assembly is open in a WPF application, you can iterate through the application's Application.Current.Windows
collection and check if any window matches the one you want to check. Here's an example of how you can do it:
csharpusing System;
using System.Linq;
using System.Reflection;
using System.Windows;
public class WindowChecker
{
// Method to check if a window from a specific assembly is open
public static bool IsWindowOpenInAssembly(string assemblyName)
{
Assembly[] loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
Assembly targetAssembly = loadedAssemblies.FirstOrDefault(assembly => assembly.GetName().Name == assemblyName);
if (targetAssembly == null)
throw new ArgumentException($"Assembly '{assemblyName}' not found.");
return Application.Current.Windows.Cast<Window>().Any(window => window.GetType().Assembly == targetAssembly);
}
}
Usage:
csharpbool isWindowOpen = WindowChecker.IsWindowOpenInAssembly("YourAssemblyName");
Replace "YourAssemblyName"
with the name of the assembly containing the window you want to check. This method will return true
if any window from the specified assembly is open, and false
otherwise.
Keep in mind that this approach assumes you are checking WPF windows within the same application domain. If the window you want to check belongs to a different application domain or a separate process, you would need to use more advanced techniques like inter-process communication (IPC) to communicate with the other process and check the window's status.