예제 #1
0
        /// <summary>
        /// Get translated text from Bing Translator service.
        /// </summary>
        /// <param name="textToTranslate">Text to translate.</param>
        /// <param name="fromLang">Language to translate from.</param>
        /// <param name="toLang">Language to translate to.</param>
        /// <returns>Translated text.</returns>
        List <string> ITranslator.GetTranslatedText(string textToTranslate,
                                                    string fromLang,
                                                    string toLang,
                                                    ILoginCredentials login)
        {
            if (login == null)
            {
                return(null);
            }

            // the TranslatorContainer gives us access to the Microsoft Translator services
            MSTranslate.Service.TranslatorContainer tc = new MSTranslate.Service.TranslatorContainer(login.TranslationServiceUri);

            // Give the TranslatorContainer access to your subscription
            tc.Credentials = new NetworkCredential(SecureStringExtensionMethod.ConvertToUnsecureString(login.User),
                                                   login.Password);

            try
            {
                // Generate the query
                DataServiceQuery <Translation> translationQuery = tc.Translate(textToTranslate, toLang, fromLang);

                // Call the query and get the results as a List
                var translationResults = translationQuery.Execute().ToList();

                // Verify there was a result
                if (translationResults.Count() <= 0)
                {
                    return(new List <string>());
                }

                // In case there were multiple results, pick the first one
                var translationResult = translationResults.First();

                List <string> ret = new List <string>();

                for (int i = 0; i < translationResults.Count(); i++)
                {
                    ret.Add(translationResult.Text);
                }

                return(ret);
            }
            catch (Exception exc)
            {
                throw new Exception(string.Format("Translation text:'{0}', toLang: '{1}', fromLang '{2}'", textToTranslate, toLang, fromLang), exc);
            }
        }
        /// <summary>
        /// This method is hooked in the definition of the <seealso cref="DropCommandProperty"/>.
        /// It is called whenever the attached property changes - in our case the event of binding
        /// and unbinding the property to a sink is what we are looking for.
        /// </summary>
        /// <param name="d"></param>
        /// <param name="e"></param>
        private static void OnCommandChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var uiElement = d as PasswordBox;                   // Remove the handler if it exist to avoid memory leaks

            uiElement.PasswordChanged -= UiElement_PasswordChanged;

            if (e.NewValue != null)
            {
                var secureString = e.NewValue as SecureString;
                if (secureString != null)
                {
                    // Question: Is there any more secure way to init the passowrd box content with?
                    uiElement.Password = SecureStringExtensionMethod.ConvertToUnsecureString(secureString);
                }
            }

            // the property is attached so we attach the Drop event handler
            uiElement.PasswordChanged += UiElement_PasswordChanged;
        }