3

I am about to use Caliburn.Micro for a new XAML Store app (not sure what to call it nowadays - does not appear to be called a "Windows Store app" any longer).

The documentation (see Simple Navigation section) advocates using UserControls for "Views" which can be contained in a "Shell".

As an experienced WPF developer I am hesitant to do so because:

  1. A UserControl in XAML is for making a reusable control, e.g. a date time picker. It is not designed to be a whole view you only have once in your application (use a Page or Window for that).
  2. Splitting a View up into sub-views only makes your life harder when you want to communicate between what would have all had the same ViewModel.
  3. All the extra code and files!

My suspicion is this UserControl abuse comes from developers used to UserControls in ASP where they can define a section of View.

Should XAML UserControl's be used like this?

markmnl
  • 727
  • 1
  • 6
  • 18
  • 5
    What's the alternative? – MetaFight Feb 03 '15 at 09:22
  • @MetaFight don't split your Views into sub views that are not not naturally sub views, i.e. keep it all in the Page. – markmnl Feb 03 '15 at 13:52
  • I don't see how splitting a view makes it more difficult to communicate with a single view model, but that's not what I was asking about. What I was asking is: If a you're suggesting Views shouldn't be `UserControl`s then what else could they be? Would they derive from another class? – MetaFight Feb 03 '15 at 14:00
  • Your containing Page or Window is the View! No need to create multiple Views within your containing View. – markmnl Feb 03 '15 at 14:05
  • 2
    I've always been in the habit of making my Views `UserControls`s so they're not bound to a specific presentation (window, page, whatever). – MetaFight Feb 03 '15 at 14:07
  • Also, if the `DataContext` is set properly on your top-most view, and the top-most view references the sub-views, then it Just Works. I don't see what the hassle is. – MetaFight Feb 03 '15 at 14:08
  • You and many others, I don't understand why, UserControls are for designed for creating Controls. There is no need to put something in a UserControl unless you want a re-usable control all over the place. Your presentation is View why compose it of single use smaller views? – markmnl Feb 03 '15 at 14:09
  • The hassle for me is my 3 points above – markmnl Feb 03 '15 at 14:09
  • I break my complex Views into smaller ones for ease of maintenance. When I first started with WPF I also felt odd about use `UserControl` as my View baseclass because, well, my View wasn't a UserControl in the WinForms sense. I vaguely remember looking for a less specific class I could derive from and finding nothing. Maybe there is a better way and I just never found it. Maybe not. Either way, `UserControl` does the job and seems to be a standard approach. – MetaFight Feb 03 '15 at 14:15
  • 1
    You have made a mindset that a `UserControl` can only be reusable which need not be the case always. It serves a good option of separating your view code as well. And that's the point @MetaFight is making here. It's much better to split your view (Yes! using `UserControl`) rather than having view.xaml containing 100s of lines. – Nikhil Vartak Apr 18 '16 at 19:17

0 Answers0