Semantic Text Query In Windows 10

Here are the steps: 

Step 1: Open a blank app and add two TextBlocks and a button either from the toolbox or by copying the following XAML code into your grid.
  1. <StackPanel Margin="10,40,0,0">  
  2. <TextBlock Text="Type the word to find" ></TextBlock>  
  3. <TextBox Name="textToFind" Width="200" Margin="0,10,0,0" HorizontalAlignment="Left"></TextBox>  
  4. <TextBlock Text="Type or Copy and Paste a sentence to find from " Margin="0,10,0,0" ></TextBlock>  
  5. <TextBox Name="sentence" Height="100" Width="300" HorizontalAlignment="Left" Margin="0,10,0,0" TextWrapping="Wrap"></TextBox>  
  6. <Button Name="find" Content="Find" Height="40" Width="120" Click="find_Click" Margin="0,10,0,0" ></Button>  
  7. <TextBlock Name="result" TextWrapping="Wrap" Width="300" HorizontalAlignment="Left"></TextBlock>  
  8. </StackPanel>    
 

Step 2:
Add the following namespaces to your project which is needed in further C# code.
  1. using System.Collections.Generic;  
  2. using Windows.Data.Text;  
  3. using Windows.UI.Text;  
  4. using Windows.UI.Xaml.Documents;  

Step 3: Copy and paste the following code to the cs page which will be called on button click event and the corresponding text will be highlighted.

  1. private void find_Click(object sender, RoutedEventArgs e)  
  2.         {  
  3.             result.Text = "";  
  4.             var mySemanticTextQuery = new Windows.Data.Text.SemanticTextQuery(textToFind.Text);  
  5.             IReadOnlyList<Windows.Data.Text.TextSegment> ranges = mySemanticTextQuery.Find(sentence.Text);  
  6.             HighlightRanges(result, sentence.Text, ranges);  
  7.         }  
  8.   
  9.         public void HighlightRanges(TextBlock tb, String TextContent, IReadOnlyList<TextSegment> ranges)  
  10.         {  
  11.             int currentPosition = 0;  
  12.             foreach (var range in ranges)  
  13.             {  
  14.                 if (range.StartPosition > currentPosition)  
  15.                 {  
  16.                     int length = (int)range.StartPosition - currentPosition;  
  17.                     var subString = TextContent.Substring(currentPosition, length);  
  18.                     tb.Inlines.Add(new Run() { Text = subString });  
  19.                     currentPosition += length;  
  20.                 }  
  21.                 var boldString = TextContent.Substring((int)range.StartPosition, (int)range.Length);  
  22.                 tb.Inlines.Add(new Run() { Text = boldStringFontWeight = FontWeights.Bold });  
  23.                 currentPosition += (int)range.Length;  
  24.             }  
  25.             if (currentPosition < TextContent.Length)  
  26.             {  
  27.                 var subString = TextContent.Substring(currentPosition);  
  28.                 tb.Inlines.Add(new Run() { Text = subString });  
  29.             }  
  30.             tb.Inlines.Add(new Run() { Text = "\r\n" });  
  31.         }  

Step 4: Run your application and test yourself.

Up Next
    Ebook Download
    View all
    Learn
    View all