如何返回Geoposition而不是IAsyncOperation<;地理位置>;或者任务<;地理位置>
本文关键字:lt 地理位置 gt IAsyncOperation 任务 或者 何返回 返回 Geoposition | 更新日期: 2023-09-27 18:30:05
我在我的javascript UWP应用程序中创建了一个Windows运行时组件(C#),但很难让它返回我想要的结果。它构建良好,但在调试时,返回null,而不是Geoposition对象。
由于下面的函数需要在我的javascript应用程序中调用,所以我似乎不能使用async关键字,但我可以在包装器中完成:
public static Geoposition GetGeolocation()
{
Geolocator _geolocator = new Geolocator();
MapLocation mapInfo = null;
_geolocator.DesiredAccuracyInMeters = 5;
try
{
Geoposition currentPosition = GetGeopoisitionWrapper(_geolocator).Result;
// Geoposition currentPosition = GetGeopoisitionWrapper(_geolocator).AsAsyncOperation().GetResults();
//other stuff needing currentPosition, and another async function call
return currentPosition;
}
catch (Exception ex)
{
return null;
}
}
包装:
private static async Task<Geoposition> GetGeopositionWrapper(Geolocator g)
{
Geoposition currentPosition = await g.GetGeopositionAsync(); // get the raw geoposition data
return currentPosition;
}
Javascript函数调用:
function getLocationForMap() {
Windows.Devices.Geolocation.Geolocator.requestAccessAsync().done(
function (accessStatus) {
switch (accessStatus) {
case Windows.Devices.Geolocation.GeolocationAccessStatus.allowed:
var result = SampleComponent.Example.getGeolocation();
break;
case Windows.Devices.Geolocation.GeolocationAccessStatus.denied:
WinJS.log && WinJS.log("Access to location is denied.", "sample", "error");
break;
case Windows.Devices.Geolocation.GeolocationAccessStatus.unspecified:
WinJS.log && WinJS.log("Unspecified error!", "sample", "error");
break;
}
},
function (err) {
WinJS.log && WinJS.log(err, "sample", "error");
});
}
我之所以需要在C#中完成这一部分,是因为我需要的函数调用之一(可能开发人员忘记包含它)从javascript API中丢失了,但存在于C#中。
的确,您不能使用await。但你可以用承诺来工作。为了实现JS/C#的互操作性,您必须使用Windows运行时数据类型。这听起来比实际情况更复杂:
在windows运行时组件中更改C#代码,如下所示:
public static IAsyncOperation <Geoposition> GetGeopoisitionAsync()
{
Geolocator g = new Geolocator();
return g.GetGeopositionAsync() as IAsyncOperation<Geoposition> ;
}
或者,如果你想在将其传递给Javascript之前访问C#中的地理位置:
public IAsyncOperation<Geoposition> GetPositionAsync2()
{
return Task.Run<Geoposition>(async () => {
Geolocator g = new Geolocator();
var pos = await g.GetGeopositionAsync();
// Do something with pos here..
return pos;
}).AsAsyncOperation();
这个方法可以直接从你的JS应用程序中调用。确保您设置了位置兼容性,并从JS应用程序中引用组件项目。
基于示例代码的调用如下所示:
function getLocationForMap() {
Windows.Devices.Geolocation.Geolocator.requestAccessAsync().done(
function (accessStatus) {
switch (accessStatus) {
case Windows.Devices.Geolocation.GeolocationAccessStatus.allowed:
// call the WinRT Component
var rc = RuntimeComponent1.Class1;
// use promises to wait for completion
rc.getGeopoisitionAsync().then(function (res) {
// access the props of the result
var m = new Windows.UI.Popups.MessageDialog(res.coordinate.latitude);
// show latitude in message box
m.showAsync();
});
break;
case Windows.Devices.Geolocation.GeolocationAccessStatus.denied:
WinJS.log && WinJS.log("Access to location is denied.", "sample", "error");
break;
case Windows.Devices.Geolocation.GeolocationAccessStatus.unspecified:
WinJS.log && WinJS.log("Unspecified error!", "sample", "error");
break;
}
},
function (err) {
WinJS.log && WinJS.log(err, "sample", "error");
});
}
在我的机器上工作:-)请在此处查看基于代码的完整工作示例:https://github.com/DanielMeixner/so/tree/master/JSAppGeolocation
您必须确保Location Capability是使用清单(Package.appxmanifest文件)设置的:
<Package
xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
IgnorableNamespaces="uap mp">
// You should have this
<Capabilities>
<DeviceCapability Name="location"/>
</Capabilities>
</Package>