![](/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.