示例#1
0
        private async void LoadButton_Click(object sender, RoutedEventArgs e)
        {
            if (DatePicker.SelectedDate > DateTime.Today)
            {
                MessageBox.Show("Нету данных по выбранно дате!");
                return;
            }

            Progress.Visibility = Visibility.Visible;
            string selectedDate = DatePicker.SelectedDate.Value.ToString("yyyy-MM-dd");

            NasaImageResponse file = await LoadImage(selectedDate);

            if (file.MediaType != "video")
            {
                videoHost.Visibility = Visibility.Hidden;
                imageHost.Visibility = Visibility.Visible;

                ImageText.Text   = file.Title;
                imageHost.Source = new BitmapImage(new Uri(file.Hdurl));
            }
            else
            {
                imageHost.Visibility = Visibility.Hidden;
                videoHost.Visibility = Visibility.Visible;

                videoHost.Address = file.Url;
            }

            Progress.Visibility = Visibility.Hidden;
        }
示例#2
0
        private Task <NasaImageResponse> LoadImage(string selectedDate)
        {
            return(Task.Run(() =>
            {
                WebClient client = new WebClient();

                string address = "https://api.nasa.gov/planetary/apod?" +
                                 "date=" + selectedDate +
                                 "&hd=true&api_key=7us9cTuYhzECs704EuxcMcNH0K1eTo40jRNiNjHN";

                using (Stream stream = client.OpenRead(address))
                {
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        string data = reader.ReadToEnd();
                        NasaImageResponse response = JsonConvert.DeserializeObject <NasaImageResponse>(data);

                        return response;
                    }
                }
            }));
        }