【问题标题】:c# windows phone -- text box doesn't understand foreign characters?c# windows phone -- 文本框看不懂外文?
【发布时间】:2014-01-12 17:13:08
【问题描述】:

我正在构建一个应用来访问适用于 Windows Phone 的 Google 翻译。我注意到,对于翻译后的单词将包含外来字符的语言,文本框会全部搞砸,而是显示随机符号等。不知道为什么。我需要在文本框中启用某些内容吗?

代码:

private void btnTranslate_Click(object sender, RoutedEventArgs e)
    {
        string text = txtboxOriginal.Text;
        string fromLanguage;
        string toLanguage;
        //Use Bing.
        if ((bool)settings["translateIsBing"] == true)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                MessageBox.Show("Can't translate with nothing to translate. Please try again.");
            }
            else
            {

            }
        }

        //Use Google.
        else if ((bool)settings["translateIsGoogle"] == true)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                MessageBox.Show("Can't translate with nothing to translate. Please try again.");
            }
            else
            {
                fromLanguage = this.getLanguageCodeGoogle(lstOriginalLanguages.SelectedIndex.ToString());
                toLanguage = this.getLanguageCodeGoogle(lstTranslateToLanguages.SelectedIndex.ToString());
                string url = "http://translate.google.com/translate_t?text=" + text + "&sl=" + fromLanguage + "&tl=" + toLanguage;
                WebClient webclient = new WebClient();
                webclient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webclient_DownloadStringCompleted);
                webclient.DownloadStringAsync(new Uri(url, UriKind.RelativeOrAbsolute));
            }
        }

    }

void webclient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error == null)
        {
            try
            {
                Debug.WriteLine(e.Result);
                string result = e.Result;
                int startPosition = result.IndexOf("TRANSLATED_TEXT='");
                int length = result.IndexOf(@"';INPUT") - startPosition;
                string partial = result.Substring(startPosition, length);
                Debug.WriteLine("Step 1: {0}", partial);
                startPosition = partial.IndexOf("'") + 1;
                length = partial.Length - startPosition;
                string secondPartial = partial.Substring(startPosition, length);
                Debug.WriteLine("Step 2: {0}", secondPartial);
                translatedText = secondPartial;
                txtboxOriginal.Text = secondPartial;
                btnTranslate.Content = "Translated";
                btnTranslate.IsEnabled = false;
                btnCopy.Visibility = Visibility.Visible;
                btnCopy.IsEnabled = true;
                btnReset.Visibility = Visibility.Visible;
                btnReset.IsEnabled = true;
            }
            catch (Exception ex) 
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

【问题讨论】:

    标签: c# visual-studio windows-phone


    【解决方案1】:

    问题可能与 Google 响应的编码类型有关。 当您查看响应的 Content-type 时,WebClient.DownloadStringAsync 将其解码为的并不总是 UTF8,因此您必须更改解码类型

    您必须更改 Webclient.Encoding 以匹配请求类型

    问题的解决方法可以找到here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-10
      • 1970-01-01
      • 1970-01-01
      • 2015-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多