Question

This is what I have so far which calls a GetCoordinates method and navigates to the map on a button click. I'm wondering though how I would pass over the coordinate data.

Does anyone know how I could pass the MyGeoPosition variable of type GeoPosition to the OnNavigatedTo method of my map class? I know how to call a method from another class but not how to pass data such as a variable.

private async Task GetCoordinates(string name = "My Car")
        {
            await Task.Run(async () =>
            {
                // Get the phone's current location.
                Geolocator MyGeolocator = new Geolocator();
                //need to pass the below variable containing coordinate data..
                MyGeolocator.DesiredAccuracyInMeters = 5;
                Geoposition MyGeoPosition = null;
                try
                {
                    MyGeoPosition = await MyGeolocator.GetGeopositionAsync(TimeSpan.FromMinutes(1), TimeSpan.FromSeconds(10));

                }
                catch (UnauthorizedAccessException)
                {
                    MessageBox.Show("Location is disabled in phone settings or capabilities are not checked.");
                }
                catch (Exception ex)
                {
                    // Something else happened while acquiring the location.
                    MessageBox.Show(ex.Message);
                }
            });
        }


        //sets location of parking space using the GetCoordinates method
        //opens map 
        private async void setLocationBtn_Click(object sender, RoutedEventArgs e)
        { 
            await this.GetCoordinates();
            NavigationService.Navigate(new Uri("/Maps.xaml", UriKind.Relative));
        }
Was it helpful?

Solution

Try something like this

FirstPage

this.NavigationService.Navigate(new Uri(string.Format("LocationView.xaml?GeoX={0}&GeoY={1}", GeoX, GeoY), UriKind.Relative));  

secondPage

  if (NavigationContext.QueryString.ContainsKey("GeoX") && NavigationContext.QueryString.ContainsKey("GeoY"))
  {
  double GeoX =Convert.ToDouble(NavigationContext.QueryString["GeoX"].ToString());
  double GeoY = Convert.ToDouble(NavigationContext.QueryString["GeoY"].ToString());
  ....
  }

OTHER TIPS

You could use PhoneApplicationservice to pass data between pages in windows phone application. Here is good example about PhoneApplicationservice. Here is a short example how PhoneApplicationService works, may this will help you.

private async void setLocationBtn_Click(object sender, RoutedEventArgs e)
   { 
     await this.GetCoordinates();
    PhoneApplicationService.Current.State["Data"] = your data;
     NavigationService.Navigate(new Uri("/Maps.xaml", UriKind.Relative));
    }

//On Second page

 protected override void OnNavigatedTo(NavigationEventArgs e)
    {
     var data =PhoneApplicationService.Current.State["Data"] as Cast your type
     PhoneApplicationService.Current.State.Remove("Data");
    }

You can pass data by four ways which is clearly explained in following post

http://nishantcop.blogspot.in/2011/08/passing-data-between-pages-in-windows.html

Found another way in my searching for another issue:

http://msdn.microsoft.com/en-us/library/windows/apps/hh771188.aspx

Scroll down to: Passing information between pages

Its a lot simpler than my solution above, but my solution has other requirements hence why I chose that one, but for your needs, this is a better way.

I had a similar issue where I was passing user credentials between classes and I decided to use IsolatedStorageSettings class. But I have read that Windows will be depreciating this class in the future as it moves to merge Windows and Windows Phone code.

SO, this is the class I believe Microsoft want you to use so that you dont get stuck with a depreciated class in the future, and its called Windows.storage.

Hope this helps.

My case as said if for passing username and password along with if the user was a premium user and if when the app starts if they are already logged on. It would then re-log the user on automatically.

Here I create the storage in MainPage class

IsolatedStorageSettings myUserSettings = IsolatedStorageSettings.ApplicationSettings;

Here is the MainPage class method:

 private void GetUserData()
        {
           // System.Diagnostics.Debug.WriteLine("Grabbing Data");

            if (IsolatedStorageSettings.ApplicationSettings.Contains("userLoggedIn"))
            {
                string isLoggedIn = IsolatedStorageSettings.ApplicationSettings["userLoggedIn"] as string;
                if (isLoggedIn.EndsWith("rue"))
                    isLoggedOn = true;
                else
                    isLoggedOn = false;
            //    System.Diagnostics.Debug.WriteLine("log in data " + isLoggedIn + " " + isLoggedOn);
            }
            else
            {
                myUserSettings.Add("userLoggedIn", "false");
                isLoggedOn = false;
            }



            if (IsolatedStorageSettings.ApplicationSettings.Contains("fullAccess"))
            {
                string hasFullAccess = IsolatedStorageSettings.ApplicationSettings["fullAccess"] as string;
                if (hasFullAccess.EndsWith("rue"))
                    fullAccess = true;
                else
                    fullAccess = false;
            }
            else
            {
                myUserSettings.Add("fullAccess", "false");
                fullAccess = false;
            }


            if (IsolatedStorageSettings.ApplicationSettings.Contains("username"))
            {
                username = IsolatedStorageSettings.ApplicationSettings["username"] as string;
            }
            else
            {
                myUserSettings.Add("username", "");
                username = "me";
            }

            if (IsolatedStorageSettings.ApplicationSettings.Contains("password"))
            {
                password = IsolatedStorageSettings.ApplicationSettings["password"] as string;
            }
            else
            {
                myUserSettings.Add("password", "");
                password = "v";
            }
            myUserSettings.Save();


        }

Now in my Login Class I have to create the storage variable again

IsolatedStorageSettings myUserSettings = IsolatedStorageSettings.ApplicationSettings; 

And now once I verfied the user I write the relevant information to the storage file: (parts of method missing as irrelevant)

       // Here I have just finished using JSON to extra info from a JSON response
    if (success.EndsWith("rue"))
    {
        if (!myUserSettings.Contains("userLoggedIn"))
        {
            myUserSettings.Add("userLoggedIn", success);
        }
        else
        {
            myUserSettings["userLoggedIn"] = success;
        }


        if (!myUserSettings.Contains("username"))
        {
            myUserSettings.Add("username", username);
        }
        else
        {
            myUserSettings["username"] = username;
        }

        if (!myUserSettings.Contains("password"))
        {
            myUserSettings.Add("password", password);
        }
        else
        {
            myUserSettings["password"] = password;
        }

        if (!myUserSettings.Contains("fullAccess"))
        {
            myUserSettings.Add("fullAccess", fullAccess);
        }
        else
        {
            myUserSettings["fullAccess"] = fullAccess;
        }
        myUserSettings.Save();

and if something does not work, check that you did save the file as follows:

       myUserSettings.Save();

Hope you can make sense of my example but please refer to the doco from Microsoft. This link shows a simple example I used to solve my requirements.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top