by
kirupa | 9 December 2007
In the
previous page,
you wrapped up work on the PeopleData class. In this
page, you will learn how to use this class by
modifying the AddButton_Click event handler to get
everything working...again.
One of the earlier gripes was that the
AddButton_Click event handler simply contains too
much important code. It would be better to offload
some of that code to a dedicated part of your
project where the data-related tasks are handled. So
far, we have done that by creating our PeopleData
class.
Let's finish up our last task by modifying our
AddButton_Click event handler to use the new methods
created in the PeopleData class. Make sure you have
Window1.xaml.cs open in Visual Studio. Your
AddButton_Click event handler currently contains the
following code:
- private
void
AddButton_Click(object
sender,
RoutedEventArgs
e)
- {
- ObjectDataProvider
odp
=
this.FindResource("PeopleListDS")
as
ObjectDataProvider;
- PeopleList
people
=
odp.Data
as
PeopleList;
-
- Person
newPerson
=
new
Person();
- newPerson.PersonName
=
NameInput.Text;
-
- people.Add(newPerson);
-
- NameInput.Text
=
String.Empty;
- }
Delete everything except for the last line where
you have NameInput.Text =
String.Empty. Copy and paste the following
one line of code to the top of your AddButton_Click
event handler:
- PeopleData.AddPerson(NameInput.Text);
Your AddButton_Click event handler will now look
like the following:
- private
void
AddButton_Click(object
sender,
RoutedEventArgs
e)
- {
- PeopleData.AddPerson(NameInput.Text);
-
- NameInput.Text
=
String.Empty;
- }
Go ahead and test out
your application. Notice that everything still works
just like before, and best of all, we greatly
improved how our application is structured. Now,
let's go back a few steps and look at why the code
you added works.
In the past few pages, you copied and pasted some
code. I haven't fully explained what each line of
code you added does, so let's go back and look at it
in greater detail:
- public
static
ObjectDataProvider
PeopleDataSource
- {
- get;
- set;
- }
In your PeopleData
class you have a static property of type
ObjectDataProvider called PeopleDataSource. This
property is fairly simple and contains just your
standard get and set statements that allow you to
store or retrieve a value.
- public
static
void
AddPerson(string
name)
- {
- Person
newPerson
=
new
Person();
- newPerson.PersonName
=
name;
-
- (PeopleData.PeopleDataSource.Data
as
PeopleList).Add(newPerson);
- }
The only other thing
in your PeopleData class that needs revisiting is
our static AddPerson method. This method takes an
argument for a person's name as a string and assigns
it to the PersonName field of a new Person object.
In the final line, I
call the earlier PeopleData source property. Because
it returns data in the form of an
ObjectDataProvider, I am able to use the Data
property to get at the PeopleList that I am
interested in. Notice that I am typecasting the
returned values as a PeopleList. Once I have access
to the PeopleList data, I can use the Add method to
add the new Person object I created. This ensures
that our listbox gets the new value.
We are almost done
with this tutorial! In the
next page, let's look at the Initialized code we
added and review everything you've done by
running through our application to see how
everything works together.
Onwards to the
next page!
|