Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Removing a class reference when updating a WPF UI

0 views
Skip to first unread message

Jan Axelson

unread,
Dec 2, 2009, 10:19:14 PM12/2/09
to
My application has a class called Program and a class called Gps. The
Program class responds to several events in the Gps class. If
possible, I would like the Gps class to be free of specific references
to the Program class.

One event tells the Program class to stop a timer. The Program class
assigns an event handler and provides the StopTimerEvent routine to
stop the timer:

Gps.StopTimerEvent += new Gps.StopTimerEventHandler(StopTimerEvent);

private static void StopTimerEvent()
{
// Stop a timer.
}

In the Gps class, a delegate stops the timer by calling StopTimerEvent
in the Program class:

internal delegate void StopTimerEventHandler();
internal static event StopTimerEventHandler StopTimerEvent;

if (null != StopTimerEvent)
{
StopTimerEvent();
}

An event that displays text on the touch panel uses the Dispatcher. In
the Program class, an UpdateText routine displays the text in
sentenceAsStringArray on a touch panel:

public static Program mainProgram;

internal void UpdateText(String[] sentenceAsStringArray)
{
// Display text on touch panel.
}

In the Gps class, the Dispatcher invokes the UpdateText routine to
display the information:

private delegate void UpdateTextDelegate(String[]
sentenceAsStringArray);

Program.mainProgram.Dispatcher.BeginInvoke(new UpdateTextDelegate
(Program.mainProgram.UpdateText), new object[1]
{ sentenceAsStringArray });

This works, but in the last statement above, the Gps class specifies
Program.mainProgram by name. Is there a way to accomplish the same
thing without having to name the class and instance of the UpdateText
routine? This way, the event code could be more generic, like
StopTimerEvent, and not tied to a specific external class and
instance.

Jan Kučera

unread,
Dec 3, 2009, 2:12:29 AM12/3/09
to
Hello Jan,
without discussing the rest of the code, what's problem with doing the
UpdateText the same way as you do the StopTimer? (so that the Program class
would need to attach a handler to your UpdateTextEvent...) If you want your
class to be more generic, it should be able to handle clients without
display anyway, and unless you are using some DispatcherObjects inside,
there is no reason to worry about Dispatcher at all, that should be a
problem of the event's receiver.

Jan

"Jan Axelson" <jan...@gmail.com> wrote in message
news:631ff67b-a693-4ec2...@v25g2000yqk.googlegroups.com...

Jan Axelson

unread,
Dec 3, 2009, 10:28:43 AM12/3/09
to
I’ve tried creating an event that calls a routine in Program to update
the display, similar to how my other events work. But when the routine
in Program tries to access the display with:

statusText.TextRuns.Clear();

I get:

System.InvalidOperationException

yet doing the same via the Dispatcher code works OK.

Jan Kučera

unread,
Dec 3, 2009, 3:22:51 PM12/3/09
to
Oh the Dispatcher in Program is okay, I meant that it shouldn't have been in
the Gps one...

Jan

"Jan Axelson" <jan...@gmail.com> wrote in message

news:b6409bb9-93bb-4a22...@s31g2000yqs.googlegroups.com...

Jan Axelson

unread,
Dec 4, 2009, 10:40:49 AM12/4/09
to
Yes! I kept the event but moved the Dispatcher to the Program class,
and all is well. Thanks much for your advice.

Jan


0 new messages