繁体   English   中英

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

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

我创建(并显示)了一个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();

当我尝试使用以下命令遍历创建的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;
         }
     });
}

windowsList似乎只有两个Windows(MainWindowView和另一个),但没有NotificationPopUpView ,这是什么原因? 我不知道我在想什么? 请向我解释问题出在哪里,如何解决?

这是什么原因? 我不知道我在想什么? 请向我解释问题出在哪里,如何解决?

Application.Current.Windows仅包含主UI线程上存在的窗口。 因为您是在新线程上创建的notificationPopUp ,所以它在那里自然不见了。 在主UI线程以外的任何其他线程上创建窗口不是一个好习惯。 如果涉及大量数据处理,则应将其分离到后台线程而不是窗口创建中,以使UI保持响应。

关于StackOverflow的类似问题: 从所有线程获取所有窗口

暂无
暂无

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

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