Solved

C# updating controls using events from another class

Posted on 2010-11-10
6
773 Views
Last Modified: 2013-12-27
Hey guys,

i'm basically trying to use an event defined in another class to update a control in a separate class, no errors, but the event doesn't seem to be firing, I'm obviously doing something wrong but just don't know what.
public class GPSCommand
{
    public GeoCoordinateWatcher GeoWatcher;

    public event EventHandler<GeoPositionStatusChangedEventArgs>
    GeoWatcher_Status;

    public void StartGeoWatcher()
    {
            
        if (GeoWatcher == null)
        {
            GeoWatcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default);
 
            GeoWatcher.MovementThreshold = 20;

            GeoWatcher.Start();

        }

    }

}

// And then the class with the event handler function...

public partial class MainPage : PhoneApplicationPage
{
    private GPSCommand GPS = new GPSCommand();

    private void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        // Get current position..
        GPS.StartGeoWatcher();
        GPS.GeoWatcher_Status += new EventHandler<System.Device.Location.GeoPositionStatusChangedEventArgs>(GPS_GeoWatcher_StatusChanged);
        }

        // Event handler for the GeoCoordinateWatcher.StatusChanged event.
        private void GPS_GeoWatcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
        {
            switch (e.Status)
            {
                case GeoPositionStatus.Disabled:
                    // The Location Service is disabled or unsupported.
                    // Check to see if the user has disabled the Location Service.
                    if (GPS.GeoWatcher.Permission == GeoPositionPermission.Denied)
                    {
                        
                        // The user has disabled the Location Service on their device.

                        tbStatus.Text = "you have this application access to location.";
                    }
                    else
                    {
                        tbStatus.Text = "location is not functioning on this device";
                    }
                    break;

                case GeoPositionStatus.Initializing:
                    tbStatus.Text = "Initializind data location service...";
                    //startLocationButton.IsEnabled = false;
                    break;

                case GeoPositionStatus.NoData:
                    tbStatus.Text = "location data is not available.";
                    //stopLocationButton.IsEnabled = true;
                    break;

                case GeoPositionStatus.Ready:
                    tbStatus.Text = "location data is available.";
                    //stopLocationButton.IsEnabled = true;
                    break;
            }
        }
    }

Open in new window

0
Comment
Question by:aarmstr2
  • 4
  • 2
6 Comments
 
LVL 24

Accepted Solution

by:
alexey_gusev earned 500 total points
ID: 34100983
sorry for being dumb :) , but where's the code where you actually fire the event in your 1st class code? something like

...
if (GeoWatcher_Status != null)
     GeoWatcher_Status();
...
0
 
LVL 1

Author Comment

by:aarmstr2
ID: 34101154
Doh, i can such a restard sometimes! Changed this line as follows;-

GPS.GeoWatcher_Status += new EventHandler<System.Device.Location.GeoPositionStatusChangedEventArgs>(GPS_GeoWatcher_StatusChanged);

to....

GPS.GeoWatcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(GPS_GeoWatcher_StatusChanged);

Works now... :S
0
 
LVL 1

Author Closing Comment

by:aarmstr2
ID: 34101157
For making me think!
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 1

Author Comment

by:aarmstr2
ID: 34101171
btw, do u really think my code was 1st class or were u being sarcastic?
0
 
LVL 24

Expert Comment

by:alexey_gusev
ID: 34101222
ah, all I meant is to save on typing the class name, sorry :) , so it was as in "your first class" :)
0
 
LVL 1

Author Comment

by:aarmstr2
ID: 34101230
lol
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about problems I had with the new location API and permissions in Marshmallow
You should read OS supplied guidelines before developing. I can't stress that enough. The guidelines will help you understand the reasons mobile app developers do what they do.  Apple is very particular when they review appstore submissions.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question