Note that there are some explanatory texts on larger screens.

plurals
  1. PONested Data Context with Unity
    primarykey
    data
    text
    <p>I took a course on VB.Net + WPF at university last year. For the final project, I decided to give MVVM a go (we hadn't discussed it at all in the course, I had just researched it and thought it would be a useful exercise). It was a good experience however I'm rather sure I might have made some poor choices when it came to design.</p> <p>I've since graduated and my job has nothing to do with WPF or Windows development however I'm developing a small application in my own time and thought it would be fun to use C# and WPF (C# is a language I very much like to work with and I enjoyed working with WPF so it's a pretty logical choice).</p> <p>Anyway, I'm using this as an opportunity to learn more about MVVM and try and implement it in a better way than I did previously. I've done a bit more reading and am finding it a lot easier to graph than I had when trying to implement it alongside learning WPF.</p> <p>I've used In The Box MVVM Training as a guide and will be using Unity for dependency injection at this.</p> <p>Now, in the sample app developed in the guide, there is a single view model (MainWindowViewModel). The MainWindow is pretty much a container with 3 or 4 UserControls which all share the DataContext of the MainWindow.</p> <p>In my app, I'd like to have a tab-based interface. As such, the MainWindow will be primary concerned with displaying a list of buttons to switch the current view (i.e. move from the 'add' view to the 'list view'). Each view will be a self-contained UserControl which will implement it's own DataContext.</p> <p>The same code in the app is as follows:</p> <pre><code>MainWindow window = container.Resolve&lt;MainWindow&gt;(); window.DataContext = container.Resolve&lt;MainWindowViewModel&gt;(); window.Show(); </code></pre> <p>That's fine for setting data context of the MainWindow, however how will I handle assigning each user context it's own ViewModel as a DataContext?</p> <p>EDIT: To be more specific, when I say tab-based interface, I don't mean it in the sense of tabs in a text editor or web browser. Rather, each 'tab' is a different screen of the application - there is only a single active screen at a time.</p> <p>Also, while Slauma's post was somewhat helpful, it didn't really explain how I'd go about injecting dependencies to those tabs. If the NewStatementView, for example, was required to output it's data, how would I inject an instance of a class that implements the 'IStatementWriter' interface?</p> <p>EDIT: To simplify my question, I'm basically trying to figure out how to inject a dependency to a class without passing every dependency through the constructor. As a contrived example: Class A has Class B. Class B takes as a constructor paramater needs an implementation of Interface I1. Class B uses Class C. Class C takes as a constructor paramater needs an implementation of Interface I2.</p> <p>How would I handle this scenario using DI (and Unity)? What I don't want to do is: public class A(I1 i1, I2 i2) { .... }</p> <p>I could register everything using Unity (i.e. create I2, then C, then I1 and B, and then finally insert these into A) but then I would have to instantiate everything when I want to use A even if I might not even need an instance of B (and what if I had a whole bunch of other classes in the same situation as B?).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload