如何突出显示 Windows Phone 7 中上下文相关搜索的结果? [英] How to Highlight the result from context sensitive search in windows phone 7?
本文介绍了如何突出显示 Windows Phone 7 中上下文相关搜索的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要根据windows phone 7中文本框中输入的文本突出显示搜索结果,
I need to to Highlight the search result according to the text that is entered in the text box in windows phone 7,
通常的 wpf 代码在 Windows Phone 7 中不起作用..有人说如何在windows phone 7中实现这一点
the usual wpf code is not working in windows phone 7. . Somebody say how to achieve this in windows phone 7
实际上这是我用来填充联系人列表的 xaml 列表框,
Actually this is the xaml Listbox I'm using to populate the Contact list,
<ListBox Name="ContactList" ItemsSource="{Binding}" Margin="14,85,14,28" Foreground="White" SizeChanged="ContactList_SizeChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Border BorderThickness="2" HorizontalAlignment="Left" VerticalAlignment="Center" BorderBrush="{StaticResource PhoneAccentBrush}" >
<Image Source="{Binding Converter={StaticResource ContactPictureConverter}}" Width="48" Height="48" Stretch="Fill" Name="img1" />
</Border>
<TextBlock Name="ContactResults" Text="{Binding Path=DisplayName, Mode=OneWay}" FontSize="{StaticResource PhoneFontSizeExtraLarge}" Margin="18,8,0,0" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<TextBox Name="contactFilterString" Margin="0,0,0,528" TextChanged="contactFilterString_TextChanged" />
C# 代码,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
//using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.UserData;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.ObjectModel;
using Microsoft.Phone.Shell;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Text.RegularExpressions;
namespace SmartContactsApp
{
public partial class MainPage : PhoneApplicationPage
{
private List<Address> lstAddress = new List<Address>();
public string addressJson = string.Empty;
// Constructor
public MainPage()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
CreateSecondaryTile();
}
/// <summary>
/// To List all the Contacts. . .
/// </summary>
private void ContactListing()
{
ContactList.DataContext = null;
Contacts cons = new Contacts();
cons.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(Contacts_SearchCompleted);
cons.SearchAsync(contactFilterString.Text, FilterKind.DisplayName, "Contacts");
}
/// <summary>
/// To Fetch All Contacts from Mobile Contacts. . .
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
{
try
{
ContactList.DataContext = e.Results;
}
catch (Exception)
{
throw;
}
}
private void contactFilterString_TextChanged(object sender, TextChangedEventArgs e)
{
ContactListing();
}
}
如何突出显示,
提前致谢!
推荐答案
我在 这个
Xml 代码:
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ListBox Name="ContactList" Margin="14,85,14,28" Foreground="White">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding DisplayName}" Width="450" TextWrapping="Wrap" FontSize="24" Visibility="Collapsed"/>
<RichTextBox Width="450" FontSize="24" Foreground="#FFFFFF"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<TextBox Name="contactFilterString" Margin="0,0,0,528" TextChanged="contactFilterString_TextChanged" />
</Grid>
c# 代码:
private void contactFilterString_TextChanged(object sender, TextChangedEventArgs e)
{
// ContactListing();
SearchVisualTree(ContactList);
if (contactFilterString.Text == "")
{
ContactListing();
}
}
private void SearchVisualTree(Action ContactListing)
{
SearchVisualTree(ContactList);
}
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;
HighlightText();
break;
}
else
{
//ContactListing();
SearchVisualTree(child);
}
}
}
private void HighlightText()
{
if (textBlock1 != null)
{
string text = textBlock1.Text;
textBlock1.Text = text;
textBlock1.Inlines.Clear();
int index = text.IndexOf(contactFilterString.Text);
int lenth = contactFilterString.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 });
textBlock1.FontSize = 30;
textBlock1.Foreground = new SolidColorBrush(Colors.Black);
}
else
{
//textBlock1.Text = "No Match";
}
}
}
/// <summary>
/// To List all the Contacts. . .
/// </summary>
private void ContactListing()
{
ContactList.DataContext = null;
Contacts cons = new Contacts();
cons.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(Contacts_SearchCompleted);
cons.SearchAsync(contactFilterString.Text, FilterKind.DisplayName, "Contacts");
}
/// <summary>
/// To Fetch All Contacts from Mobile Contacts. . .
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
{
try
{
ContactList.DataContext = e.Results;
}
catch (Exception)
{
throw;
}
}
感谢您的帮助!
这篇关于如何突出显示 Windows Phone 7 中上下文相关搜索的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文