Sample code for WP7

Oct 16, 2011 at 6:56 AM

Hi

I am new to Flickr. I was looking fo some sample code for WP7 to get me started with

any help would be appreciated

Coordinator
Oct 16, 2011 at 6:06 PM

I've been working on an example app but its not done yet.

If you are new to Flickr then I would recommend looking through the Flickr API docs on the Flickr web site. When you know which method you want to call you can then call the same method in the Flickr .net library.

If you have a specific question regarding a particular method then feel free to ask.

Sam

Oct 19, 2011 at 2:08 PM

Hi

Thanks for your reply

I have started using the library now but having one typical problem

 public void GetImages(string query)
        {
            var flickr = new FlickrNet.Flickr("MyApikey");
            var photoSearchOptions = new PhotoSearchOptions { Tags = query, PerPage = 50,Page = 1};
            flickr.PhotosSearchAsync(photoSearchOptions, OnFlickrSearchCompleted);
        }
 private void OnFlickrSearchCompleted(FlickrResult<PhotoCollection> result)
        {
           

            if (result.Error ==null)
            {
                var imglist= result.Result.Select(photo => new ImageSearchResult {MedialUrl = photo.ThumbnailUrl, RefUrl = photo.WebUrl}).ToList();
                
                Messaging.RaiseSearchCompleted.Send(new SearchCompleted { Exception = null, SearchResults = imglist.ToList() });
                
            }
            else
            {
                Messaging.RaiseSearchCompleted.Send(new SearchCompleted { Exception = result.Error, SearchResults = null });
                return;
            }

          
        }
When this code gets executed i get the images but the method OnFlickrSearchCompleted method again gets called automatically when I binding my viewmodel with the search results and the next time i get error "Invalid cross thread Exception"
Can you help me with this
Thanks
Coordinator
Oct 19, 2011 at 3:37 PM

That error usually occurs when you try to update a UI element from a thread that is not the UI thread.

Use the Dispatcher pattern to perform the update:

Dispatcher.BeginInvoke( () => { UpdateControlHere(); } )

Sam

Oct 21, 2011 at 10:52 AM

Hi Sam

thanks for your reply.

But my application follows MVVM pattern and I don't access the controls via code behind. I have viewmodel where i set the properties. Below is the code of it. When the debugger comes to the first line it again goes back to callback of FlickSearchAsync method and then give Cross thread aceess error.

I have implemented Bing Search Image service using the same code and it works like a gem. I dont know what is wrong with Flickr library.

  private void OnSearchCompleted1(SearchCompleted searchCompleted)
        {
           
            IsBusy = false;
            if (searchCompleted.Exception == null)
            {
                
                if (searchCompleted.SearchResults != null)
                {
                    _currentPage = 0;
                    _allSearchResults = null;
                    _allSearchResults = searchCompleted.SearchResults;
                    ImageSearchResults=new List<ImageSearchResult>();
                    ProcessResults();
                }
                
            }
            else
            {
                MessageBox.Show(searchCompleted.Exception.Message); //TODO remove messagebox and implement IDialogService
            }

        }
Coordinator
Oct 21, 2011 at 10:58 AM

If you are updating your view model then it will be updating the UI, so you need to make sure you modifications to the view model are on the UI thread.

I don't know anything about Bing Search, so I can't really comment on it, or how it handles threading issues.