What is the preferred method of handling all the events from the App when using MVVM (MVVM Light in my case)? So, things like Application_Launching
, Appli开发者_开发问答cation_Activated
, Application_Deactivated
, and Application_Closing
.
My thought is to broadcast a message on each event that the view models can subscribe to. My other thought would be to call a method on the view models directly from the App.xaml.cs
events.
Those events you have mentioned above have nothing to do with the View at all, because the View is either going to get created or going to be destroyed with those events. That point makes the events propogate in to ViewModel code need not be a MVVM way mainly because there is no View involved in those event raising times. Just to implement this properly for a 'Seperation of Concern', you can have you MainViewModel (Mostly singleton) has all 4 methods and call it directly from App.xaml. Or else an event delegate ( messenger) would be more clean. I assume you want to do some general operations on those events like IsolatedStorage or serialization/deserialization operations.
I keep all my files in seperate DLLs ( Models in App.Model.dll, Views in App.Views.dll, etc...) so there's very little code in the main boot runtime. I use a central controller and hook app level events, central settings etc to it that are app specific.
精彩评论