<!--
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
-->
<UserControl x:Class="Microsoft.Windows.Controls.Samples.SearchSuggestionSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:clr="clr-namespace:System;assembly=mscorlib"
xmlns:common="clr-namespace:Microsoft.Windows.Controls.Samples;assembly=Microsoft.Windows.Controls.Samples.Common"
xmlns:controls="clr-namespace:Microsoft.Windows.Controls;assembly=Microsoft.Windows.Controls">
<UserControl.Resources>
<Style x:Key="SearchTextBoxStyle" TargetType="TextBox">
<Setter Property="Background" Value="#AAFFFFFF" />
<Setter Property="FontFamily" Value="Verdana" />
<Setter Property="FontSize" Value="18" />
<Setter Property="Padding" Value="2" />
</Style>
</UserControl.Resources>
<StackPanel>
<ContentControl Content="Live.com Search Suggestions" Style="{StaticResource Header}" />
<!-- AutoCompleteBox example -->
<Grid Width="650" Height="406" Background="{StaticResource SearchSuggestionsImageBrush}">
<StackPanel Height="34" Width="490" Orientation="Horizontal" VerticalAlignment="Top" Margin="80,119,80,0">
<TextBlock FontSize="18" FontFamily="Verdana" VerticalAlignment="Center"><Run Text="Live Search"/></TextBlock>
<Grid Width="360" Height="34" Margin="8,0,0,0">
<controls:AutoCompleteBox
SearchMode="None"
x:Name="Search"
TextBoxStyle="{StaticResource SearchTextBoxStyle}"
IsEnabled="False" />
<Button x:Name="Go" Cursor="Hand" IsEnabled="False" Width="32" Height="32" HorizontalAlignment="Right" BorderThickness="0,0,0,0" RenderTransformOrigin="0.5,0.5">
<Button.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX="0.85" ScaleY="0.85"/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Button.RenderTransform>
<Button.Template>
<ControlTemplate TargetType="Button">
<Grid Margin="0" x:Name="RootElement">
<Rectangle Margin="0" Fill="#FF549C00" Stroke="#FF000000" StrokeThickness="0" RadiusX="2.5" RadiusY="2.5"/>
<Ellipse Height="16.105" Margin="0,6.438,5,0" VerticalAlignment="Top" Stroke="#FFFFFFFF" StrokeThickness="2" Width="15.938" HorizontalAlignment="Right"/>
<Path Height="7.875" HorizontalAlignment="Left" Margin="7.754,0,0,6.467" VerticalAlignment="Bottom" Width="7.75" Fill="#FFFFFFFF" Stretch="Fill" Stroke="#FFFFFFFF" StrokeThickness="2" Data="M14.097251,19.182762 L8.7665224,24.552877"/>
</Grid>
</ControlTemplate>
</Button.Template>
</Button>
</Grid>
</StackPanel>
<!-- Hosting warning message -->
<StackPanel x:Name="HostingWarning" Background="#88000000">
<StackPanel Background="#aaffffff" Margin="6">
<TextBlock Margin="6" UseLayoutRounding="False" FontWeight="Bold">This sample must be hosted on a web server.</TextBlock>
<TextBlock Margin="6" UseLayoutRounding="False" TextWrapping="Wrap">A cross-domain web service call can only be made when the page is hosted from a server via the HTTP scheme. Consider hosting this using IIS, the built-in Web Development Server in Visual Studio, or another server technology.</TextBlock>
</StackPanel>
</StackPanel>
</Grid>
<src:SourceViewer xmlns:src="clr-namespace:Microsoft.Windows.Controls.Samples;assembly=Microsoft.Windows.Controls.Samples.Common" xmlns:sys="clr-namespace:System;assembly=mscorlib">
<src:SourceFile Path="SearchSuggestionSample.xaml">
<src:SourceFile.Source>
<sys:String><!--
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
-->
<UserControl x:Class="Microsoft.Windows.Controls.Samples.SearchSuggestionSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:clr="clr-namespace:System;assembly=mscorlib"
xmlns:common="clr-namespace:Microsoft.Windows.Controls.Samples;assembly=Microsoft.Windows.Controls.Samples.Common"
xmlns:controls="clr-namespace:Microsoft.Windows.Controls;assembly=Microsoft.Windows.Controls">
<UserControl.Resources>
<Style x:Key="SearchTextBoxStyle" TargetType="TextBox">
<Setter Property="Background" Value="#AAFFFFFF" />
<Setter Property="FontFamily" Value="Verdana" />
<Setter Property="FontSize" Value="18" />
<Setter Property="Padding" Value="2" />
</Style>
</UserControl.Resources>
<StackPanel>
<ContentControl Content="Live.com Search Suggestions" Style="{StaticResource Header}" />
<!-- AutoCompleteBox example -->
<Grid Width="650" Height="406" Background="{StaticResource SearchSuggestionsImageBrush}">
<StackPanel Height="34" Width="490" Orientation="Horizontal" VerticalAlignment="Top" Margin="80,119,80,0">
<TextBlock FontSize="18" FontFamily="Verdana" VerticalAlignment="Center"><Run Text="Live Search"/></TextBlock>
<Grid Width="360" Height="34" Margin="8,0,0,0">
<controls:AutoCompleteBox
SearchMode="None"
x:Name="Search"
TextBoxStyle="{StaticResource SearchTextBoxStyle}"
IsEnabled="False" />
<Button x:Name="Go" Cursor="Hand" IsEnabled="False" Width="32" Height="32" HorizontalAlignment="Right" BorderThickness="0,0,0,0" RenderTransformOrigin="0.5,0.5">
<Button.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX="0.85" ScaleY="0.85"/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Button.RenderTransform>
<Button.Template>
<ControlTemplate TargetType="Button">
<Grid Margin="0" x:Name="RootElement">
<Rectangle Margin="0" Fill="#FF549C00" Stroke="#FF000000" StrokeThickness="0" RadiusX="2.5" RadiusY="2.5"/>
<Ellipse Height="16.105" Margin="0,6.438,5,0" VerticalAlignment="Top" Stroke="#FFFFFFFF" StrokeThickness="2" Width="15.938" HorizontalAlignment="Right"/>
<Path Height="7.875" HorizontalAlignment="Left" Margin="7.754,0,0,6.467" VerticalAlignment="Bottom" Width="7.75" Fill="#FFFFFFFF" Stretch="Fill" Stroke="#FFFFFFFF" StrokeThickness="2" Data="M14.097251,19.182762 L8.7665224,24.552877"/>
</Grid>
</ControlTemplate>
</Button.Template>
</Button>
</Grid>
</StackPanel>
<!-- Hosting warning message -->
<StackPanel x:Name="HostingWarning" Background="#88000000">
<StackPanel Background="#aaffffff" Margin="6">
<TextBlock Margin="6" UseLayoutRounding="False" FontWeight="Bold">This sample must be hosted on a web server.</TextBlock>
<TextBlock Margin="6" UseLayoutRounding="False" TextWrapping="Wrap">A cross-domain web service call can only be made when the page is hosted from a server via the HTTP scheme. Consider hosting this using IIS, the built-in Web Development Server in Visual Studio, or another server technology.</TextBlock>
</StackPanel>
</StackPanel>
</Grid>
</StackPanel>
</UserControl></sys:String>
</src:SourceFile.Source>
</src:SourceFile>
<src:SourceFile Path="SearchSuggestionSample.xaml.cs">
<src:SourceFile.Source>
<sys:String>// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Json;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.ComponentModel;
namespace Microsoft.Windows.Controls.Samples
{
/// <summary>
/// A simple auto complete search suggestions sample that connects to a
/// real web service.
/// </summary>
[Sample("Search Suggestions", DifficultyLevel.Scenario)]
[Category("AutoCompleteBox")]
public partial class SearchSuggestionSample : UserControl
{
/// <summary>
/// Initializes a new instance of the SearchSuggestionSample class.
/// </summary>
public SearchSuggestionSample()
{
InitializeComponent();
Loaded += OnLoaded;
}
/// <summary>
/// Handles the Loaded event by initializing the control for live web
/// service use if the stack is available.
/// </summary>
/// <param name="sender">The source object.</param>
/// <param name="e">The event data.</param>
private void OnLoaded(object sender, RoutedEventArgs e)
{
if (WebServiceHelper.CanMakeHttpRequests)
{
HostingWarning.Visibility = Visibility.Collapsed;
Go.IsEnabled = true;
Search.IsEnabled = true;
Search.Populating += Search_Populating;
Action go = () => HtmlPage.Window.Navigate(WebServiceHelper.CreateWebSearchUri(Search.Text), "_blank");
Search.KeyUp += (s, args) =>
{
if (args.Key == System.Windows.Input.Key.Enter)
{
go();
}
};
Go.Click += (s, args) => go();
}
}
/// <summary>
/// Handle and cancel the Populating event, and kick off the web service
/// request.
/// </summary>
/// <param name="sender">The source object.</param>
/// <param name="e">The event data.</param>
private void Search_Populating(object sender, PopulatingEventArgs e)
{
AutoCompleteBox autoComplete = (AutoCompleteBox)sender;
// Allow us to wait for the response
e.Cancel = true;
// Create a request for suggestion
WebClient wc = new WebClient();
wc.DownloadStringCompleted += OnDownloadStringCompleted;
wc.DownloadStringAsync(WebServiceHelper.CreateWebSearchSuggestionsUri(autoComplete.SearchText), autoComplete);
}
/// <summary>
/// Handle the string download completed event of WebClient.
/// </summary>
/// <param name="sender">The source object.</param>
/// <param name="e">The event data.</param>
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Any failure in the Json or request parsing should not be surfaced.")]
private void OnDownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
AutoCompleteBox autoComplete = e.UserState as AutoCompleteBox;
if (autoComplete != null && e.Error == null && !e.Cancelled && !string.IsNullOrEmpty(e.Result))
{
List<string> data = new List<string>();
try
{
JsonObject jso = ((JsonObject)JsonObject.Parse(e.Result))["SearchSuggestion"] as JsonObject;
string originalSearchString = jso["Query"];
if (originalSearchString == autoComplete.SearchText)
{
foreach (JsonObject suggestion in (JsonArray)jso["Section"])
{
data.Add(suggestion.Values.First());
}
// Diplay the AutoCompleteBox drop down with any suggestions
autoComplete.ItemsSource = data;
autoComplete.PopulateComplete();
}
}
catch
{
}
}
}
}
}</sys:String>
</src:SourceFile.Source>
</src:SourceFile>
</src:SourceViewer>
</StackPanel>
</UserControl>