【发布时间】:2015-03-31 21:47:04
【问题描述】:
系统信息
- Windows 10 技术预览版(内部版本 9926)
- Visual Studio 社区 2013
尝试调试:- [AT&T] Lumia 635(适用于手机的 Windows 10 Technical Preview 版本 9941 w/ Lumia Cyan)
- [AT&T] Lumia 1520(带有 Lumia Denim 和 PfD 的 Windows Phone 8.1)
- [解锁] BLU Win Jr(Windows Phone 8.1 with PfD)
- [Verizon] Lumia 图标(带有 Lumia Denim 和 PfD 的 Windows Phone 8.1)
我试图让定位服务在我的应用中运行。以前,我让 Visual Studio 抛出错误。这是一个带有消息“Use of undefined keyword value 1 for event TaskScheduled in async”的ArgumentException。谷歌搜索没有找到任何解决方案。
代码如下:
Geolocator Locator = new Geolocator();
Geoposition Position = await Locator.GetGeopositionAsync();
Geocoordinate Coordinate = Position.Coordinate;
当我可以得到要抛出的错误时,异常是在上面示例中的第 2 行或第 3 行抛出的。 我简化了原始代码以尝试修复它,但这是原始代码:
Geolocator Locator = new Geolocator();
Geocoordinate Coordinate = (await Locator.GetGeopositionAsync()).Position.Coordinate;
整个应用程序在调试时工作正常,但在其他情况下几乎瞬间崩溃。
这是一个Windows 8.1 Universal项目,专注于手机项目。
提前致谢
编辑:根据要求,这是完整的方法:
private static bool CheckConnection()
{
ConnectionProfile connections = NetworkInformation.GetInternetConnectionProfile();
bool internet = connections != null && connections.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess;
return internet;
}
public static async Task<double> GetTemperature(bool Force)
{
if (CheckConnection() || Force)
{
Geolocator Locator = new Geolocator();
await Task.Yield(); //Error occurs here
Geoposition Position = await Locator.GetGeopositionAsync();
Geocoordinate Coordinate = Position.Coordinate;
HttpClient Client = new HttpClient();
double Temperature;
Uri u = new Uri(string.Format("http://api.worldweatheronline.com/free/v1/weather.ashx?q={0},{1}&format=xml&num_of_days=1&date=today&cc=yes&key={2}",
Coordinate.Point.Position.Latitude,
Coordinate.Point.Position.Longitude,
"API KEY"),
UriKind.Absolute);
string Raw = await Client.GetStringAsync(u);
XElement main = XElement.Parse(Raw), current_condition, temp_c;
current_condition = main.Element("current_condition");
temp_c = current_condition.Element("temp_C");
Temperature = Convert.ToDouble(temp_c.Value);
switch (Memory.TempUnit)
{
case 0:
Temperature = Convertions.Temperature.CelsiusToFahrenheit(Temperature);
break;
case 2:
Temperature = Convertions.Temperature.CelsiusToKelvin(Temperature);
break;
}
return Temperature;
}
else
{
throw new InvalidOperationException("Cannot connect to the weather server.");
}
}
编辑 2: 我已经 asked for help on Twitter 和 received a reply 要求进行复制项目。我重新创建了原始应用程序的主要部分,但我无法得到错误。但是,您可能会出现错误so here's the project。
编辑 3:如果它有帮助,这里是异常详细信息:
System.ArgumentException occurred
_HResult=-2147024809
_message=Use of undefined keyword value 1 for event TaskScheduled.
HResult=-2147024809
IsTransient=false
Message=Use of undefined keyword value 1 for event TaskScheduled.
Source=mscorlib
StackTrace:
at System.Diagnostics.Tracing.ManifestBuilder.GetKeywords(UInt64 keywords, String eventName)
InnerException:
【问题讨论】:
-
@Noseratio 我已经在你链接的两个线程中尽我所能。另外,我尝试了您提供的解决方法。它在调试时仍然可以完美运行,但在不调试时仍然会崩溃。感谢您的回复
-
显然,这是一个 WP/WRT 错误,我会在connect.microsoft.com 报告它。顺便说一句,也试试这个:
await Task.Yeild(); Geocoordinate Coordinate = (await Locator.GetGeopositionAsync()).Position.Coordinate;在Task.Yeild()或await Locator.GetGeopositionAsync()之后崩溃吗? -
顺便说一句,我很感激你在你这个年纪处理这些复杂的事情,干得好:)
-
@Noseratio 我使用在
Task.Yield()和await Locator.GetGeopositionAsync()之后弹出的 MessageDialogs 进行设置。我的猜测是它在Task.Yield()崩溃,因为没有弹出对话框。同样,它只在不调试的情况下执行此操作。谢谢顺便说一句,哈哈:P
标签: c# geolocation async-await win-universal-app argumentexception