Posts Tagged 'WPF'

Connecting to Live Search using the HttpClient

Preview 2 of the WCF REST Starter Kit has been posted to CodePlex. Check it out at http://tinyurl.com/wcf-rest-kit2. Included in this latest release of the WCF REST Starter Kit are a great new set of client connection tools that make consumption of RESTful services a breeze.

At the forefront of these client connection tools is the HttpClient. This class allows quick and easy access to download any content from a url. I’ve created a simple WPF Application that demonstrates using the HttpClient and the Live Search API.  For more information on the LiveSearch API or to get your LiveSearch API Key goto http://dev.live.com/getstarted/

Click here to Download the Full Source Code

Windows Live Search WPF Application

This WPF Application consists of a Window with some code-behind,  and a LiveSearch.cs file that was created using the Paste as XML Type feature of the WCF REST Starter Kit.  Before you can serialize your results from the Live Search API you need to run the query in your browser and copy the xml results into your clipboard.  After that you can go into Visual Studio and create a file in your project called LiveSearch.cs and then go to Edit-> Paste as XML Type.  As for the WPF Window it has a Grid with 2 rows and 3 columns.  In the top row is a search textbox and two buttons.  In the second row is a ScrollViewer that contains an ItemsControl. 

The first button does a single threaded search call to the Live Api and sets the ItemsSource of the ItemsControl.  As you can see by the code below its very simple, first we construct the uri, then we call .Get on that uri.  The Get call returns an HttpResponseMessage.  The response message has a number of different chaining methods and extension methods,  we are using the EnsureStatusIsSuccess, which is equivalent to the EnsureStatusIs(HttpStatusCode.OK).  After we call EnsureStatusIsSuccessful (which returns the original message or an exception) we can read the content.  In this case we are reading the content as a xml serialized response of the type created above using the Paste as XML Type feature.

private void Search_Click(object sender, RoutedEventArgs e)
{
      string liveApi = "Your Live Api";
      string uriFormat = "http://api.search.live.net/xml.aspx?AppId={0}&Market=en-US&Query={1}&Sources=web&Web.Count=25";

      using (var client = new HttpClient())
      {
          string uri = string.Format(uriFormat, liveApi, this.SearchText.Text);

          var response = client.Get(uri).EnsureStatusIsSuccessful()
              .Content.ReadAsXmlSerializable<SearchResponse>();

          this.Results.ItemsSource = response.Web.Results;
      }
}

Since its not recommended to run potentially long running operation on the UI thread.  I created a second button to demonstate the Live Search feature using the SendAsync method.  Below is the code for that.

private void SearchAsync_Click(object sender, RoutedEventArgs e)
{
      string liveApi = "Your Live Api";
      string uriFormat = "http://api.search.live.net/xml.aspx?AppId={0}&Market=en-US&Query={1}&Sources=web&Web.Count=25";

      var client = new HttpClient();
      string uri = string.Format(uriFormat, liveApi, this.SearchText.Text);

      client.SendCompleted += client_SendCompleted;
      client.SendAsync(new HttpRequestMessage(HttpMethod.GET.ToString(), uri));
}

void client_SendCompleted(object sender, SendCompletedEventArgs e)
{
      var response = e.Response.EnsureStatusIsSuccessful()
              .Content.ReadAsXmlSerializable<SearchResponse>();

      this.Results.ItemsSource = response.Web.Results;
}

Feel free to download the full demo using the link above, I hope you enjoy it. The new client connect features of the WCF REST Starter Kit create a really simple way of communicating with third party REST services.

Advertisements

NativeFileTypeViewer : FileIconImageConverter – Convert a filename to the file system icon.

I’ve recently been creating more desktop applications and I often needed to print out the system icons for standard files.  In the past I had used Photoshop to crop out the icons and I ended up only supportting a limited number of file icons.  And as I was writing this post I also found a shell32 tatic  used to create ImageLists in WinForms.

In order to load the system file icons and descriptions for my WPF applications, I built this Binding Converter.  The FileIconImageConverter.cs file is an IValueConverter that accepts  a filename or full file path.  The Converter supports caching and both 32-pixel and 16-pixel icons.  With the exception of the cache checking functionality, this converter has 4 main parts. First it creates a blank 0kb text file the the same extension as the source filename.  Second it loads a SHFILEINFO Struct using Win32.SHGetFileInfo and creates an Icon from the handle. Third it creates Bitmap from the Icon and saves it to a MemoryStream as a PNG.  Finally it loads the ImageSource using a PngBitmapDecoder from the previous Memory Stream.

I would love to give credit to Atul Gupta’s Blog, which help me out a lot with this.  I haven’t tried it in Silverlight, please let me know if you try it.  Please see my website for the full code.

http://www.bendewey.com/code/FilenameIconImageConverter.html

This little application, called the NativeFileTypeViewer (source code), loads a directory and converts the filenames into the filename descriptions and the file icon, to demonstrate the use of the binding converters.

Native FileType Viewer

Swoosh Button

I created this little swoosh button, as I call it, as my main button design for most of my apps. There are three colors for now but it can easily be adapted to support multiple more. There is a standard blue color, a red color (i used for the cancel buttons), and a green color (I used for the OK and Next Buttons) . I also think the button looks really good with all caps text.

I keep trying to come up with a good way to pass the color in without creating a custom control. If anyone has any thought please feel free to share.

Enjoy

SwooshButtonBlueSwoosh

Usage

<Button Width=”60″ x:Name=”browse” Style=”{StaticResource SwooshButton}”>Browse</Button>

Code

http://www.bendewey.name/code/swooshButton.html


What else am I doing?

StackOverflow Facebook Twitter LinkedIn Live

Twitter

Pages

November 2017
M T W T F S S
« Feb    
 12345
6789101112
13141516171819
20212223242526
27282930