3

Is there a way to get a list of all windows in Avalonia?

The equivalent of this in WPF

Application.Current.Windows

My requirement is to activate or close a certain window based on its DataContext.

If I can't access such a list; is there a way to track the creation and destruction of windows to create an internal list?

Etienne Charland
  • 3,424
  • 5
  • 28
  • 58

2 Answers2

4

You need IClassicDesktopStyleApplicationLifetime::Windows property. Lifetime is available from Application's ApplicationLifetime property.

e. g.

((IClassicDesktopStyleApplicationLifetime)Application.Current.ApplicationLifetime).Windows

Note that it's not available for Mobile, WebAssembly and Linux framebuffer platforms.

kekekeks
  • 3,193
  • 19
  • 16
  • Didn't know Mobile and WebAssembly were supported at all yet? What is Linux framebuffer? It's a generic library for MVVM development, so it needs to be compatible for any use-case. That interface name specifically says ClassicDesktopApplication -- works fine on Windows, Linux and MacOS desktop? – Etienne Charland Dec 07 '21 at 15:46
  • WASM is currently in preview (runs existing apps mostly out of the box, but there are some issues with fonts and full AOT right now). Mobile backends need IME work to be finished, which is expected 2022 Q1, then production-ready iOS support should become available rather quicly. Linux framebuffer is fbdev/kms-drm + libinput/libevdev backend intended for use on embedded devices. Classic desktop lifetime is designed for dekstop Windows (not HoloLens or XBox), desktop Linux (X11 or XWayland) and macOS (including sandboxed mode for AppStore). – kekekeks Dec 08 '21 at 16:48
1

you can create WindowsManagerClass with one static propery with type of List<Window> like this

 public class WindowsManager
    {
        public static List<Window> AllWindows = new List<Window>();
    }

and add to AllWindows like this code in your Form constructor

 public MainWindow()
        {
            InitializeComponent();
            WindowsManager.AllWindows.Add(this);
        }

and where you need you can access reference like this

var allwindows = WindowsManager.AllWindows;

            var selectedWindows = allwindows.FirstOrDefault(x => x.Name == "Test");
            if (selectedWindows != null)
            {
                if (selectedWindows.IsActive)
                {
                    selectedWindows.Close();
                }
            }

Full form code (in this example when you click button form will be close)

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            WindowsManager.AllWindows.Add(this);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var allwindows = WindowsManager.AllWindows;

            var selectedWindows = allwindows.FirstOrDefault(x => x.Name == "");
            if (selectedWindows != null)
            {
                if (selectedWindows.IsActive)
                {
                    selectedWindows.Close();
                }
            }
        }
    }
Akbar Asghari
  • 613
  • 8
  • 19
  • This is a class library managing windows (porting MvvmDialogs to Avalonia). I can't ask the users to add code into every single one of their views. But since I manage opening all windows, I could hack around and track windows opening, listening to close events, to have an internal list. I want to see whether there's a simpler option first. – Etienne Charland Dec 07 '21 at 00:31
  • Is this still the best way to access Windows in Avalonia? – Donny V. Jan 04 '22 at 02:09
  • 1
    @DonnyV. maybe but it's my idea – Akbar Asghari Jan 04 '22 at 05:46