Valerii
Valerii

Reputation: 2317

How to properly ignore windows created by Visual Studio debugging tool for XAML

In my application I need to get a list of all windows.

var windows = Application.Current.Windows;

If I run my application in debug mode I see not only my forms in the list, but also instances of Microsoft.XamlDiagnostics.WpfTap.WpfVisualTreeService.Adorners.AdornerLayerWindow created by Visual Studio debugging tool for XAML.

What is the right way to filter list of windows to ignore windows created by debugging tool? I don't want to reference additional assembly and check if

window is AdornerLayerWindow

and I don't want to filter like

window.GetType().Name != "AdornerLayerWindow"

Any other ideas?

Upvotes: 4

Views: 1067

Answers (2)

Sixteen '
Sixteen '

Reputation: 1

        List<Window> lstWindows = new List<Window>();
        foreach (Window win in System.Windows.Application.Current.Windows)
        {
            if (win == sender)
                continue;

            #if DEBUG
            //The collection System.Windows.Application.Current.Windows contains the Debugging Window
            //that contains the Inspect Element/Hot Reload controls
            if (win.GetType().FullName.Equals("Microsoft.VisualStudio.DesignTools.WpfTap.WpfVisualTreeService.Adorners.AdornerWindow"))
                continue;
            #endif

            lstWindows.Add(win);
        }

Upvotes: 0

Junyuan Luo
Junyuan Luo

Reputation: 21

I just do the trick window.ActualWidth != 0. It works good for me.

Application.Current.Windows
    .Cast<Window>()
    .Where(w => w.ActualWidth != 0)
    .ToList()
    .ForEach(w => w.Close());

Upvotes: 0

Related Questions