WPF controls don??™t include parameterized constructors,
so even adding a simple button to a window takes several lines of code. One potential
advantage is that code-only development offers unlimited avenues for customization. For
example, you could generate a form full of input controls based on the information in a database
record, or you could conditionally decide to add or substitute controls depending on the
current user. All you need is a sprinkling of conditional logic. By contrast, when you use XAML
documents they??™re embedded in your assembly as fixed unchanging resources.
nNote Even though you probably won??™t create a code-only WPF application, you probably will use the
code-only approach to creating a WPF control at some point when you need an adaptable chunk of user
interface.
Following is the code for a modest window with a single button and an event handler
(see Figure 2-3). When the window is created, the constructor calls an InitializeComponent()
method that instantiates and configures the button and the form and hooks up the event
handler.
nNote To create this example, you must code the Window1 class from scratch (right-click the Solution
Explorer, and choose Add ??° Class to get started). You can??™t choose Add ??°Window, because that will add a
code file and a XAML template for your window, complete with an automatically generated InitializeComponent()
method.
Pages:
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159