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
13 namespace WEBSWAPP.SilverLightUI
14 {
15 public partial class Page : UserControl
16 {
17 public Page()
18 {
19 InitializeComponent();
20
21 }
22 private ServiceReference1.LocationServiceSoapClient ws = null;
23 private void UserControl_Loaded(object sender, RoutedEventArgs e)
24 {
25 //wire up all of the events handlers
26 ws = new WEBSWAPP.SilverLightUI.ServiceReference1.LocationServiceSoapClient();
27 ws.GetSuggestionsCompleted +=
28 new EventHandler<WEBSWAPP.SilverLightUI.ServiceReference1.GetSuggestionsCompletedEventArgs>(ws_GetSuggestionsCompleted);
29 ws.GetCountriesCompleted +=
30 new EventHandler<WEBSWAPP.SilverLightUI.ServiceReference1.GetCountriesCompletedEventArgs>(ws_GetCountriesCompleted);
31 ws.GetCitiesCompleted +=
32 new EventHandler<WEBSWAPP.SilverLightUI.ServiceReference1.GetCitiesCompletedEventArgs>(ws_GetCitiesCompleted);
33 ws.GetCapitalCitiesCompleted +=
34 new EventHandler<WEBSWAPP.SilverLightUI.ServiceReference1.GetCapitalCitiesCompletedEventArgs>(ws_GetCapitalCitiesCompleted);
35 ws.GetCityInfoCompleted +=
36 new EventHandler<WEBSWAPP.SilverLightUI.ServiceReference1.GetCityInfoCompletedEventArgs>(ws_GetCityInfoCompleted);
37 ws.GetProvinceInfoCompleted +=
38 new EventHandler<WEBSWAPP.SilverLightUI.ServiceReference1.GetProvinceInfoCompletedEventArgs>(ws_GetProvinceInfoCompleted);
39 ws.GetCountryInfoCompleted +=
40 new EventHandler<WEBSWAPP.SilverLightUI.ServiceReference1.GetCountryInfoCompletedEventArgs>(ws_GetCountryInfoCompleted);
41
42 ws.GetCountriesAsync();
43 }
44
45 void ws_GetProvinceInfoCompleted(object sender, WEBSWAPP.SilverLightUI.ServiceReference1.GetProvinceInfoCompletedEventArgs e)
46 {
47 if (e.Error != null)
48 {
49 tbStatus.Text = e.Error.Message + " " + e.Error.InnerException.ToString();
50 tbStatus.Visibility = Visibility.Visible;
51 return;
52 }
53 lbProvinceInfo.ItemsSource = e.Result;
54 }
55
56 void ws_GetCountryInfoCompleted(object sender, WEBSWAPP.SilverLightUI.ServiceReference1.GetCountryInfoCompletedEventArgs e)
57 {
58 if (e.Error != null)
59 {
60 tbStatus.Text = e.Error.Message + " " + e.Error.InnerException.ToString();
61 tbStatus.Visibility = Visibility.Visible;
62 return;
63 }
64 lbCountryInfo.ItemsSource = e.Result;
65 }
66
67 void ws_GetCityInfoCompleted(object sender, WEBSWAPP.SilverLightUI.ServiceReference1.GetCityInfoCompletedEventArgs e)
68 {
69 if (e.Error != null)
70 {
71 tbStatus.Text = e.Error.Message + " " + e.Error.InnerException.ToString();
72 tbStatus.Visibility = Visibility.Visible;
73 return;
74 }
75 lbCityInfo.ItemsSource = e.Result;
76
77 }
78
79 void ws_GetCapitalCitiesCompleted(object sender, WEBSWAPP.SilverLightUI.ServiceReference1.GetCapitalCitiesCompletedEventArgs e)
80 {
81 if (e.Error != null)
82 {
83 tbStatus.Text = e.Error.Message + " " + e.Error.InnerException.ToString();
84 tbStatus.Visibility = Visibility.Visible;
85 return;
86 }
87 lbLocations.ItemsSource = null;
88 lbLocations.ItemsSource = e.Result;
89 }
90
91 void ws_GetCitiesCompleted(object sender, WEBSWAPP.SilverLightUI.ServiceReference1.GetCitiesCompletedEventArgs e)
92 {
93 if (e.Error != null)
94 {
95 tbStatus.Text = e.Error.Message + " " + e.Error.InnerException.ToString();
96 tbStatus.Visibility = Visibility.Visible;
97 return;
98 }
99 lbLocations.ItemsSource = null;
100 lbLocations.ItemsSource = e.Result;
101 }
102
103 void ws_GetCountriesCompleted(object sender, WEBSWAPP.SilverLightUI.ServiceReference1.GetCountriesCompletedEventArgs e)
104 {
105 if (e.Error != null)
106 {
107 tbStatus.Text = e.Error.Message + " " + e.Error.InnerException.ToString();
108 tbStatus.Visibility = Visibility.Visible;
109 return;
110 }
111
112 lbLocations.ItemsSource = null;
113 cbCountries.ItemsSource = e.Result;
114
115 }
116
117 void ws_GetSuggestionsCompleted(object sender, WEBSWAPP.SilverLightUI.ServiceReference1.GetSuggestionsCompletedEventArgs e)
118 {
119 if (e.Error != null)
120 {
121 tbStatus.Text = e.Error.Message + " " + e.Error.InnerException.ToString();
122 tbStatus.Visibility = Visibility.Visible;
123 return;
124 }
125 lbLocations.ItemsSource = null;
126 lbLocations.ItemsSource = e.Result;
127 }
128
129 private bool countryNameChanged;
130 private bool cityNameChanged;
131 private bool capitalCityChanged;
132
133 private void SelectRecord()
134 {
135 //this happens when the counry combobox selection is changed
136 if (cityNameChanged && capitalCityChanged)
137 {
138 if (string.IsNullOrEmpty(tbCity.Text)) //check box clicked
139 {
140 if ((cbCountries.SelectedItem as ServiceReference1.CountryInfo) == null)
141 ws.GetCapitalCitiesAsync(0,
142 chkCapital.IsChecked.HasValue ? chkCapital.IsChecked.Value : false);
143 else
144 ws.GetCapitalCitiesAsync((cbCountries.SelectedItem as ServiceReference1.CountryInfo).Id,
145 chkCapital.IsChecked.HasValue ? chkCapital.IsChecked.Value : false);
146 }
147
148 else //user typed a text in the city name textbox
149 {
150 if ((ServiceReference1.CountryInfo)cbCountries.SelectedItem == null)
151 {
152 ws.GetSuggestionsAsync(tbCity.Text, 0, 150);
153 }
154 else
155 {
156 ws.GetSuggestionsAsync(tbCity.Text, ((ServiceReference1.CountryInfo)cbCountries.SelectedItem).Id, 150);
157 }
158 }
159 }
160 else if (countryNameChanged)
161 {
162 ws.GetCapitalCitiesAsync(((ServiceReference1.CountryInfo)cbCountries.SelectedItem).Id,
163 chkCapital.IsChecked.HasValue ? chkCapital.IsChecked.Value : false);
164 }
165 }
166
167 private void tbCity_TextChanged(object sender, TextChangedEventArgs e)
168 {
169 cityNameChanged = true;
170 capitalCityChanged = true;
171 chkCapital.IsChecked = string.IsNullOrEmpty(tbCity.Text);
172 SelectRecord();
173 }
174
175 private void cbCountries_SelectionChanged(object sender, SelectionChangedEventArgs e)
176 {
177 if (e.AddedItems.Count != 0)
178 {
179 countryNameChanged = true;
180 SelectRecord();
181 }
182 }
183
184 private void CheckBox_Click(object sender, RoutedEventArgs e)
185 {
186 if (chkCapital.IsChecked.Value) tbCity.Text = string.Empty;
187 cityNameChanged = true;
188 capitalCityChanged = true;
189 SelectRecord();
190
191 }
192
193 private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
194 {
195 ServiceReference1.CityInfo selectedCity = null;
196 if (e.AddedItems.Count == 0)
197 selectedCity = e.RemovedItems[0] as ServiceReference1.CityInfo;
198 else
199 selectedCity = e.AddedItems[0] as ServiceReference1.CityInfo;
200
201
202 if (selectedCity != null)
203 {
204 tbCityName.Text = "Information for the City named: " + selectedCity.Name;
205 tbProvinceName.Text = "Information for the province/state named: " + selectedCity.Province;
206 tbCountryName.Text = "Information for the country named: " + selectedCity.Country;
207 int cityId = selectedCity.Id;
208 ws.GetCityInfoAsync(cityId);
209 ws.GetProvinceInfoAsync(cityId);
210 ws.GetCountryInfoAsync(cityId);
211 }
212 }
213 }
214 }