Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.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# Bing映射REST服务工具包-访问委托之外的值_C#_Callback_Delegates_Bing Maps - Fatal编程技术网

C# Bing映射REST服务工具包-访问委托之外的值

C# Bing映射REST服务工具包-访问委托之外的值,c#,callback,delegates,bing-maps,C#,Callback,Delegates,Bing Maps,Bing地图REST服务工具包的示例代码使用委托获取响应,然后从委托方法中输出消息。但是,它没有演示如何从GetResponse调用外部访问响应。我不知道如何从该委托返回值。换句话说,假设我想在Console.ReadLine()行前面使用longitude变量的值如何访问该范围内的变量 using System; using System.Collections.Generic; using System.Linq; using System.Text;

Bing地图REST服务工具包的示例代码使用委托获取响应,然后从委托方法中输出消息。但是,它没有演示如何从GetResponse调用外部访问响应。我不知道如何从该委托返回值。换句话说,假设我想在
Console.ReadLine()行前面使用
longitude
变量的值如何访问该范围内的变量

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using BingMapsRESTToolkit;
    using System.Configuration;
    using System.Net;
    using System.Runtime.Serialization.Json;

    namespace RESTToolkitTestConsoleApp
    {

        class Program
        {

            static private string _ApiKey = System.Configuration.ConfigurationManager.AppSettings.Get("BingMapsKey");

            static void Main(string[] args)
            {
                string query = "1 Microsoft Way, Redmond, WA";

                Uri geocodeRequest = new Uri(string.Format("http://dev.virtualearth.net/REST/v1/Locations?q={0}&key={1}", query, _ApiKey));

                GetResponse(geocodeRequest, (x) =>
                {
                    Console.WriteLine(x.ResourceSets[0].Resources.Length + " result(s) found.");
                    decimal latitude = (decimal)((Location)x.ResourceSets[0].Resources[0]).Point.Coordinates[0];
                    decimal longitude = (decimal)((Location)x.ResourceSets[0].Resources[0]).Point.Coordinates[1];
                    Console.WriteLine("Latitude: " + latitude);
                    Console.WriteLine("Longitude: " + longitude);
                });
                Console.ReadLine();
            }

            private static void GetResponse(Uri uri, Action<Response> callback)
            {
                WebClient wc = new WebClient();
                wc.OpenReadCompleted += (o, a) =>
                {
                    if (callback != null)
                    {
                        // Requires a reference to System.Runtime.Serialization
                        DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Response));
                        callback(ser.ReadObject(a.Result) as Response);
                    }
                };
                wc.OpenReadAsync(uri);
            }
        }
    }
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用BingMapsRESTToolkit;
使用系统配置;
Net系统;
使用System.Runtime.Serialization.Json;
命名空间RESTToolkitTestConsoleApp
{
班级计划
{
静态私有字符串_ApiKey=System.Configuration.ConfigurationManager.AppSettings.Get(“BingMapsKey”);
静态void Main(字符串[]参数)
{
string query=“1华盛顿州雷德蒙市微软路”;
Uri geocodeRequest=新Uri(string.Format(“http://dev.virtualearth.net/REST/v1/Locations?q={0}&key={1},查询,_ApiKey));
GetResponse(geocodeRequest,(x)=>
{
Console.WriteLine(x.ResourceSets[0].Resources.Length+“找到结果”);
十进制纬度=(十进制)((位置)x.ResourceSets[0]。资源[0])。点。坐标[0];
十进制经度=(十进制)((位置)x.ResourceSets[0]。资源[0])。点。坐标[1];
控制台写入线(“纬度:+纬度);
Console.WriteLine(“经度:+经度”);
});
Console.ReadLine();
}
私有静态void GetResponse(Uri、操作回调)
{
WebClient wc=新的WebClient();
wc.OpenReadCompleted+=(o,a)=>
{
if(回调!=null)
{
//需要对System.Runtime.Serialization的引用
DataContractJsonSerializer ser=新的DataContractJsonSerializer(类型(响应));
回调(ser.ReadObject(a.Result)作为响应);
}
};
OpenReadAsync(uri);
}
}
}
对于所提供的示例,可用于控制流,特别是等待异步完成,如下所示:

class Program
{
    private static readonly string ApiKey = System.Configuration.ConfigurationManager.AppSettings.Get("BingMapsKey");
    private static readonly AutoResetEvent StopWaitHandle = new AutoResetEvent(false);

    public static void Main()
    {
        var query = "1 Microsoft Way, Redmond, WA";
        BingMapsRESTToolkit.Location result = null;

        Uri geocodeRequest = new Uri(string.Format("http://dev.virtualearth.net/REST/v1/Locations?q={0}&key={1}",
            query, ApiKey));
        GetResponse(geocodeRequest, (x) =>
        {
           if (response != null &&
              response.ResourceSets != null &&
              response.ResourceSets.Length > 0 &&
              response.ResourceSets[0].Resources != null &&
              response.ResourceSets[0].Resources.Length > 0)
           {
               result = response.ResourceSets[0].Resources[0] as BingMapsRESTToolkit.Location;
            }
        });
        StopWaitHandle.WaitOne(); //wait for callback
        Console.WriteLine(result.Point); //<-access result 
        Console.ReadLine();

    }

    private static void GetResponse(Uri uri, Action<Response> callback)
    {
        var wc = new WebClient();
        wc.OpenReadCompleted += (o, a) =>
        {
            if (callback != null)
            {
                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Response));
                callback(ser.ReadObject(a.Result) as Response);
            }
            StopWaitHandle.Set(); //signal the wait handle
        };
        wc.OpenReadAsync(uri);
    }

}
在哪里

公共静态异步任务执行程序(字符串queryText)
{
//创建一个请求。
var request=new GeocodeRequest()
{
Query=queryText,
MaxResults=1,
BingMapsKey=ApiKey
};
//使用ServiceManager处理请求。
var response=wait request.Execute();
if(响应!=null&&
response.ResourceSets!=null&&
response.ResourceSets.Length>0&&
响应。资源集[0]。资源!=null&&
response.ResourceSets[0].Resources.Length>0)
{
返回response.ResourceSets[0]。资源[0]作为BingMapsRESTToolkit.Location;
}
返回null;
}
public static void Main()
{
    var task = ExecuteQuery("1 Microsoft Way, Redmond, WA");
    task.Wait();
    Console.WriteLine(task.Result);
    Console.ReadLine();
}
public static async Task<BingMapsRESTToolkit.Location> ExecuteQuery(string queryText)
{
    //Create a request.
    var request = new GeocodeRequest()
        {
            Query = queryText,
            MaxResults = 1,
            BingMapsKey = ApiKey
    };
    //Process the request by using the ServiceManager.
    var response = await request.Execute();
    if (response != null &&
            response.ResourceSets != null &&
            response.ResourceSets.Length > 0 &&
            response.ResourceSets[0].Resources != null &&
            response.ResourceSets[0].Resources.Length > 0)
    {
         return response.ResourceSets[0].Resources[0] as BingMapsRESTToolkit.Location;
    }
    return null;
}