简体   繁体   English

Application.Current.Windows不包含所有创建的Window

[英]Application.Current.Windows doesn't contain all created Window

I created (and shown) a Window as follows : 我创建(并显示)了一个Window,如下所示:

var thread = new Thread(() =>
   {
        notificationPopUp = new NotificationPopUpView(unreadNotifications.First().session_s, unreadNotifications.First().secondry_msg);
        notificationPopUp.Show();
        Dispatcher.Run();

   });

thread.SetApartmentState(ApartmentState.STA);
thread.IsBackground = true;
thread.Start();

when I try to itterate through the created Windows using : 当我尝试使用以下命令遍历创建的Windows时:

Application.Current.Dispatcher.Invoke(() =>
{
     windowsList = Application.Current.Windows.Cast<Window>();
});

foreach (var window in windowsList)
{
     Application.Current.Dispatcher.Invoke(() =>
     {
         if (window.DataContext == viewModel)
         {
              returnValue = window;
         }
     });
}

The windowsList seem to have only two Windows (the MainWindowView, and another one) but not the NotificationPopUpView , what would be the cause of this ? windowsList似乎只有两个Windows(MainWindowView和另一个),但没有NotificationPopUpView ,这是什么原因? I don't know what I'm missing ? 我不知道我在想什么? please explain to me what is the problem and how can I correct this ? 请向我解释问题出在哪里,如何解决?

what would be the cause of this ? 这是什么原因? I don't know what I'm missing ? 我不知道我在想什么? please explain to me what is the problem and how can I correct this ? 请向我解释问题出在哪里,如何解决?

Application.Current.Windows only contains windows that exist on the main UI thread. Application.Current.Windows仅包含主UI线程上存在的窗口。 Because you created notificationPopUp on a new thread, it's naturally missing there. 因为您是在新线程上创建的notificationPopUp ,所以它在那里自然不见了。 It's not good practice to create windows on any other thread but the main UI thread. 在主UI线程以外的任何其他线程上创建窗口不是一个好习惯。 If there is a lot of data processing involved, you should separate that to a background thread instead of the window creation to keep the UI responsive. 如果涉及大量数据处理,则应将其分离到后台线程而不是窗口创建中,以使UI保持响应。

Similiar question on StackOverflow: Get all windows from all threads . 关于StackOverflow的类似问题: 从所有线程获取所有窗口

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM