【发布时间】:2014-10-04 14:10:57
【问题描述】:
我正在下载一个 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";
}
}
}
【问题讨论】:
-
谢谢。我会检查一下,看看它是否也适用于我的情况!
标签: c# regex windows-phone-8 text