0

我开发了一个应用程序来从 mainpage.xaml 获取经度和纬度,现在我在另一个页面 page2.xaml 中,如何获取从 mainpage.xaml 获取的经度和纬度代码?

下面是我的 mainpage.xaml.cs 代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Controls.Maps;
using System.Device.Location;
using System.Threading;


namespace Fyp
{
public partial class MainPage : PhoneApplicationPage
{
    /// <summary>
    /// This sample receives data from the Location Service and displays the geographic coordinates of the device.
    /// </summary>

    GeoCoordinateWatcher watcher;
    string accuracyText = "";
    Pushpin myPushpin = new Pushpin();

    /// <summary>
    /// Constructor for the PhoneApplicationPage object
    /// </summary>
    public MainPage()
    {
        InitializeComponent();
    }



    #region User Interface

    /// <summary>
    /// Click event handler for the low accuracy button
    /// </summary>
    /// <param name="sender">The control that raised the event</param>
    /// <param name="e">An EventArgs object containing event data.</param>
    private void NextButtonClick(object sender, EventArgs e)
    {
        // Start data acquisition from the Location Service, low accuracy
        NavigationService.Navigate(new Uri("/workshop.xaml", UriKind.Relative));
    }

    /// <summary>
    /// Click event handler for the high accuracy button
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void StartButtonClick(object sender, EventArgs e)
    {
        // Start data acquisition from the Location Service, high accuracy
        accuracyText = "FIT Building Multimedia University, Cyberjaya, 63000 Selangor";
        watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
        watcher.MovementThreshold = 100;
        StartLocationService(GeoPositionAccuracy.High);

        watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
        watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
        myMap.ZoomLevel = 16.0f;
        watcher.Start();
    }
    private void StopButtonClick(object sender, EventArgs e)
    {
        if (watcher != null)
        {
            watcher.Stop();
        }
        StatusTextBlock.Text = "location service is off";
        LatitudeTextBlock.Text = " ";
        LongitudeTextBlock.Text = " ";
    }

    #endregion

    #region Application logic

    /// <summary>
    /// Helper method to start up the location data acquisition
    /// </summary>
    /// <param name="accuracy">The accuracy level </param>
    private void StartLocationService(GeoPositionAccuracy accuracy)
    {
        // Reinitialize the GeoCoordinateWatcher
        StatusTextBlock.Text = "starting, " + accuracyText;
        watcher = new GeoCoordinateWatcher(accuracy);
        watcher.MovementThreshold = 20;

        // Add event handlers for StatusChanged and PositionChanged events
        watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
        watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);

        // Start data acquisition
        watcher.Start();
    }

    /// <summary>
    /// Handler for the StatusChanged event. This invokes MyStatusChanged on the UI thread and
    /// passes the GeoPositionStatusChangedEventArgs
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
    {
        Deployment.Current.Dispatcher.BeginInvoke(() => MyStatusChanged(e));

    }
    /// <summary>
    /// Custom method called from the StatusChanged event handler
    /// </summary>
    /// <param name="e"></param>
    void MyStatusChanged(GeoPositionStatusChangedEventArgs e)
    {
        switch (e.Status)
        {
            case GeoPositionStatus.Disabled:
                // The location service is disabled or unsupported.
                // Alert the user
                StatusTextBlock.Text = "location is unsupported on this device";
                break;
            case GeoPositionStatus.Initializing:
                // The location service is initializing.
                // Disable the Start Location button
                StatusTextBlock.Text = "initializing location service," + accuracyText;
                break;
            case GeoPositionStatus.NoData:
                // The location service is working, but it cannot get location data
                // Alert the user and enable the Stop Location button
                StatusTextBlock.Text = "data unavailable," + accuracyText;
                break;
            case GeoPositionStatus.Ready:
                // The location service is working and is receiving location data
                // Show the current position and enable the Stop Location button
                StatusTextBlock.Text = " " + accuracyText;
                break;

        }
    }

    /// <summary>
    /// Handler for the PositionChanged event. This invokes MyStatusChanged on the UI thread and
    /// passes the GeoPositionStatusChangedEventArgs
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
    {
        Deployment.Current.Dispatcher.BeginInvoke(() => MyPositionChanged(e));
    }

    /// <summary>
    /// Custom method called from the PositionChanged event handler
    /// </summary>
    /// <param name="e"></param>
    void MyPositionChanged(GeoPositionChangedEventArgs<GeoCoordinate> e)
    {
        // Update the TextBlocks to show the current location
        LatitudeTextBlock.Text = e.Position.Location.Latitude.ToString("0.000");
        LongitudeTextBlock.Text = e.Position.Location.Longitude.ToString("0.000");
        // center the pushpin and map on the current position
        myPushpin.Location = e.Position.Location;
        myMap.Center = e.Position.Location;
        // if this is the first time that myPushpin is being plotted, plot it!
        if (myMap.Children.Contains(myPushpin) == false) { myMap.Children.Add(myPushpin); };


    }

    #endregion

    private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
    {

    }
}

}

4

1 回答 1

1

您应该设置查询字符串参数

NavigationService.Navigate(new Uri(String.Format("/workshop.xaml?lat={0}&lng={1}", currentLat, currentLng), UriKind.Relative));

然后在你的第二页:

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
    base.OnNavigatedTo(e);

    string lat = NavigationContext.QueryString["lat"];
    string lng = NavigationContext.QueryString["lng"];
}
于 2012-11-20T16:43:14.023 回答