[Sample of Mar 9th] Use Silverlight fragment navigation to perform a search - Microsoft All-In-One Code Framework - Site Home - MSDN Blogs
Microsoft All-In-One Code Framework - Developers' Pain Killer

[Sample of Mar 9th] Use Silverlight fragment navigation to perform a search

Rate This
  • Comments 1

 

Homepage image
Sample of the Day RSS Feed

Sample Download:
C# version: http://code.msdn.microsoft.com/CSSL4FragmentSearch-abb6a266
VB version: http://code.msdn.microsoft.com/VBSL4FragmentSearch-2e8b4c6d

Today’s code sample demonstrates the use of fragment navigation within Silverlight to perform a search.  The advantage this provides is that the search can then be saved by URL, and linked directly, with consistent behavior, allowing users to send links to direct results within Silverlight.

image

The sample was written by Microsoft Senior Escalation Engineer - Jon Burchel.

imageYou can find more code samples that demonstrate the most typical programming scenarios by using Microsoft All-In-One Code Framework Sample Browser or Sample Browser Visual Studio extension. They give you the flexibility to search samples, download samples on demand, manage the downloaded samples in a centralized place, and automatically be notified about sample updates. If it is the first time that you hear about Microsoft All-In-One Code Framework, please watch the introduction video on Microsoft Showcase, or read the introduction on our homepage http://1code.codeplex.com/.

 

Introduction

This is a simple sample demonstrating the use of fragment navigation within Silverlight to perform a search.  The advantage this provides is that the search can then be saved by URL, and linked directly, with consistent behavior, allowing users to send links to direct results within Silverlight.

 

Running the Sample

To run the sample, press ctrl + F5 start the project. The application will show a popup box, click "yes" to continue, a web page will load, with the Silverlight fragment navigation's search component.  Input your text in TextBox and click search button, and a Bing search filtered on Microsoft.com will be performed with the search terms.  Note that the URL string will now include the search terms as a fragment, which can subsequently be bookmarked or copied and pasted into a new browser tab, after which time the same search will again be performed (as long as the URL remains valid - in the debug mode, as long as the project continues to run on the same port).

image

 

Using the Code

The sample uses one simple Silverlight component, and all the relevant code is in MainPage.xaml, and its code behind, MainPage.xaml.cs.  The code simply implements a fragment navigation event, which performs a web search with the Bing web service API, applying the fragment text as the search terms.

Following steps below to create this sample:

Step 1. Create a VB "Silverlight Application" in Visual Studio 2010 or Visual Web Developer 2010. Name it as "VBSL4FragmentSearch".

Step 2. Create a Service Reference folder and add Bing service.

Step 3. Add Sdk Frame with StackPanel, TextBlock, ListBox controls in MainPage, you can refer to MainPage.xaml page.

Step 4  Add VB code in MainPage.xaml.cs page as shown below:

Partial Public Class MainPage 
    Inherits UserControl 
    Dim results As New ObservableCollection(Of WebResult)() 
    Public Sub New() 
        InitializeComponent() 
        SearchResults.ItemsSource = results 
    End Sub 
  
    Private Sub Frame_FragmentNavigation(ByVal sender As Object, ByVal e As System.Windows.Navigation.FragmentNavigationEventArgs) 
        results.Clear() 
  
        Dim sr As New Bing.SearchRequest() 
        sr.Query = Convert.ToString(e.Fragment) & " (site:microsoft.com)" 
        sr.AppId = "1009092976966EFB6DD6B0F0B98FE5E617990903" 
        sr.Sources = New SourceType() {SourceType.Web} 
        sr.Web = New Bing.WebRequest() 
        Dim bing As Bing.BingPortTypeClient = New BingPortTypeClient() 
        AddHandler bing.SearchCompleted, AddressOf bing_SearchCompleted 
        bing.SearchAsync(sr) 
    End Sub 
  
    Private Sub bing_SearchCompleted(ByVal sender As Object, ByVal e As SearchCompletedEventArgs) 
        If e.Result.Web.Results IsNot Nothing Then 
            For Each wr As WebResult In e.Result.Web.Results 
                results.Add(wr) 
            Next 
        End If 
    End Sub 
  
    Private Sub Button_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) 
        ContentFrame.Navigate(New Uri("#" + SearchText.Text, UriKind.Relative)) 
    End Sub 
  
    Private Sub Link_MouseLeftButtonDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs) 
        Dim uri As New Uri(TryCast(TryCast(sender, StackPanel).Tag, String)) 
        System.Windows.Browser.HtmlPage.Window.Navigate(uri) 
    End Sub 
End Class

Step 5. Build the application and you can debug it.

 

More Information

For more information about Silverlight fragment navigation, see
http://msdn.microsoft.com/en-us/library/system.windows.controls.page.onfragmentnavigation%28VS.95%29.aspx.

Bing API, Version 2
http://msdn.microsoft.com/en-us/library/dd251056.aspx

Leave a Comment
  • Please add 8 and 4 and type the answer here:
  • Post
Page 1 of 1 (1 items)