?
Solved

C# updating controls using events from another class

Posted on 2010-11-10
6
Medium Priority
?
777 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 2000 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
On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Preface: This article is part of a series focused on cross platform mobile app development (specifically Android and iOS) using the Alloy framework and Titanium Studio made by Appcelerator (https://www.appcelerator.com/). This article presumes a wor…
Learn how to use the free Acronis True Image app to easily transfer data between iPhones and Android phones.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month11 days, 20 hours left to enroll

752 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