![](/img/trans.png)
[英]Current Time in TextBox. in C# visual studio 2010 windows phone
[英]C# Project in Visual Studio 2010 for Windows Phone Notefunction
我试图通过保存gps时使用gps坐标使我的notefunction发布您当前所在的城市。 目前,它仅显示“未知位置”。 我现在有点迷失了,我花了很长时间尝试使用此代码才能使其正常工作,所以请有人能告诉我我做错了什么吗?
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 Service传入并正确格式化,请确认该数据实际上已发送到Terra Web Service,并且已从Web Service调用返回了数据。 如果您的Terra Web Service仍返回“未知位置”,请重试,但这一次在主要城市附近绘制GPS位置图,以增加Web服务知道您靠近哪个城市的几率。 如果您仍返回“未知位置”,则可以确定问题出在Web服务提供商。
以我在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.
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.