Posts Tagged ‘Windows Service’
WP7 accessing a Windows Service DataService
This application took 2 minutes to write…
Now that I have demonstrated how Silverlight can access a DataService that is hosted by a WindowsService the final example is to show that it will work on a WP7.
I created a new Windows Phone application and added a Button and Textbox.
I added a Service Reference to the DataService to the Solution.
Finally I pretty much copied the code from the Silverlight application in the previous post.
using WPService.DataService; using System.Xml.Linq; namespace WPService { public partial class MainPage : PhoneApplicationPage { DataServiceClient dataService = null; // Constructor public MainPage() { InitializeComponent(); dataService = new DataServiceClient(); } private void button1_Click(object sender, RoutedEventArgs e) { try { dataService.GetDateTimeCompleted += new EventHandler<GetDateTimeCompletedEventArgs>(dataService_GetDateTimeCompleted); dataService.GetDateTimeAsync(); } catch (Exception ex) { textBlock1.Text = string.Format("Error occurred: {0}", ex.Message); } } void dataService_GetDateTimeCompleted(object sender, DataService.GetDateTimeCompletedEventArgs e) { try { XElement xResult = (XElement)e.Result; string date = xResult.Element("Date").Value; string time = xResult.Element("Time").Value; textBlock1.Text = string.Format("The date is: {0}\nThe time is: {1}", date, time); } catch (Exception ex) { textBlock1.Text = string.Format("Error occurred: {0}", ex.Message); } } } }
I clicked the Button!
Says it all!