Wpf MVVM Load UI control asynchronously - c#

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.

Related

Notify parent page from child page

I am building an app using UWP and I have MainPage.cs which contains a Frame control where I load different child pages. I would like to pass a listener to the child page to notify my parent page about event that happen in the child. On android I would pass a callback listener which I will have to trigger in the sub-pages so I can get notified in parentl The issue here is that pages are started Frame.Navigate(typeOf(LoginPage)) for which I cannot set a callback since login page is not a referece/object but a type class.
Any help would be greatly appreciated.
It seems like the MainPage is a singleton instance. In such case you could just expose this instance by creating a static MainPage Instance property in MainPage and set it in its constructor. Then you could access MainPage directly from any other page.
The proper solution however would be to use a MVVM framework and instead of communicating between the views directly put the communication within the view models. In MVVM you usually have a sort of navigation service which allows passing parameters/return values and also you can have a event aggregator to raise messages to which other components can subscribe. Good examples of MVVM frameworks are MvvmCross, MvvmLight and Prism. It is definitely worth learning to use and integrate them early on, because it will make your life significantly easier in terms of code maintenance in the long run.

WPF UserControl that is a tab in a tabcontrol - how to detect if it is closing

I have a class that is inheriting from a UserControl. I am showing this class in a WPF TabControl as a tab. The tab has a small x and can be closed by clicking on it. I need a way to do some cleanup code before the tab is destroyed.
I don't believe I can use the Unloaded event to do this because the Unloaded event is called when the UserControl is being destroyed and it is also being called when you click on another tab.
Any ideas on how to deal with this situation?
EDIT:
Here is more info.
In my UserControl class I have a 3rd part control that I am using. Basically a graphing control. There are a couple lines of code I would like to run to ensure that there are no memory leaks. If you want to read more about it then this would be the web address that talks more about it:
http://support.scichart.com/index.php?/News/NewsItem/View/21/wpf-xname-memory-leak--how-to-clear-memory-in-scichart
You can have a look at the way I have done this in the dragablz TabControl on GitHub.
Essentially the TabControl listens to a RoutedCommend raised from the close button, then calls an optional callback which enables a MVVM view model (or old-style control type code) to dispose an associated view model, or perform any other tidy up code you want to do (or indeed cancel the close operation).
In the example project file on GitHub, look for ClosingItemHandlerImpl and work back from there.
http://github.com/ButchersBoy/Dragablz/blob/master/DragablzDemo/BoundExampleModel.cs
ClosingItemHandlerImpl is bound in from the XAML, and the tab control will call it prior to removing a tab.

Where to write my startup method in WPF user control

In a WPF Window Application, I can override OnStartup method to add my own logic when the app is launched.
My my app is a add-on of another app, actually, its a UserControl.
Where to add my own logic to the app when the app is launched?
Now, I write the code in code behind. Is there a more sensible place to do that?
Are you just trying to define startup logic for a UserControl?
Then it depends what you're trying to initialize:
Are you trying to initialize all sorts of UI-related data?
The best solution would be either in the constructor, or in the Loaded event in code behind
Are you trying to initialize the underlying data for its ViewModel?
Then you should create it when you call the UserControl from your parent app. It should initialize your ViewModel and its data, and ilnk it to the UserControl you're trying to load
Should be enough to get you started!

Backgroundworker in a Winform Usercontrol in an ActiveX Control crashing the ActiveX

I've written an ActiveX-Control with a bindable property.
The ActiveX consists of a single Dialog that shows a WinForms UserControl (via CWinFormsControl<..>).
When the ActiveX property changes, a function on the Usercontrol is called which then should do some database calls and display the results.
So far everything works.
Now when I want to move the database queries to a BackgroundWorker, but everytime I call my database functions in the backgroundworker, the ActiveX-Control crashes. The UserControl on it's own (in a normal WinForms-Application) is running fine.
Any idea how I can find what's wrong here? Are there general pitfalls with Threads and ActiveX-Controls I'm not aware of?
ActiveX control is thread-affinity, only the thread(usually is main windows thread) which create the ActiveX control can call into the control's property setting/function call. Otherwise, the control will crash. If the background worker thread want to notify or call back to control, the best way is post windows message to control, and let control's creating thread to reponse to message and do what ever you want to do.

Silverlight issues with InitializeComponent and Databinding

I have some issues with Silverlight issues with InitializeComponent and Databinding. I have a tree structure that, I guess, has to be bound with data directly with the XAML code and that binding is activated when the Main page calls "InitializeComponent".
Here is the rub. The silverlight app has to wait in a call back from a server in order to know what data to load. And this happens in the guts of the C# code long after the initialization has occurred.
So I do not know what to do. It seems only the main page can call InitializeComponent() but I have to wait until I have the data to load and then call InitializeComponent and that happens long after the MainPage is run.
Everything in Silverlight is designed to work with asynchronously loaded data, especially bindings.
You can initialise with bindings to properties that start empty or null. Initialise is all about parsing the Xaml into plain old C# objects (POCOs) to generate a visual tree of controls.
The bindings will update when the data changes so long as the properties make use of INotifyPropertyChanged (i.e. in the parent container that holds your lists/hierarchy) or if they are ObservableCollections they provide change notification about their contents themselves.
If you have a more specific question, please post code sample so we have something to reference.

Categories

Resources