![](/img/trans.png)
[英]c# Application.Current.Windows does not include all windows
[英]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.