我们有一个 Windows Phone 7 应用程序,它使用一组 3 种使用 Reactive Extensions 的服务方法,定义如下:
public static class ServiceClient
{
public static IObservable<string> LookupImage(byte[] image) {...}
public static IObservable<XDocument> GetDefinition(string id) {...}
public static IObservable<Dictionary<string, byte[]>> GetFiles(string id, string[] fileNames) {...}
}
我们需要 WP7 应用程序在上述客户端中不断调用LookupImage
(每次使用不同的byte[] image
数据集),直到返回IObservable<string>
非空。获得Observable
字符串后,我们必须调用GetDefinition
和GetFiles
方法(按此顺序)。
调用LookupImage
应该在服务响应返回时发生,而不是由计时器控制,因为它会根据网络连接速度而变化,我们需要能够发送尽可能多的这些。
对于上述问题的解决方案,我将不胜感激。作为开始,我有以下内容
private void RunLookupAndRenderLogic()
{
byte[] imageBytes = GetImageBytes();
// There are some cases where the image was not 'interesting' enough in which case GetImageBytes() returns null
if (pictureBytes != null)
{
// Where we have image data, send this to LookupImage service method
var markerLookup = ServiceClient.LookupImage(imageBytes);
markerLookup.Subscribe(id =>
{
// If the id is empty, we need to call this again.
if (String.IsNullOrEmpty(id))
{
???
}
// If we have an id, call GetDefinition and GetFiles methods of the service. No further calls to LookupImage should take place.
RenderLogic(id);
});
}
else
// If no interesting image was returned, try again
RunRecognitionAndRenderLogic();
}