Visual Studio 2010中用于Windows Phone Note函数的C#项目



我试图让我的note函数在保存笔记时使用gps坐标来发布您所在的当前城市。现在它只显示"未知位置"。我现在有点迷路了,我已经用这个代码工作了这么长时间,试图让它发挥作用,所以有人能告诉我我做错了什么吗?

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 System.Device.Location;
using System.Text;
using System.IO.IsolatedStorage;
using System.IO;
using Secret.myTerraService;
namespace Secret
{
public partial class AddNotePage : PhoneApplicationPage
{
private IsolatedStorageSettings settings =     IsolatedStorageSettings.ApplicationSettings;
private string location = "";
#region Hämtar din geografiska position
public AddNotePage()
{
InitializeComponent();
GeoCoordinateWatcher watcher;
watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default)
{
MovementThreshold = 20
};
watcher.PositionChanged += this.watcher_PositionChanged;
watcher.StatusChanged += this.watcher_StatusChanged;
watcher.Start();
}
private void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
switch (e.Status)
{
case GeoPositionStatus.Disabled:
// location is unsupported on this device
break;
case GeoPositionStatus.NoData:
// data unavailable
break;
}
}
private void watcher_PositionChanged(object sender,   GeoPositionChangedEventArgs<GeoCoordinate> e)
{
var epl = e.Position.Location;
// Access the position information thusly:
epl.Latitude.ToString("0.000");
epl.Longitude.ToString("0.000");
epl.Altitude.ToString();
epl.HorizontalAccuracy.ToString();
epl.VerticalAccuracy.ToString();
epl.Course.ToString();
epl.Speed.ToString();
e.Position.Timestamp.LocalDateTime.ToString();


}
void client_ConvertLonLatPtToNearestPlaceCompleted(object sender,   myTerraService.ConvertLonLatPtToNearestPlaceCompletedEventArgs e)
{
location = e.Result;
//throw new NotImplementedException();
}

#endregion
#region Knappfunktioner
private void AppBar_Cancel_Click(object sender, EventArgs e)
{
navigateBack();
}
private void AppBar_Save_Click(object sender, EventArgs e)
{ // spara en ny anteckning
if (location.Trim().Length == 0)
{
location = "Okänd Plats";
}
// skapa namnet på filen
StringBuilder sb = new StringBuilder();
sb.Append(DateTime.Now.Year);
sb.Append("_");
sb.Append(String.Format("{0:00}", DateTime.Now.Month));
sb.Append("_");
sb.Append(String.Format("{0:00}", DateTime.Now.Day));
sb.Append("_");
sb.Append(String.Format("{0:00}", DateTime.Now.Hour));
sb.Append("_");
sb.Append(String.Format("{0:00}", DateTime.Now.Minute));
sb.Append("_");
sb.Append(String.Format("{0:00}", DateTime.Now.Second));
sb.Append("_");
location = location.Replace(" ", "-");
location = location.Replace(", ", "_");
sb.Append(location);
sb.Append(".txt");
//spara filen i Isolated Storage
var appStorage = IsolatedStorageFile.GetUserStoreForApplication();
try
{
using (var fileStream = appStorage.OpenFile(sb.ToString(),  System.IO.FileMode.Create))
{
using (StreamWriter sw = new StreamWriter(fileStream))
{
sw.WriteLine(editTextBox.Text);
}
}
}
catch
{
// åtgärda vid senare tillfälle..
}

//Klart Navigera tillbaka till NoteMainPage
navigateBack();
}

在测试这一点时,我可以看到代码可能会中断的一些地方。您应该使用断点进行调试,以实际确认您的应用程序正在获取GPS位置数据。如果没有,请使用Windows Phone模拟器并运行GPS模拟(然后再次确认)。

接下来,一旦您知道您的GPS数据进入并为Terra Web服务正确格式化,请确认数据实际上正在发送到Terra Web Service,并且数据是从Web服务调用返回的。如果您的Terra网络服务仍然返回"未知位置",请重试,但这次绘制主要城市附近的GPS位置,以增加网络服务知道您所在城市的几率。如果您仍然返回"不明位置",则您可以相当确定问题出在网络服务提供商。

根据我使用Windows Phone定位服务的经验(我只使用过带WiFi接入的开发手机(即没有sim卡)),位置数据有时需要几秒钟或几分钟才能获取。如果你在地下室或GPS无法找到你的区域的物理开发手机上测试,很可能数据没有生成。此外,因为Windows Phone的位置数据不一定是即时的,所以你不能总是随时调用它并期望它准备好位置数据。根据我的经验,我让用户选择使用定位服务(根据Windows Phone Marketplace提交要求),然后让后台代理在用户使用应用程序时提取位置数据。这样,位置数据可能会在用户需要时准备好(就像您的示例中用户保存注释时的情况一样)。

以下是我在C#中为您制作的一个适用于您的Windows Phone应用程序的工作示例。为了简洁和节省时间,示例是一个控制台应用程序。如果你还想不出来,我会为Windows Phone编码。尽管如此,您确实拥有使其工作所需的一切,只需插入lat和long变量即可。下载工作源代码(Visual Studio 2010)

C#源代码片段

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TerraServiceExample.com.msrmaps; // add the service using statement
// http://msrmaps.com/terraservice2.asmx
namespace TerraServiceExample
{
class Program
{
/// <summary> The main entry point for the application. </summary>
static void Main(string[] args)
{
// Create the GPS point from your location services data
LonLatPt location = new LonLatPt();
// Modify Lat and Lon based on your needs
// This example uses the GPS Coordinates for "Eau Claire, Wisconsin, United States"
location.Lat = 44.811349;
location.Lon = -91.498494;
// Create a new TerraService object
TerraService ts = new TerraService();
// Output the nearest location from the TerraService
Console.WriteLine(ts.ConvertLonLatPtToNearestPlace(location));
// For console app to stay open/close easily
Console.WriteLine("Press any key to close window...");
Console.ReadKey();
// Lastly, appreciate the Microsoft folks that made this available for free
// They are all interesting individuals but you should read about Jim Gray via Wikipedia to
// understand some history behind this cool web service.
}
}
}

最新更新