One way of looking at type safety is that it adds automatic tests all over your code that stop some things breaking in some ways. One of the tools that helps this in .NET is generics.
However, both WinForms and WPF are generics-free. There is no ListBox<T>
control, for example, which could only show items of the specified type. Such controls invariably operate on object
instead.
Why are generics and not popular with UI framework developers?
3 Answers 3
WinForms came with .NET 1.0 before there was support for generics. In the case of WPF, development started on that before the Framework supported generics as well. Although generics support came to .NET before WPF was released, it would have taken substantial effort to go back and add it in.
Also, the XAML to represent generics is not pretty
-
1I get the impression that XAML was never meant to be hand-written, which makes the ugliness argument moot. XAML is already a PITA to hand-write. As for the history thing – that’s very interesting to know, do you have a reference?Roman Starkov– Roman Starkov2012年04月13日 14:06:27 +00:00Commented Apr 13, 2012 at 14:06
-
I still write most of my XAML by hand. And even when I use Blend for coarse layout and animations, I still go in and touch it up by hand after the fact. What I should have pointed out is that XAML didn't support generics in V1 (except for as part of the root element)Michael Brown– Michael Brown2012年04月13日 14:08:43 +00:00Commented Apr 13, 2012 at 14:08
In case of WinForms it's backward compatibility with early .NET versions that didn't support generics (hence all those object sender
s in event handlers), while WPF implements databinding differently ({Binding Path=Name}
etc.; it works by reflection).
-
Is there a good reason why
Binding
itself can’t be generic in source and destination, implementingIBindingSource<TSrc>
andIBindingDestination<TDest>
?Roman Starkov– Roman Starkov2012年04月13日 12:34:30 +00:00Commented Apr 13, 2012 at 12:34 -
@romkyns maybe because it would force you to update TSrc and TDest twice any time it is necessary (in both the class - model - and the XAML view)? I'm guessing / thinking loudKonrad Morawski– Konrad Morawski2012年04月14日 09:56:31 +00:00Commented Apr 14, 2012 at 9:56
For a UI framework it makes much more sense to take objects and bind reflexively than to strongly-type the interfaces somehow. Lots of cases where you need to throw completely arbitrary objects into arbitrary UI containers in cases where you can't divine the types at runtime.
-
1
ListBox<object>
solves that perfectly fine, so I’m not sure this answer explains anything. And besides, 99% of the time I only put specific object types into my UIs.Roman Starkov– Roman Starkov2012年04月13日 14:17:34 +00:00Commented Apr 13, 2012 at 14:17
java.awt
andjavax.swing
were developed before generics came to java they remained without generics to provide backwards compatibility