开发者

Wpf MVVM Load UI control asynchronously

I have a mvvm wpf application that loads up a window consisting of many controls 开发者_运维技巧in one go. I want this window to load up the controls separately and asynchronously.

Any suggestions?


For that matter, I use a Singleton pattern. If you're familiar with PRISM and its Bootstrapper, it is kinda similar.

The main idea here is to override the method OnStartup in your App.xaml. Default behavior shows in your xaml StartupUri="MainWindow.xaml", you'll have to remove that property.

In the OnStartup override method, I use a class which creates all my objects (Views, ViewModels, link DataContexts... ) and fires an event when initialization is complete.

At this time I dismiss the splashscreen and show a fully loaded app (the InitializeComponent is called when you create your MainWindow, so it'll be already called at this time).

For more extended use, you can add events in your windows and EventHandlers in your bootstrapper class. I use it for example when I want to fully refresh my app (reboot it), and also for database requests (which are, in my case, performed only on application startup to load the referential).

Hope it helped :)


Unfortunately, if your controls are defined in XAML, they're going to be initialized with the InitializeComponent call and go through the loading process. All of the events in loading a XAML window happen whether or not you subscribe to them.

You could add controls dynamically to the form in code behind, but if you have a separate thread do the work, you would need to use the Application Dispatcher CheckAccess and Invoke methods to ensure the controls are loaded on the thread which owns them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜