{"id":63,"date":"2010-12-30T14:33:17","date_gmt":"2010-12-30T09:03:17","guid":{"rendered":"http:\/\/techtwaddle.net\/?p=63"},"modified":"2011-01-26T14:40:56","modified_gmt":"2011-01-26T09:10:56","slug":"windows-phone-7-building-a-simple-dictionary-web-client","status":"publish","type":"post","link":"https:\/\/techtwaddle.co.in\/blog\/2010\/12\/30\/windows-phone-7-building-a-simple-dictionary-web-client\/","title":{"rendered":"Windows Phone 7: Building a simple dictionary web client"},"content":{"rendered":"<p>Like I mentioned in <a href=\"http:\/\/geekswithblogs.net\/TechTwaddle\/archive\/2010\/11\/29\/dictionary-web-service.aspx\">this post<\/a> a while back, I came across a dictionary web service called <a href=\"http:\/\/www.aonaware.com\/index.htm\">Aonaware<\/a> that serves up word definitions from various dictionaries and is really easy to use. The services page on their website, <a title=\"http:\/\/services.aonaware.com\/DictService\/DictService.asmx\" href=\"http:\/\/services.aonaware.com\/DictService\/DictService.asmx\">http:\/\/services.aonaware.com\/DictService\/DictService.asmx<\/a>, lists all the operations that are supported by the dictionary service. Here they are,<\/p>\n<p><span style=\"color: #400000;\">Word Dictionary Web Service<\/span><\/p>\n<p><span style=\"color: #400000;\">The following operations are supported. For a formal definition, please review the <\/span><a href=\"http:\/\/services.aonaware.com\/DictService\/DictService.asmx?WSDL\"><span style=\"color: #400000;\">Service Description<\/span><\/a><span style=\"color: #400000;\">. <\/span><\/p>\n<ul>\n<li><a href=\"http:\/\/services.aonaware.com\/DictService\/DictService.asmx?op=Define\">Define<\/a><br \/>\n<span style=\"color: #400000;\">Define given word, returning definitions from all dictionaries <\/span><\/li>\n<li><a href=\"http:\/\/services.aonaware.com\/DictService\/DictService.asmx?op=DefineInDict\">DefineInDict<\/a><br \/>\n<span style=\"color: #400000;\">Define given word, returning definitions from specified dictionary <\/span><\/li>\n<li><a href=\"http:\/\/services.aonaware.com\/DictService\/DictService.asmx?op=DictionaryInfo\">DictionaryInfo<\/a><br \/>\n<span style=\"color: #400000;\">Show information about the specified dictionary <\/span><\/li>\n<li><a href=\"http:\/\/services.aonaware.com\/DictService\/DictService.asmx?op=DictionaryList\">DictionaryList<\/a><br \/>\n<span style=\"color: #400000;\">Returns a list of available dictionaries <\/span><\/li>\n<li><a href=\"http:\/\/services.aonaware.com\/DictService\/DictService.asmx?op=DictionaryListExtended\">DictionaryListExtended<\/a><br \/>\n<span style=\"color: #400000;\">Returns a list of advanced dictionaries (e.g. translating dictionaries) <\/span><\/li>\n<li><a href=\"http:\/\/services.aonaware.com\/DictService\/DictService.asmx?op=Match\">Match<\/a><br \/>\n<span style=\"color: #400000;\">Look for matching words in all dictionaries using the given strategy <\/span><\/li>\n<li><a href=\"http:\/\/services.aonaware.com\/DictService\/DictService.asmx?op=MatchInDict\">MatchInDict<\/a><br \/>\n<span style=\"color: #400000;\">Look for matching words in the specified dictionary using the given strategy <\/span><\/li>\n<li><a href=\"http:\/\/services.aonaware.com\/DictService\/DictService.asmx?op=ServerInfo\">ServerInfo<\/a><br \/>\n<span style=\"color: #400000;\">Show remote server information <\/span><\/li>\n<li><a href=\"http:\/\/services.aonaware.com\/DictService\/DictService.asmx?op=StrategyList\">StrategyList<\/a><br \/>\n<span style=\"color: #400000;\">Return list of all available strategies on the server <\/span><\/li>\n<\/ul>\n<p>Follow the links above to get more information on each API.<\/p>\n<p>In this post we will be building a simple windows  phone 7 client which uses this service to get word definitions for words  entered by the user. The application will also allow the user to select  a dictionary from all the available ones and look up the word  definition in that dictionary. So of all the apis above we will be using  only two, <em>DictionaryList()<\/em> to get a list of all supported dictionaries and <em>DefineInDict()<\/em> to get the word definition from a particular dictionary.<\/p>\n<p>Before we get started, a note to you all; I would  have liked to implement this application using concepts from data  binding, item templates, data templates etc. I have a basic  understanding of what they are but, being a beginner, I am not very  comfortable with those topics yet so I didn\u2019t use them. I thought I\u2019ll  get this version out of the way and maybe in the next version I could  give those a try.<\/p>\n<p>A somewhat scary mock-up of the what the final application will look like,<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" title=\"AppMockup\" src=\"http:\/\/geekswithblogs.net\/images\/geekswithblogs_net\/TechTwaddle\/WindowsLiveWriter\/WindowsPhone7Buildingasimpledictionarywe_13E86\/AppMockup_f158c577-3ef8-47bb-8582-0ca83463d2b5.jpg\" border=\"0\" alt=\"AppMockup\" width=\"240\" height=\"409\" \/><\/p>\n<p><em>Select Dictionary <\/em>is a list picker control from the <a href=\"http:\/\/silverlight.codeplex.com\/\">silverlight toolkit<\/a> (you need to <a href=\"http:\/\/silverlight.codeplex.com\/releases\/view\/55034\">download and install<\/a> the toolkit if you haven\u2019t already). Below it is a textbox where the  user can enter words to look up and a button beside it to fetch the word  definition when clicked. Finally we have a textblock which occupies the  remaining area and displays the word definition from the selected  dictionary.<\/p>\n<p>Create a silverlight application for windows phone 7,  AonawareDictionaryClient, and add references to the silverlight toolkit  and the web service. From the solution explorer right on <em>References<\/em> and select <em>Microsoft.Phone.Controls.Toolkit<\/em> from under the .NET tab,<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" title=\"adding-reference-sl-toolkit\" src=\"http:\/\/geekswithblogs.net\/images\/geekswithblogs_net\/TechTwaddle\/WindowsLiveWriter\/WindowsPhone7Buildingasimpledictionarywe_13E86\/adding-reference-sl-toolkit.jpg\" border=\"0\" alt=\"adding-reference-sl-toolkit\" width=\"486\" height=\"410\" \/><\/p>\n<p>Next, add a reference to the web service. Again right click on <em>References<\/em> and this time select <em>Add Service Reference<\/em> In the resulting dialog paste the service url in the Address field and press go, (url \u2013&gt; <a title=\"http:\/\/services.aonaware.com\/DictService\/DictService.asmx\" href=\"http:\/\/services.aonaware.com\/DictService\/DictService.asmx\">http:\/\/services.aonaware.com\/DictService\/DictService.asmx<\/a>)<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" title=\"AddingReference\" src=\"http:\/\/geekswithblogs.net\/images\/geekswithblogs_net\/TechTwaddle\/WindowsLiveWriter\/WindowsPhone7Buildingasimpledictionarywe_13E86\/AddingReference.jpg\" border=\"0\" alt=\"AddingReference\" width=\"635\" height=\"512\" \/><\/p>\n<p>once the service is discovered, provide a name for the NameSpace, in this case I\u2019ve called it <em>AonawareDictionaryService<\/em>. Press OK. You can now use the classes and functions that are generated in the <em>AonawareDictionaryClient.AonawareDictionaryService<\/em> namespace.<\/p>\n<p>Let\u2019s get the UI done now. In <em>MainPage.xaml<\/em> add a namespace declaration to use the toolkit controls,<\/p>\n<p><span style=\"font-family: Arial; color: #400000; font-size: x-small;\">xmlns:toolkit=&#8221;clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit&#8221;<\/span><\/p>\n<p>the content of <em>LayoutRoot<\/em> is changed as follows, (sorry, no syntax highlighting in this post)<\/p>\n<p><span style=\"color: #400000;\"><span style=\"font-family: Arial; font-size: x-small;\">&lt;StackPanel x:Name=&#8221;TitlePanel&#8221; Grid.Row=&#8221;0&#8243; Margin=&#8221;12,5,0,5&#8243;&gt;<br \/>\n&lt;TextBlock x:Name=&#8221;ApplicationTitle&#8221; Text=&#8221;AONAWARE DICTIONARY  CLIENT&#8221; Style=&#8221;{StaticResource PhoneTextNormalStyle}&#8221;\/&gt;<br \/>\n<\/span><span style=\"font-size: x-small;\"><span style=\"font-family: Arial;\"><span style=\"color: #008000;\"> &lt;!&#8211;&lt;TextBlock x:Name=&#8221;PageTitle&#8221; Text=&#8221;page name&#8221;  Margin=&#8221;9,-7,0,0&#8243; Style=&#8221;{StaticResource  PhoneTextTitle1Style}&#8221;\/&gt;&#8211;&gt;<br \/>\n<\/span>&lt;\/StackPanel&gt; <\/span><\/span><\/span><\/p>\n<p><span style=\"color: #400000;\"><span style=\"font-size: x-small;\"><span style=\"font-family: Arial;\"><span style=\"color: #008000;\">&lt;!&#8211;ContentPanel &#8211; place additional content here&#8211;&gt;<br \/>\n<\/span>&lt;Grid x:Name=&#8221;ContentPanel&#8221; Grid.Row=&#8221;1&#8243; Margin=&#8221;12,0,12,0&#8243;&gt;<br \/>\n&lt;Grid.RowDefinitions&gt;<br \/>\n&lt;RowDefinition Height=&#8221;Auto&#8221;\/&gt;<br \/>\n&lt;RowDefinition Height=&#8221;Auto&#8221;\/&gt;<br \/>\n&lt;RowDefinition Height=&#8221;*&#8221;\/&gt;<br \/>\n&lt;\/Grid.RowDefinitions&gt;<br \/>\n&lt;toolkit:ListPicker Grid.Row=&#8221;0&#8243; x:Name=&#8221;listPickerDictionaryList&#8221;<br \/>\nHeader=&#8221;Select Dictionary :&#8221;&gt;<br \/>\n&lt;\/toolkit:ListPicker&gt; <\/span><\/span><\/span><\/p>\n<p><span style=\"font-family: Arial; color: #400000; font-size: x-small;\"> &lt;Grid Grid.Row=&#8221;1&#8243; Margin=&#8221;0,5,0,0&#8243;&gt;<br \/>\n&lt;Grid.ColumnDefinitions&gt;<br \/>\n&lt;ColumnDefinition Width=&#8221;*&#8221;\/&gt;<br \/>\n&lt;ColumnDefinition Width=&#8221;Auto&#8221; \/&gt;<br \/>\n&lt;\/Grid.ColumnDefinitions&gt; <\/span><\/p>\n<p><span style=\"font-family: Arial; color: #400000; font-size: x-small;\"> &lt;TextBox x:Name=&#8221;txtboxInputWord&#8221; Grid.Column=&#8221;0&#8243; GotFocus=&#8221;OnTextboxInputWordGotFocus&#8221; \/&gt;<br \/>\n&lt;Button x:Name=&#8221;btnGo&#8221; Grid.Column=&#8221;1&#8243; Click=&#8221;OnButtonGoClick&#8221; &gt;<br \/>\n&lt;Button.Content&gt;<br \/>\n&lt;Image Source=&#8221;\/images\/button-go.png&#8221;\/&gt;<br \/>\n&lt;\/Button.Content&gt;<br \/>\n&lt;\/Button&gt;<br \/>\n&lt;\/Grid&gt; <\/span><\/p>\n<p><span style=\"font-family: Arial; color: #400000; font-size: x-small;\"> &lt;ScrollViewer Grid.Row=&#8221;2&#8243; x:Name=&#8221;scrollViewer&#8221;&gt;<br \/>\n&lt;TextBlock\u00a0 Margin=&#8221;12,5,12,5&#8243;\u00a0 x:Name=&#8221;txtBlockWordMeaning&#8221; HorizontalAlignment=&#8221;Stretch&#8221;<br \/>\nVerticalAlignment=&#8221;Stretch&#8221; TextWrapping=&#8221;Wrap&#8221;<br \/>\nFontSize=&#8221;26&#8243; \/&gt;<br \/>\n&lt;\/ScrollViewer&gt;<br \/>\n&lt;\/Grid&gt;<\/span><\/p>\n<p>I have commented out the <em>PageTitle <\/em>as it occupies too much valuable space, and the <em>ContentPanel<\/em> is changed to contain three rows. First row contains the list picker  control, second row contains the textbox and the button, and the third  row contains a textblock within a scroll viewer.<\/p>\n<p>The designer will now be showing the final ui,<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" title=\"designerViewOfUI\" src=\"http:\/\/geekswithblogs.net\/images\/geekswithblogs_net\/TechTwaddle\/WindowsLiveWriter\/WindowsPhone7Buildingasimpledictionarywe_13E86\/designerViewOfUI.jpg\" border=\"0\" alt=\"designerViewOfUI\" width=\"273\" height=\"484\" \/><\/p>\n<p>Now go to <em>MainPage.xaml.cs<\/em>, and add the following namespace declarations,<\/p>\n<p><span style=\"font-family: Arial; color: #400000; font-size: x-small;\">using Microsoft.Phone.Controls;<br \/>\nusing AonawareDictionaryClient.AonawareDictionaryService;<br \/>\nusing System.IO.IsolatedStorage;<\/span><\/p>\n<p>A class called <em>DictServiceSoapClient<\/em> would  have been created for you in the background when you added a reference  to the web service. This class functions as a wrapper to the services  exported by the web service. All the web service functions that we saw  at the start can be access through this class, or more precisely through  an object of this class.<\/p>\n<p>Create a data member of type <em>DictServiceSoapClient<\/em> in the <em>Mainpage<\/em> class, and a function which initializes it,<\/p>\n<p><span style=\"font-family: Arial; color: #400000; font-size: x-small;\">DictServiceSoapClient DictSvcClient = null; <\/span><\/p>\n<p><span style=\"font-family: Arial; color: #400000; font-size: x-small;\">private DictServiceSoapClient GetDictServiceSoapClient()<br \/>\n{<br \/>\nif (null == DictSvcClient)<br \/>\n{<br \/>\nDictSvcClient = new DictServiceSoapClient();<br \/>\n} <\/span><\/p>\n<p><span style=\"font-family: Arial; color: #400000; font-size: x-small;\"> return DictSvcClient;<br \/>\n} <\/span><\/p>\n<p>We have two major tasks remaining. First, when the  application loads we need to populate the list picker with all the  supported dictionaries and second, when the user enters a word and  clicks on the arrow button we need to fetch the word\u2019s meaning.<\/p>\n<p><strong><br \/>\nPopulating the List Picker<\/strong><\/p>\n<p>In the <em>OnNavigatingTo<\/em> event of the <em>MainPage<\/em>, we call the <em>DictionaryList()<\/em> api. This can also be done in the <em>OnLoading<\/em> event handler of the <em>MainPage<\/em>; not sure if one has an advantage over the other. Here\u2019s the code for <em>OnNavigatedTo<\/em>,<\/p>\n<p><span style=\"font-family: arial; color: #400000; font-size: x-small;\">protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)<br \/>\n{<br \/>\nDictServiceSoapClient client = GetDictServiceSoapClient(); <\/span><\/p>\n<p><span style=\"font-family: arial; color: #400000; font-size: x-small;\"> client.DictionaryListCompleted += new EventHandler&lt;DictionaryListCompletedEventArgs&gt;(OnGetDictionaryListCompleted); <\/span><\/p>\n<p><span style=\"font-family: arial; color: #400000; font-size: x-small;\"> client.DictionaryListAsync(); <\/span><\/p>\n<p><span style=\"font-family: arial; color: #400000; font-size: x-small;\"> base.OnNavigatedTo(e);<br \/>\n} <\/span><\/p>\n<p>Windows Phone 7 supports only async calls to web  services. When we added a reference to the dictionary service,  asynchronous versions of all the functions were generated automatically.  So in the above function we register a handler to the <em>DictionaryListCompleted<\/em> event which will occur when the call to <em>DictionaryList()<\/em> gets a response from the server. Then we call the <em>DictionaryListAsync()<\/em> function which is the async version of the <em>DictionaryList()<\/em> api. The result of this api will be sent to the handler <em>OnGetDictionaryListCompleted()<\/em>,<\/p>\n<p><span style=\"font-family: arial; color: #400000; font-size: x-small;\">void OnGetDictionaryListCompleted(object sender, DictionaryListCompletedEventArgs e)<br \/>\n{<br \/>\nIsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;<br \/>\nDictionary[] listOfDictionaries; <\/span><\/p>\n<p><span style=\"font-family: arial; color: #400000; font-size: x-small;\"> if (e.Error == null)<br \/>\n{<br \/>\nlistOfDictionaries = e.Result;<br \/>\nPopulateListPicker(listOfDictionaries, settings);<br \/>\n}<br \/>\nelse if (settings.Contains(&#8220;SavedDictionaryList&#8221;))<br \/>\n{<br \/>\nlistOfDictionaries = settings[&#8220;SavedDictionaryList&#8221;] as Dictionary[];<br \/>\nPopulateListPicker(listOfDictionaries, settings);<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nMessageBoxResult res = MessageBox.Show(&#8220;An error occured while  retrieving dictionary list, do you want to try again?&#8221;, &#8220;Error&#8221;,  MessageBoxButton.OKCancel); <\/span><\/p>\n<p><span style=\"font-family: arial; color: #400000; font-size: x-small;\"> if (MessageBoxResult.OK == res)<br \/>\n{<br \/>\nGetDictServiceSoapClient().DictionaryListAsync();<br \/>\n}<br \/>\n} <\/span><\/p>\n<p><span style=\"font-family: arial; color: #400000; font-size: x-small;\"> settings.Save();<br \/>\n}<\/span><\/p>\n<p>I have used <em>IsolatedStorageSettings<\/em> to store  a few things; the entire dictionary list and the dictionary that is  selected when the user exits the application, so that the next time when  the user starts the application the current dictionary is set to the  last selected value. First we check if the api returned any error, if  the error object is null <em>e.Result<\/em> will contain the list (actually array) of <em>Dictionary<\/em> type objects. If there was an error, we check the isolated storage  settings to see if there is a dictionary list stored from a previous  instance of the application and if so, we populate the list picker based  on this saved list. Note that in this case there are chances that the  dictionary list might be out of date if there have been changes on the  server. Finally, if none of these cases are true, we display an error  message to the user and try to fetch the list again.<\/p>\n<p><em>PopulateListPicker()<\/em> is passed the array of <em>Dictionary<\/em> objects and the settings object as well,<\/p>\n<p><span style=\"font-family: arial; color: #400000; font-size: x-small;\">void PopulateListPicker(Dictionary[] listOfDictionaries, IsolatedStorageSettings settings)<br \/>\n{<br \/>\nlistPickerDictionaryList.Items.Clear(); <\/span><\/p>\n<p><span style=\"font-family: arial; color: #400000; font-size: x-small;\"> foreach (Dictionary dictionary in listOfDictionaries)<br \/>\n{<br \/>\nlistPickerDictionaryList.Items.Add(dictionary.Name);<br \/>\n} <\/span><\/p>\n<p><span style=\"font-family: arial; color: #400000; font-size: x-small;\"> settings[&#8220;SavedDictionaryList&#8221;] = listOfDictionaries; <\/span><\/p>\n<p><span style=\"font-family: arial; color: #400000; font-size: x-small;\"> string savedDictionaryName;<br \/>\nif (settings.Contains(&#8220;SavedDictionary&#8221;))<br \/>\n{<br \/>\nsavedDictionaryName = settings[&#8220;SavedDictionary&#8221;] as string;<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nsavedDictionaryName = &#8220;WordNet (r) 2.0&#8221;; \/\/default dictionary, wordnet<br \/>\n} <\/span><\/p>\n<p><span style=\"font-family: arial; color: #400000; font-size: x-small;\"> foreach (string dictName in listPickerDictionaryList.Items)<br \/>\n{<br \/>\nif (dictName == savedDictionaryName)<br \/>\n{<br \/>\nlistPickerDictionaryList.SelectedItem = dictName;<br \/>\nbreak;<br \/>\n}<br \/>\n} <\/span><\/p>\n<p><span style=\"font-size: x-small;\"><span style=\"font-family: arial; color: #400000;\"> settings[&#8220;SavedDictionary&#8221;] = listPickerDictionaryList.SelectedItem as string;<br \/>\n} <\/span><\/span><\/p>\n<p>We first clear all the items from the list picker,  add the dictionary names from the array and then create a key in the  settings called <em>SavedDictionaryList<\/em> and store the dictionary  list in it. We then check if there is saved dictionary available from a  previous instance, if there is, we set it as the selected item in the  list picker. And if not, we set \u201cWordNet \u00ae 2.0\u201d as the default  dictionary. Before returning, we save the selected dictionary in the \u201c<em>SavedDictionary<\/em>\u201d key of the isolated storage settings.<br \/>\n<strong>Fetching word definitions<\/strong><\/p>\n<p>Getting this part done is very similar to the above code. We get the input word from the textbox, call into <em>DefineInDictAsync()<\/em> to fetch the definition and when <em>DefineInDictAsync<\/em> completes, we get the result and display it in the textblock. Here is the handler for the button click,<\/p>\n<p><span style=\"font-family: arial; color: #400000; font-size: x-small;\">private void OnButtonGoClick(object sender, RoutedEventArgs e)<br \/>\n{<br \/>\ntxtBlockWordMeaning.Text = &#8220;Please wait..&#8221;; <\/span><\/p>\n<p><span style=\"font-family: arial; color: #400000; font-size: x-small;\"> IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings; <\/span><\/p>\n<p><span style=\"font-family: arial; color: #400000; font-size: x-small;\"> if (txtboxInputWord.Text.Trim().Length &lt;= 0)<br \/>\n{<br \/>\nMessageBox.Show(&#8220;Please enter a word in the textbox and press &#8216;Go'&#8221;);<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nDictionary[] listOfDictionaries = settings[&#8220;SavedDictionaryList&#8221;] as Dictionary[];<br \/>\nstring selectedDictionary = listPickerDictionaryList.SelectedItem.ToString(); <\/span><\/p>\n<p><span style=\"font-family: arial; color: #400000; font-size: x-small;\"> string dictId = &#8220;wn&#8221;; \/\/default dictionary is wordnet (wn is the dict id) <\/span><\/p>\n<p><span style=\"font-family: arial; color: #400000; font-size: x-small;\"> foreach (Dictionary dict in listOfDictionaries)<br \/>\n{<br \/>\nif (dict.Name == selectedDictionary)<br \/>\n{<br \/>\ndictId = dict.Id;<br \/>\nbreak;<br \/>\n}<br \/>\n} <\/span><\/p>\n<p><span style=\"font-family: arial; color: #400000; font-size: x-small;\"> DictServiceSoapClient client = GetDictServiceSoapClient(); <\/span><\/p>\n<p><span style=\"font-family: arial; color: #400000; font-size: x-small;\"> client.DefineInDictCompleted += new EventHandler&lt;DefineInDictCompletedEventArgs&gt;(OnDefineInDictCompleted);<br \/>\nclient.DefineInDictAsync(dictId, txtboxInputWord.Text.Trim());<br \/>\n}<br \/>\n}<\/span><\/p>\n<p>We validate the input and then select the dictionary  id based on the currently selected dictionary. We need the dictionary id  because the api <em>DefineInDict()<\/em> expects the dictionary  identifier and not the dictionary name. We could very well have stored  the dictionary id in isolated storage settings too. Again, same as  before, we register a event handler for the <em>DefineInDictCompleted<\/em> event and call the <em>DefineInDictAsync()<\/em> method passing in the dictionary id and the input word.<\/p>\n<p><span style=\"font-family: Arial; color: #400000; font-size: x-small;\">void OnDefineInDictCompleted(object sender, DefineInDictCompletedEventArgs e)<br \/>\n{<br \/>\nWordDefinition wd = e.Result; <\/span><\/p>\n<p><span style=\"font-family: Arial; color: #400000; font-size: x-small;\"> scrollViewer.ScrollToVerticalOffset(0.0f); <\/span><\/p>\n<p><span style=\"font-family: Arial; color: #400000; font-size: x-small;\"> if (wd == null || e.Error != null || wd.Definitions.Length == 0)<br \/>\n{<br \/>\ntxtBlockWordMeaning.Text = String.Format(&#8220;No definitions were  found for &#8216;{0}&#8217; in &#8216;{1}'&#8221;, txtboxInputWord.Text.Trim(),  listPickerDictionaryList.SelectedItem.ToString().Trim());<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nforeach (Definition def in wd.Definitions)<br \/>\n{<br \/>\nstring str = def.WordDefinition;<br \/>\nstr = str.Replace(&#8221;\u00a0 &#8220;, &#8221; &#8220;); \/\/some formatting<br \/>\ntxtBlockWordMeaning.Text = str;<br \/>\n}<br \/>\n}<br \/>\n} <\/span><\/p>\n<p>When the api completes, <em>e.Result<\/em> will contain a <em>WordDefnition<\/em> object. This class is also generated in the background while adding the  service reference. We check the word definitions within this class to  see if any results were returned, if not, we display a message to the  user in the textblock. If a definition was found the text on the  textblock is set to display the definition of the word.<\/p>\n<p>Adding final touches, we now need to save the current  dictionary when the application exits. A small but useful thing is  selecting the entire word in the input textbox when the user selects it.  This makes sure that if the user has looked up a definition for a  really long word, he doesn\u2019t have to press \u2018<em>clear<\/em>\u2019 too many times to enter the next word,<\/p>\n<p><span style=\"font-family: arial; color: #400000; font-size: x-small;\">protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)<br \/>\n{<br \/>\nIsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings; <\/span><\/p>\n<p><span style=\"font-family: arial; color: #400000; font-size: x-small;\"> settings[&#8220;SavedDictionary&#8221;] = listPickerDictionaryList.SelectedItem as string; <\/span><\/p>\n<p><span style=\"font-family: arial; color: #400000; font-size: x-small;\"> settings.Save(); <\/span><\/p>\n<p><span style=\"font-family: arial; color: #400000; font-size: x-small;\"> base.OnNavigatingFrom(e);<br \/>\n} <\/span><\/p>\n<p><span style=\"font-family: arial; color: #400000; font-size: x-small;\">private void OnTextboxInputWordGotFocus(object sender, RoutedEventArgs e)<br \/>\n{<br \/>\nTextBox txtbox = sender as TextBox; <\/span><\/p>\n<p><span style=\"font-family: arial; color: #400000; font-size: x-small;\"> if (txtbox.Text.Trim().Length &gt; 0)<br \/>\n{<br \/>\ntxtbox.SelectionStart = 0;<br \/>\ntxtbox.SelectionLength = txtbox.Text.Length;<br \/>\n}<br \/>\n} <\/span><\/p>\n<p><em>OnNavigatingFrom()<\/em> is called whenever you navigate away from the <em>MainPage<\/em>, since our application contains only one page that would mean that it is exiting (or getting tombstoned).<\/p>\n<p>I leave you with a short video of the application in  action, but before that if you have any suggestions on how to make the  code better and improve it please do leave a comment.<\/p>\n<p><object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" width=\"480\" height=\"385\" codebase=\"http:\/\/download.macromedia.com\/pub\/shockwave\/cabs\/flash\/swflash.cab#version=6,0,40,0\"><param name=\"allowFullScreen\" value=\"true\" \/><param name=\"allowscriptaccess\" value=\"always\" \/><param name=\"src\" value=\"http:\/\/www.youtube.com\/v\/cqruDwzO_90?fs=1&amp;hl=en_US&amp;color1=0x3a3a3a&amp;color2=0x999999\" \/><param name=\"allowfullscreen\" value=\"true\" \/><\/object><\/p>\n<p>Until next time\u2026<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Like I mentioned in this post a while back, I came across a dictionary web service called Aonaware that serves up word definitions from various dictionaries and is really easy to use. The services page on their website, http:\/\/services.aonaware.com\/DictService\/DictService.asmx, lists all the operations that are supported by the dictionary service. Here they are, Word Dictionary &hellip; <a href=\"https:\/\/techtwaddle.co.in\/blog\/2010\/12\/30\/windows-phone-7-building-a-simple-dictionary-web-client\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Windows Phone 7: Building a simple dictionary web client<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1ktFF-11","_links":{"self":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts\/63"}],"collection":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/comments?post=63"}],"version-history":[{"count":2,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts\/63\/revisions"}],"predecessor-version":[{"id":71,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts\/63\/revisions\/71"}],"wp:attachment":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/media?parent=63"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/categories?post=63"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/tags?post=63"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}