gpt4 book ai didi

silverlight-4.0 - 使用Rx对Bing map 中的地址进行地址解析

转载 作者:行者123 更新时间:2023-12-02 04:10:42 25 4
gpt4 key购买 nike

我正在学习对正在使用的Silverlight 4应用程序使用Rx扩展。我创建了一个示例应用程序来确定该过程,但无法获取任何结果。
这是主要代码:

    private IObservable<Location> GetGPSCoordinates(string Address1)
{
var gsc = new GeocodeServiceClient("BasicHttpBinding_IGeocodeService") as IGeocodeService;

Location returnLocation = new Location();
GeocodeResponse gcResp = new GeocodeResponse();

GeocodeRequest gcr = new GeocodeRequest();
gcr.Credentials = new Credentials();
gcr.Credentials.ApplicationId = APP_ID2;
gcr.Query = Address1;

var myFunc = Observable.FromAsyncPattern<GeocodeRequest, GeocodeResponse>(gsc.BeginGeocode, gsc.EndGeocode);
gcResp = myFunc(gcr) as GeocodeResponse;

if (gcResp.Results.Count > 0 && gcResp.Results[0].Locations.Count > 0)
{
returnLocation = gcResp.Results[0].Locations[0];
}
return returnLocation as IObservable<Location>;
}

gcResp返回为空。任何想法或建议将不胜感激。

最佳答案

您要订阅的可观察源是异步的,因此您不能在订阅后立即访问结果。您需要在订阅中访问结果。

更好的是,根本不用订阅,只需编写响应即可:

private IObservable<Location> GetGPSCoordinates(string Address1)
{
IGeocodeService gsc =
new GeocodeServiceClient("BasicHttpBinding_IGeocodeService");

Location returnLocation = new Location();
GeocodeResponse gcResp = new GeocodeResponse();

GeocodeRequest gcr = new GeocodeRequest();
gcr.Credentials = new Credentials();
gcr.Credentials.ApplicationId = APP_ID2;
gcr.Query = Address1;

var factory = Observable.FromAsyncPattern<GeocodeRequest, GeocodeResponse>(
gsc.BeginGeocode, gsc.EndGeocode);

return factory(gcr)
.Where(response => response.Results.Count > 0 &&
response.Results[0].Locations.Count > 0)
.Select(response => response.Results[0].Locations[0]);
}

如果只需要第一个有效值(地址的位置不太可能更改),则在 .Take(1)Where之间添加 Select

编辑:如果要专门处理找不到的地址,则可以返回结果并让使用者处理该地址,也可以返回Exception并在订阅时提供 OnError处理程序。如果您打算做后者,则可以使用 SelectMany:
return factory(gcr)
.SelectMany(response => (response.Results.Count > 0 &&
response.Results[0].Locations.Count > 0)
? Observable.Return(response.Results[0].Locations[0])
: Observable.Throw<Location>(new AddressNotFoundException())
);

关于silverlight-4.0 - 使用Rx对Bing map 中的地址进行地址解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5333487/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com