C# 如何支持Windows 8&;8.1地理定位API
我已经在我的程序中添加了地理定位支持,但我有一个问题,我不知道如何处理。我用的是VS2012 我已经在我的解决方案中添加了一个DLL,它可以进行所有的地理定位调用。我在.csproj文件中将支持的Windows版本设置为8.1。DLL项目也使用.NET 4.5,而解决方案中的其他项目使用.NET 4.0 我在检索位置的方法中有以下代码:C# 如何支持Windows 8&;8.1地理定位API,c#,.net,windows-store-apps,C#,.net,Windows Store Apps,我已经在我的程序中添加了地理定位支持,但我有一个问题,我不知道如何处理。我用的是VS2012 我已经在我的解决方案中添加了一个DLL,它可以进行所有的地理定位调用。我在.csproj文件中将支持的Windows版本设置为8.1。DLL项目也使用.NET 4.5,而解决方案中的其他项目使用.NET 4.0 我在检索位置的方法中有以下代码: protected override void Initialize() { // Initialization code here Pars
protected override void Initialize() {
// Initialization code here
ParseGeoposition(); // Exception is thrown on this line
}
private void ParseGeoPositin() {
// . . .
bool done = false;
while ( !done )
try {
if ( UseCoordinate ) {
newPosition.Position.Latitude = positionRecord.Coordinate.Latitude;
newPosition.Position.Longitude = positionRecord.Coordinate.Longitude;
} else {
newPosition.Position.Latitude = positionRecord.Coordinate.Point.Position.Latitude;
newPosition.Position.Longitude = positionRecord.Coordinate.Point.Position.Longitude;
}
done = true;
} catch ( MissingMethodException ) {
if ( !UseCoordinate ) {
throw new NotSupportedException( "This machine does not support the Windows Geolocation API." );
}
UseCoordinate = false;
}
// . . .
}
usecordinate
标志开始显示true
。代码应切换到使用点
属性(如果坐标
属性不受支持,则将使用坐标
设置为假
)
该代码在我的Windows 8.1开发计算机上运行良好。但是,当我在Windows 8.0计算机上安装该代码时,在调用包含此代码的方法的行上引发了一个MissingMethodException
。这就好像try不存在一样。引发的异常为:
System.MissingMethodException:找不到方法:“Windows.Devices.Geolocation.Geopoint Windows.Devices.Geolocation.Geocoordinate.get_Point()”
显然,我的方法是有缺陷的。我不希望看到Windows版本号,因为我的经理不希望我这样做。正确的编码方法是什么?当方法由JITer编译时,会发生异常-您需要使用8.0兼容的地理位置API调用(和朋友),或对使用8.1特定指令和相应的
try…catch
块的方法进行保护调用。当JITer编译方法时,会发生异常-您需要使用8.0兼容的地理位置API调用(和friends),或对使用8.1特定指令和相应的try…catch
块的方法进行保护调用。当JITer编译方法时,会发生异常-您需要使用8.0兼容的地理位置API调用(和friends),或对使用8.1特定指令和相应的try…catch
块的方法进行保护调用。当JITer编译方法时,会发生异常-您需要使用8.0兼容的地理位置API调用(和friends),或对使用8.1特定指令的方法进行保护调用,这些方法使用适当的try…catch
块。使用Rowland提供的信息,我能够实现这一点。Windows 8和Windows 8.1 API之间的区别在于GeoCoordination
类的坐标
属性Coordination
属性是一个GeoCoordination
对象,它具有纬度
、经度
和高度
属性。在Windows 8.1中,这些属性仍然存在,但被标记为过时。Windows 8.1中的GeoCoordination
类有一个名为Point
类型的新属性地理点。此对象具有名为位置
的属性,类型为基本地理位置
。此对象具有纬度
、经度
和高度
属性
类中有一个名为positionRecord
的私有实例变量:
private Geoposition positionRecord = null;
这是通过调用Initialize
方法中的GetPositionAsync
来设置的,然后每当Geolocator
引发其PositionChanged
事件时,我就在代码中创建了两组函数:
private double GetAltitudeFromCoordinate() {
return positionRecord.Coordinate.Altitude == null ? 0.0 : positionRecord.Coordinate.Altitude.Value;
}
private double GetAltitudeFromPoint() {
return positionRecord.Coordinate.Point.Position.Altitude;
}
private double GetLatitudeFromCoordinate() {
return positionRecord.Coordinate.Latitude;
}
private double GetLatitudeFromPoint() {
return positionRecord.Coordinate.Point.Position.Latitude;
}
private double GetLongitudeFromCoordinate() {
return positionRecord.Coordinate.Longitude;
}
private double GetLongitudeFromPoint() {
return positionRecord.Coordinate.Point.Position.Longitude;
}
接下来,我创建了两个用于解析纬度和经度的方法:
private void ParseLatLonFromCoordinate( GpsInformation newPosition ) {
// We assume this is a Windows 8.0 machine that doesn't have the Point property.
newPosition.Position.Latitude = positionRecord.Coordinate.Latitude;
newPosition.Position.Longitude = positionRecord.Coordinate.Longitude;
}
private void ParseLatLonFromPoint( GpsInformation newPosition ) {
// The system must not support the Coordinate.Latitude & Longitude properties any more.
newPosition.Position.Latitude = positionRecord.Coordinate.Point.Position.Latitude;
newPosition.Position.Longitude = positionRecord.Coordinate.Point.Position.Longitude;
}
我在类中添加了一个名为usecordinations
的bool
属性。在程序的初始化代码中,我添加了此行来初始化标志:
UseCoordinate = positionRecord.Coordinate.GetType().GetProperty( "Point" ) == null;
最后,我添加了一个名为ParsePosition
的方法,该方法在上述代码之后调用,并且每当geological
引发PositionChanged
事件时调用:
private void ParsePosition() {
GpsInformation newPosition = new GpsInformation();
newPosition.TimeStamp = DateTime.Now;
try {
if ( UseCoordinate )
ParseLatLonFromCoordinate( newPosition );
else
ParseLatLonFromPoint( newPosition );
} catch ( MissingMethodException ) {
// The Geolocation API must not be supported.
throw new NotSupportedException( "This machine does not support the Windows Geolocation API." );
}
// . . .
}
这在Windows 8和Windows 8.1上都有效,不会引发
MissingMethodException.
使用Rowland提供的信息,我能够实现这一点。Windows 8和Windows 8.1 API之间的区别在于GeoCoordination
类的坐标属性。在Windows 8中,坐标属性y是一个地理坐标
对象,它具有纬度
、经度
和高度
属性。在Windows 8.1中,这些属性仍然存在,但被标记为过时。Windows 8.1中的地理坐标
类有一个名为点
的新属性,类型为地理点
。这有一个称为位置
的属性,类型为基本地理位置
。此对象具有纬度
、经度
和高度
属性
类中有一个名为positionRecord
的私有实例变量:
private Geoposition positionRecord = null;
这是通过调用Initialize
方法中的GetPositionAsync
来设置的,然后每当Geolocator
引发其PositionChanged
事件时,我就在代码中创建了两组函数:
private double GetAltitudeFromCoordinate() {
return positionRecord.Coordinate.Altitude == null ? 0.0 : positionRecord.Coordinate.Altitude.Value;
}
private double GetAltitudeFromPoint() {
return positionRecord.Coordinate.Point.Position.Altitude;
}
private double GetLatitudeFromCoordinate() {
return positionRecord.Coordinate.Latitude;
}
private double GetLatitudeFromPoint() {
return positionRecord.Coordinate.Point.Position.Latitude;
}
private double GetLongitudeFromCoordinate() {
return positionRecord.Coordinate.Longitude;
}
private double GetLongitudeFromPoint() {
return positionRecord.Coordinate.Point.Position.Longitude;
}
接下来,我创建了两个用于解析纬度和经度的方法:
private void ParseLatLonFromCoordinate( GpsInformation newPosition ) {
// We assume this is a Windows 8.0 machine that doesn't have the Point property.
newPosition.Position.Latitude = positionRecord.Coordinate.Latitude;
newPosition.Position.Longitude = positionRecord.Coordinate.Longitude;
}
private void ParseLatLonFromPoint( GpsInformation newPosition ) {
// The system must not support the Coordinate.Latitude & Longitude properties any more.
newPosition.Position.Latitude = positionRecord.Coordinate.Point.Position.Latitude;
newPosition.Position.Longitude = positionRecord.Coordinate.Point.Position.Longitude;
}
我在类中添加了一个名为usecordinations
的bool
属性。在程序的初始化代码中,我添加了此行来初始化标志:
UseCoordinate = positionRecord.Coordinate.GetType().GetProperty( "Point" ) == null;
最后,我添加了一个名为ParsePosition
的方法,该方法在上述代码之后调用,并且每当geological
引发PositionChanged
事件时调用:
private void ParsePosition() {
GpsInformation newPosition = new GpsInformation();
newPosition.TimeStamp = DateTime.Now;
try {
if ( UseCoordinate )
ParseLatLonFromCoordinate( newPosition );
else
ParseLatLonFromPoint( newPosition );
} catch ( MissingMethodException ) {
// The Geolocation API must not be supported.
throw new NotSupportedException( "This machine does not support the Windows Geolocation API." );
}
// . . .
}
这在Windows 8和8.1上都有效,不会引发MissingMethodExceptions.
使用Rowland提供的信息,我能够实现这一点。Windows 8和Windows 8.1 API之间的区别在于地理坐标