Reputation: 6379
As the title suggest, this question is specifically looking for solutions (or ways to avoid) the problems I have experienced with an application which creates view models directly from XAML (no code references to the objects).
This is a mature, functioning and fully MVVM app - and in various areas visual objects are typically added to portions of the main XAML window, or popup windows, like so:
<viewModels:MyViewModel MyDependencyProperty="{Binding}" />
In these cases, the instance of the ViewModel is created with parameterless construction and we sometimes initialise dependency properties as shown above.
The main XAML window has a DataContext of the primary view model - the ApplicationViewModel. Mostly all our views are resource dictionaries with styles defining various templates for the view model target types. These are pulled into merged dictionaries in our skin XAML files.
Problem 1
At times, other objects around the application need to interact with the ApplicationViewModel. At the point of instantiation, however, no reference is available in XAML or otherwise - leading to overuse of a static ApplicationViewModel instance (not good).
Problem 2
Quite often our XAML-instantiated objects will have event hooks and visual elements which require explicit unhooking / disposal. Our visual tree is extremely large and complicated and because we don't have any references in code to our view models there is a constant overhead of managing the disposal and avoiding memory growth - and throwing away the main window along with any view model or visual element which may cause it to remain in memory - is a constant challenge.
I'm not asking for any code here, more for descriptions of your approaches to / experiences of these problems.
I cannot supply code for a variety of reasons - the main one being that there is far too much needed to demonstrate!
UPDATE
Just for information, another important thing about the application is the constant creation of new ViewModel items which represent rich items which appear in a number of constantly rolling lists which are available in tabs. From each of these lists, there are numerous command operations, popup dialogs and other functions which require downstream ViewModel creation (which is currently on-demand). These lists support various touch gestures, including flick/momentum scrolling, slideout menus, touch expansion and drag drop not to mention having rich displays involving vector images and animations!
Upvotes: 1
Views: 184
Reputation: 80272
Having worked on something which sounds like it is of similar complexity, I can offer some recommendations to make your application fast, responsive and memory efficient:
Upvotes: 1