1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Net;
5: using System.Windows;
6: using System.Windows.Controls;
7: using System.Windows.Documents;
8: using System.Windows.Input;
9: using System.Windows.Media;
10: using System.Windows.Media.Animation;
11: using System.Windows.Shapes;
12: using Newtonsoft.Json.Linq;
13: using System.Threading;
14: using System.Windows.Threading;
15: using System.Collections.ObjectModel;
16:
17: namespace YtInstantSL
18: {
19: public partial class MainPage : UserControl
20: {
21: string MyDevKey = "YourDevKey";
22: IList<ydata> videos;
23: IList<ydata> videosThumb;
24: private bool isi;
25: DispatcherTimer dispatcherTimer = new DispatcherTimer();
26: public MainPage()
27: {
28: InitializeComponent();
29: isi = false;
30:
31: dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
32: dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
33:
34: }
35:
36: private void txtKeyword_GotFocus(object sender, RoutedEventArgs e)
37: {
38: if (txtKeyword.Text == "Please enter your keyword ....")
39: {
40: txtKeyword.Text = "";
41: }
42: }
43:
44: private void txtKeyword_TextChanged(object sender, TextChangedEventArgs e)
45: {
46:
47: wc_todo("http://gdata.youtube.com/feeds/api/videos?v=2&format=5&alt=jsonc&max-results=5&q=" + txtKeyword.Text + "&key=" + MyDevKey);
48: if (txtKeyword.Text.Length > 0)
49: {
50: dispatcherTimer.Start();
51: }
52:
53: }
54:
55: private void wc_todo(string Uri)
56: {
57: WebClient wc1 = new WebClient();
58: WebClient wc2 = new WebClient();
59: try
60: {
61: if (!(wc1.IsBusy))
62: {
63: wc1.DownloadStringAsync(new Uri(Uri, UriKind.Absolute));
64: wc1.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
65: }
66: else if (!(wc2.IsBusy))
67: {
68: wc2.DownloadStringAsync(new Uri(Uri, UriKind.Absolute));
69: wc2.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
70: }
71: }
72: catch (WebException ex)
73: {
74: MessageBox.Show(ex.Message);
75: }
76:
77: }
78:
79: public void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs c)
80: {
81: try
82: {
83: var o = JObject.Parse(c.Result);
84: videos = (from v in o["data"]["items"].Children() select new ydata { title = (string)v["title"], description = (string)v["description"], thumbnail = (string)v["thumbnail"]["sqDefault"], url = (string)v["content"]["5"] }).ToList<ydata>();
85:
86: if (videos.Count() > 0)
87: {
88: isi = true;
89: tbStatus.Text = txtKeyword.Text;
90: videosThumb =videos ;
91: lbOther.DataContext = videosThumb;
92: }
93: }
94: catch (Exception ex)
95: {
96: if (txtKeyword.Text != "")
97: {
98: tbStatus.Text = "Error : Video with this keyword not found.";
99: }
100: }
101: }
102: private void dispatcherTimer_Tick(object sender, EventArgs e)
103: {
104: if(isi == true){
105: try
106: {
107:
108: webBrowser1.Navigate(new Uri(videos[0].url,UriKind.Absolute));
109: dispatcherTimer.Stop();
110: }
111: catch (Exception ex)
112: {
113: MessageBox.Show(ex.Message);
114:
115: }
116: }
117: }
118:
119: private void lbOther_SelectionChanged(object sender, SelectionChangedEventArgs e)
120: {
121: webBrowser1.Navigate(new Uri(videos[lbOther.SelectedIndex].url));
122: }
123: }
124: }