示例#1
0
        }         // end SearchMethod

        /// <summary>
        /// Gets the selected image URL and sends it back to the FlickrImage property to display the image
        /// </summary>
        private void GetFlickrImage()
        {
            int i = FlickrList.IndexOf(SelectedImage);

            if (i <= FlickrList.Count)
            {
                FlickrImage = selectedImage.URL;
            }
        } // end GetFlickrImage
示例#2
0
        /// <summary>
        /// Method is called when the search button is selected
        /// Searches all images related to the tag inserted by user
        /// </summary>
        private async void SeachMethod()
        {
            if (flickrTask != null && flickrTask.Status != TaskStatus.RanToCompletion)
            {
                var result = MessageBox.Show("Cancel the current Flickr search?", "Are you sure?", MessageBoxButton.YesNo);
                if (result == MessageBoxResult.No)
                {
                    return;
                }
                else
                {
                    flickrClient.CancelAsync(); // cancel current search
                }
            } // end if
            else
            {
                var flickrURL = string.Format("https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key={0}&tags={1}&tag_mode=all&per_page=500&privacy_filter=1"
                                              , KEY, Tag.Replace(" ", ","));

                FlickrList.Clear(); // clear imagesListBox
                FlickrImage = null; // clear pictureBox
                x.Title     = "Loading...";
                FlickrList.Add(x);  // display Loading...

                try
                {
                    flickrTask = flickrClient.DownloadStringTaskAsync(flickrURL);
                    XDocument flickrXML    = XDocument.Parse(await flickrTask);
                    var       flickrPhotos =
                        (from photo in flickrXML.Descendants("photo")
                         let id = photo.Attribute("id").Value
                                  let title = photo.Attribute("title").Value
                                              let secret = photo.Attribute("secret").Value
                                                           let server = photo.Attribute("server").Value
                                                                        let farm = photo.Attribute("farm").Value
                                                                                   select new FlickrResults
                    {
                        Title = title,
                        URL = string.Format("https://farm{0}.staticflickr.com/{1}/{2}_{3}.jpg", farm, server, id, secret)
                    }).ToList();
                    if (flickrPhotos.Any())
                    {
                        FlickrList.Clear(); // clear imagesListBox
                        FlickrList = new ObservableCollection <FlickrResults>(flickrPhotos);
                    }
                    else // no matches were found
                    {
                        FlickrList.Clear(); // clear imagesListBox
                        x.Title = "No matches";
                        FlickrList.Add(x);
                    }
                } // end try
                catch (WebException)
                {
                    // check whether Task failed
                    if (flickrTask.Status == TaskStatus.Faulted)
                    {
                        MessageBox.Show("Unable to get results from Flickr");
                    }
                    FlickrList.Clear(); // clear imagesListBox
                    x.Title = "Error occurred";
                    FlickrList.Add(x);
                } // end catch
            }     // end else
        }         // end SearchMethod