简体   繁体   English

在Windows Phone 8中跟踪位置时发生异常

[英]Exception while tracking location in Windows Phone 8

When I try to track location it works perfectly but when i add service reference to it it throws an exception when I try the same program without adding location only add service reference it works perfectly My code is here below while copy from How to continuously track the phone's location for Windows Phone 8 当我尝试跟踪它完美的位置,但是当我的服务引用添加到它,当我尝试同样的程序而无需添加位置只添加服务引用它的工作原理抛出一个异常完美的我的代码是在这里之下,而从副本如何不断跟踪Windows Phone 8手机的位置

public partial class MainPage : PhoneApplicationPage
{

    Geolocator geolocator = null;
    bool tracking = false;
    ServiceReference2.GetPositionClient client = new ServiceReference2.GetPositionClient();
    // Constructor
    public MainPage()
    {
        InitializeComponent();

        // Sample code to localize the ApplicationBar
        //BuildLocalizedApplicationBar();
    }
    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        if (IsolatedStorageSettings.ApplicationSettings.Contains("LocationConsent"))
        {
            // User has opted in or out of Location
            return;
        }
        else
        {
            MessageBoxResult result =
                MessageBox.Show("This app accesses your phone's location. Is that ok?",
                "Location",
                MessageBoxButton.OKCancel);

            if (result == MessageBoxResult.OK)
            {
                IsolatedStorageSettings.ApplicationSettings["LocationConsent"] = true;
            }
            else
            {
                IsolatedStorageSettings.ApplicationSettings["LocationConsent"] = false;
            }

            IsolatedStorageSettings.ApplicationSettings.Save();
        }
    }
    private void TrackLocation_Click(object sender, RoutedEventArgs e)
    {
        if ((bool)IsolatedStorageSettings.ApplicationSettings["LocationConsent"] != true)
        {
            // The user has opted out of Location.
            return;
        }

        if (!tracking)
        {
            geolocator = new Geolocator();
            geolocator.DesiredAccuracy = PositionAccuracy.High;
            geolocator.MovementThreshold = 100; // The units are meters.

            geolocator.StatusChanged += geolocator_StatusChanged;
            geolocator.PositionChanged += geolocator_PositionChanged;

            tracking = true;
            TrackLocationButton.Content = "stop tracking";
        }
        else
        {
            geolocator.PositionChanged -= geolocator_PositionChanged;
            geolocator.StatusChanged -= geolocator_StatusChanged;
            geolocator = null;

            tracking = false;
            TrackLocationButton.Content = "track location";
            StatusTextBlock.Text = "stopped";
        }
    }
    void geolocator_StatusChanged(Geolocator sender, StatusChangedEventArgs args)
    {
        string status = "";

        switch (args.Status)
        {
            case PositionStatus.Disabled:
                // the application does not have the right capability or the location master switch is off
                status = "location is disabled in phone settings";
                break;
            case PositionStatus.Initializing:
                // the geolocator started the tracking operation
                status = "initializing";
                break;
            case PositionStatus.NoData:
                // the location service was not able to acquire the location
                status = "no data";
                break;
            case PositionStatus.Ready:
                // the location service is generating geopositions as specified by the tracking parameters
                status = "ready";
                break;
            case PositionStatus.NotAvailable:
                status = "not available";
                // not used in WindowsPhone, Windows desktop uses this value to signal that there is no hardware capable to acquire location information
                break;
            case PositionStatus.NotInitialized:
                // the initial state of the geolocator, once the tracking operation is stopped by the user the geolocator moves back to this state

                break;
        }

        Dispatcher.BeginInvoke(() =>
        {
            StatusTextBlock.Text = status;
        });
    }





    void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
    {
        client.getPosCompleted += new EventHandler<ServiceReference2.getPosCompletedEventArgs>(sendData);

        client.getPosAsync(11,11);


        Dispatcher.BeginInvoke(() =>
        {
            LatitudeTextBlock.Text = args.Position.Coordinate.Latitude.ToString("0.00");
            LongitudeTextBlock.Text = args.Position.Coordinate.Longitude.ToString("0.00");
        });
    }


    public void sendData(object sender, ServiceReference2.getPosCompletedEventArgs e)
    {

        dd.Text = e.Result;

    }
    }

you have 你有

client.getPosCompleted += new EventHandler<ServiceReference2.getPosCompletedEventArgs>(sendData);

but you haven't given Client any values anywhere else, I assume that you are getting a null Reference exception, and that this is why. 但是您没有在其他任何地方给Client任何值,我假设您收到的是null引用异常,这就是原因。

它只是解决了它,只是IIS设置的错误,因为移动设备和PC位于不同的网络上,因此无法进行通信。我只是转发路由器设置中的端口–

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM