Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何返回地理位置而不是IAsyncOperation<;地理位置>;或任务<;地理位置>;?_C#_Asynchronous_Windows Runtime - Fatal编程技术网

C# 如何返回地理位置而不是IAsyncOperation<;地理位置>;或任务<;地理位置>;?

C# 如何返回地理位置而不是IAsyncOperation<;地理位置>;或任务<;地理位置>;?,c#,asynchronous,windows-runtime,C#,Asynchronous,Windows Runtime,我已经在我的javascript UWP应用程序中创建了一个,但是我很难让它返回我想要的结果。它构建得很好,但在调试时,返回null,而不是Geoposition对象 因为下面的函数需要在我的javascript应用程序中调用,所以我可以在包装器中调用: public static Geoposition GetGeolocation() { Geolocator _geolocator = new Geolocator(); MapLocation m

我已经在我的javascript UWP应用程序中创建了一个,但是我很难让它返回我想要的结果。它构建得很好,但在调试时,返回null,而不是Geoposition对象

因为下面的函数需要在我的javascript应用程序中调用,所以我可以在包装器中调用:

 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;
    }

我之所以需要在C#中完成这一部分,是因为javascript API中缺少了我需要的一个函数调用(可能是开发人员忘记了包含它),但它存在于C#中

您必须确保使用清单(Package.appxmanifest文件)设置了定位功能


//你应该有这个

没错,您不能使用wait。但是你可以用承诺来工作。为了实现JS/C互操作性,您必须使用Windows运行时数据类型。这听起来比实际情况更复杂:

在windows运行时组件中更改C#代码,如下所示:

public static  IAsyncOperation <Geoposition> GetGeopoisitionAsync()
        {
            Geolocator g = new Geolocator();
            return g.GetGeopositionAsync() as IAsyncOperation<Geoposition> ;                        
        }
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();
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");
        });
}
在我的机器上工作:-)
在此处查看基于代码的完整工作示例:

为什么停在这里?方法名称也应该重命名
GetGeopoisitionWrapperAsync()
或其他什么。感谢您的回复-我已启用位置,并已在我的应用程序的javascript部分请求权限。我不能在main函数中使用async关键字,因为我需要在javascript应用程序中调用它。为了澄清,我编辑了我原来的帖子。我之所以需要在C#中完成这一部分,是因为javascript API中缺少了一个我需要的函数调用(可能是开发人员忘记了包含它),但C#中存在这个函数调用。您能告诉我缺少什么函数调用吗?我可以让地理定位小组知道。@RaymondChen:我正在尝试调用Windows.Services.Maps.MapLocationFinder.FindLocationStatasync(pointToReverseGeocode)。在MapLocationFinder级别,它返回未定义。这里的文档说明它应该在javascript中工作:我与maps团队进行了检查
Windows.Services.Maps.MapLocationFinder
旨在与XAML MapControl结合使用。如果您正在编写JS应用程序,他们建议您使用为JS设计的。感谢Daniel的回复!这是可行的,但是你知道如何在C代码中获取Geoposition对象吗?最终的计划是从中获取纬度和经度值,并在返回结果之前进行一些反向地理编码。我尝试过Geoposition currentPosition=g.GetGeopositionAsync().GetResults(),但它总是返回null.Hi!我调整了上面的样品。如果遵循上述代码,您可以访问该职位。我还更新了Github上的示例。我希望这对你有帮助。效果很好。非常感谢你,丹尼尔!!这很好用!我也遇到了同样的问题。谢谢
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();
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");
        });
}