WPF应用程序获取有关已加载事件的数据

问题描述:

大家好!

我是wpf的新手,需要一些建议来解决以下问题.

在我的wpf应用程序中,我有多个对话框(窗口).每个对话框都有window_loaded事件,在这里我从数据库中获取一些数据和/或进行一些计算.

基本的想法是这个

Hi all!

Im quite new in wpf and need some advices to solve following problem.

In my wpf application I have multiple dialogs (windows). Every dialog has window_loaded event where I fetch some data from database and/or make some calculations.

Basic idea is this

private void Window_Loaded(object sender, RoutedEventArgs e)
{
GetDataFromDatabase();
MakeCalculations();                                   
}



下面的代码运行良好,但是在加载窗口时会出现一些延迟,并且窗口在缓慢的计算机上一会儿无响应.尤其是带有动画的对话框.

希望你有主意:)

干杯!



Following code works well but when window is loaded there will be some laggy moments and window is unresponsive on slow computers for while. This is seen especially dialogs with animation on it.

Hope you got idea :)

Cheers!



请考虑到Loaded事件发生在与UI相同的线程上.因此,如果
Hi,

Please take into consideration that the Loaded event takes place on the same thread as the UI. Thus, if the
GetDataFromDatabase()

MakeCalculations()

是长时间运行的操作,则UI在此期间将无响应.

您可以利用异步操作.请查看以下内容:

使用异步处理的WPF应用程序的响应UI [使用分派器构建更多响应式应用程序 [

are a long running operations, the UI will be non responsive during this time.

You could make use of asynchronous operations. Please have a look at the following:

Responsive UIs for WPF Applications Using Asynchronous Processing[^]
Build More Responsive Apps With The Dispatcher[^]

Kind regards,