我在我的javascript UWP应用中创建了一个Windows Runtime Component (C#),但是无法让它返回我想要的结果。它构建正常,但在调试时,返回null,而不是Geoposition对象。
由于需要在我的javascript应用程序中调用以下函数,看起来我can't use the async keyword,但我可以在包装器中执行:
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#中。
答案 0 :(得分:1)
这是真的,你不能使用等待。但你可以使用承诺。您必须使用Windows运行时数据类型来实现JS / C#互操作性才能使其正常工作。听起来比这更复杂:
更改Windows运行时组件中的C#代码,如下所示:
public static IAsyncOperation <Geoposition> GetGeopoisitionAsync()
{
Geolocator g = new Geolocator();
return g.GetGeopositionAsync() as IAsyncOperation<Geoposition> ;
}
或者如果您希望在将C#中的地理位置传递到Javascript之前访问它,请执行以下操作:
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 App调用此方法。确保设置位置兼容性并从JS App引用组件项目。
基于示例代码的调用 - 如下所示:
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
答案 1 :(得分:0)
您必须确保使用您的清单(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>