Solved

C# updating controls using events from another class

Posted on 2010-11-10
6
775 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

When I was a beginner in Windows Mobile programming, even for small test I used to create a single MFC dialog-based application with one button, and put my test code in the OnLButtonDown method. For Pocket PC it worked fine, on some Windows CE d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

696 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