2014-10-04 65 views
0

我正在下载一个xml文件并将其保存到手机(Windows Phone 8应用程序),然后将其作为列表打开。这份清单中有希腊文字,包括上,下和特殊字符。我希望用户能够通过这个列表进行搜索,而不用让他写出与列表中相同的文本,所以,到目前为止,我已经将搜索栏和列表变成了大写字母,所以如果用户输入“to”该列表突出显示“to”,“To”,“TO”,暂时擦除不匹配的单词。但是,如果用户试图写出一个没有语调的字母,则列表中具有语调的单词将全部擦除,直到用户按下退格键并用语调输入该字母。替换列表中的特殊字符,以便您可以轻松搜索

例如:列表中有2个单词。 Σπύρος,Μάρα。 用户按下列表中的字母'Μ',Σπύρος消失,并且Μάρα变为Μάρα。现在用户键入'α'并且M也会消失,因为他必须键入'ά'。如果他有,它会是Μάρα等。

所以我的问题是,是否可以用没有语调的相同字符替换一个带有语调的字符?例如。 í到a,Ά到A等。在Unicode字符表上它们有不同的代码。

至今代码:

private void businessFilterString_TextChanged(object sender, TextChangedEventArgs e) 
    { 
     sI.ItemsSource = businesses.Collection.Where(b => b.name.ToUpper().Contains(searchBox.Text.ToUpper()) || b.address.ToUpper().Contains(searchBox.Text.ToUpper())).ToList(); 
     LayoutUpdateFlag = true; 
    } 

    private void sI_LayoutUpdated(object sender, EventArgs e) 
    { 
     if (LayoutUpdateFlag) 
     { 
      SearchVisualTree(sI); 
     } 
     LayoutUpdateFlag = false; 
    } 
    private void SearchVisualTree(DependencyObject targetElement) 
    { 

     var count = VisualTreeHelper.GetChildrenCount(targetElement); 

     for (int i = 0; i < count; i++) 
     { 
      var child = VisualTreeHelper.GetChild(targetElement, i); 
      if (child is TextBlock) 
      { 
       textBlock1 = (TextBlock)child; 
       prevText = textBlock1.Text.ToUpper(); 
       HighlightText(); 
      } 
      else 
      { 
       SearchVisualTree(child); 
      } 
     } 
    } 

    private void HighlightText() 
    { 
     if (textBlock1 != null) 
     { 
      string text = textBlock1.Text.ToUpper(); 
      textBlock1.Text = text; 
      textBlock1.Inlines.Clear(); 

      int index = text.IndexOf(searchBox.Text.ToUpper()); 
      int lenth = searchBox.Text.Length; 


      if (!(index < 0)) 
      { 
       Run run = new Run() { Text = text.Substring(index, lenth), FontWeight = FontWeights.ExtraBold}; 
       run.Foreground = new SolidColorBrush(Colors.Orange); 
       textBlock1.Inlines.Add(new Run() { Text = text.Substring(0, index), FontWeight = FontWeights.Normal}); 
       textBlock1.Inlines.Add(run); 
       textBlock1.Inlines.Add(new Run() { Text = text.Substring(index + lenth), FontWeight = FontWeights.Normal }); 
      } 
      else 
      { 
       //textBlock1.Text = "No Match"; 
      } 
     } 
    } 
+1

可能的重复http://stackoverflow.com/questions/249087/how-do-i-remove-diacritics-accents-from-a-string-in-net – Mrchief 2014-10-04 15:37:02

+0

谢谢。我会检查一下,看看它是否也适用于我的案例! – 2014-10-04 21:52:14

回答

0

所以我添加

private static string DeleteAccentAndSpecialsChar(string OriginalText) 
    { 
     string strTemp = OriginalText; 

     Regex rega = new Regex("[Ά]"); 
     Regex rege = new Regex("[Έ]"); 
     Regex regi = new Regex("[Ί]"); 
     Regex regu = new Regex("[Ύ]"); 
     Regex regh = new Regex("[Ή]"); 
     Regex rego = new Regex("[Ό]"); 
     Regex regw = new Regex("[Ώ]"); 

     strTemp = rega.Replace(strTemp, "Α"); 
     strTemp = rege.Replace(strTemp, "Ε"); 
     strTemp = regi.Replace(strTemp, "Ι"); 
     strTemp = regu.Replace(strTemp, "Υ"); 
     strTemp = regh.Replace(strTemp, "Η"); 
     strTemp = rego.Replace(strTemp, "Ο"); 
     strTemp = regw.Replace(strTemp, "Ω"); 

     return strTemp; 
    } 

,改变

private void businessFilterString_TextChanged(object sender, TextChangedEventArgs e) 
{ 
    sI.ItemsSource = businesses.Collection.Where(b => b.name.ToUpper().Contains(searchBox.Text.ToUpper()) || b.address.ToUpper().Contains(searchBox.Text.ToUpper())).ToList(); 
    LayoutUpdateFlag = true; 
} 

private void businessFilterString_TextChanged(object sender, TextChangedEventArgs e) 
    { 
     sI.ItemsSource = businesses.Collection.Where(b => DeleteAccentAndSpecialsChar(b.name.ToUpper()).Contains(DeleteAccentAndSpecialsChar(searchBox.Text.ToUpper()))).ToList(); 
     LayoutUpdateFlag = true; 
    } 

这样所有的文字都显示在上面,语调消失。如果用户在他的文本中使用语调,则不会显示任何东西,但对我来说很合适,因为上面的文本不需要语调。