Tag Archives: silverlight

IronNails : Rails like development for IronRuby with WPF/Silverlight

For my book IronRuby I’m working on chapter 4. That chapter is about doing WPF development with IronRuby. I started out with a straight port of Witty to IronRuby. As I was doing that the cogs started turning and I came up with a way to bring the rails style of development to WPF.   I decided to investigate that route a little bit further and now I have a small framework that enables you to write WPF applications with the MVC paradigm. I decided to open that code up as open source and host it on github. 

At first I used the name Sails for my framework but it turns out there is java clone of rails that is called opensails. So to avoid confusion David M. Peterson proposed the name IronNails.

On Sat, 02 Aug 2008 08:00:44 -0600, Charles Oliver Nutter wrote:

    FYI, there’s already a framework named "Sails" for Java:
http://www.opensails.org/

For the sake of sticking to the "Iron" theme, why not replace the ‘S’ with an ‘N’ and go with IronNails. ;-)   Maybed it’s just me, but if given the choice, I’d much rather nail it than sail it any day of the week. :D


/M:D

M. David Peterson

You can find the project here: http://github.com/casualjim/ironnails

At this moment it’s definitely not finished at all, but it does work. The remainder of the week I’ll move my previous demo code onto this framework, update the code samples in my chapter and finish the content. I hope I will have all of this done by the end of next week.

Back to the IronNails project:

Because DLR objects cannot be used to bind to in WPF you have to define a skeleton of the ViewModel in C#, but this will change in the future. When that changes I’ll look at extending the framework to make use of some other WPF patterns like defining a DependencyObject and Behaviors. Once those are defined you get a very clean separation between design and behavior.

This is abstracted away from you but in the background the framework works with the View – ViewModel – Model – Controller pattern although I’ve tried to keep your exposure to the view model to a minimum. The framework follows naming conventions per language. So in C# and XAML you camel case stuff and in IronRuby you underscore stuff.

The very core of the framework is defined in C# but most of the code is IronRuby, depending on how hard it will be after the DLR RTM’s I may look at adding support for all the DLR languages.

IronNails
=========

IronNails is a framework inspired by the Rails and rucola frameworks. It offers a rails-like way of developing
applications with IronRuby and Windows Presentation Foundation (WPF).
This framework uses the pattern Model – ViewModel – View – Controller (M-VM-V-C). It should be able to run on both WPF
and Silverlight.
The idea is that the views can be created using a design tool like Blend for example and just save that xaml as is. The
designer should not need to use anything else than drag and drop to create a GUI design. The behaviors are then added to
the view by using predefined behaviors in the framework or by defining your own behavior.
The framework then generates a proxy for the view which will be used to transparently wire up the commands in the
behaviors to controller actions.

You are now able to write the following code for a controller:

class MyController < IronNails::Controller::Base

  view_action :show_message, :triggers => :my_button do
    MessageBox.show "This is the great message from a block"
  end

  view_action :change_color, :triggers => { :element => :my_text_block, :event => :mouse_enter }
  view_action :reset_color, :triggers => { :element => :my_text_block, :event => :mouse_leave } do |view|
    view.my_text_block.foreground = :black.to_brush
  end

  view_object :people, Person.find_all

  def change_color(view)
    view.my_text_block.foreground = :red.to_brush
  end

end

At this moment the project has 0 unit tests, it has below minimal documentation and it still needs a work like defining the behaviors. I have to move on with my book but intend to continue developing this framework after my book is finished and IronRuby RTM’s.  There are some more workarounds in the project that will all disappear as IronRuby progresses.

kick it on DotNetKicks.com

An IronRuby Digg Client

Scott Guthrie has a series of tutorials on how to consume the Digg API with Silverlight and C#. I wanted to know if I could convert that tutorial to IronRuby.

What I came up has the same end result but due to the fact that IronRuby and its Silverlight implementation isn’t complete yet I can’t mimic the structure of that tutorial yet. I couldn’t yet figure out how to load external xaml files so I put all the xaml in one file.

I have one C# file that resides in the lib folder, this file is called DigStory.cs and contains a DiggStory class which represents a story on digg. It uses XLinq to parse the xml document and return a list of stories.

Download the complete code

The code contained in that file:

public class DiggStory
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public int NumDiggs { get; set; }
    public Uri HrefLink { get; set; }
    public string ThumbNail { get; set; }
    public string UserName { get; set; }

    public static IEnumerable ParseFeed(string feedContent)
    {
        var diggFeed = XDocument.Parse(feedContent);

        var result = from story in diggFeed.Descendants("story")
                     where story.Element("thumbnail") != null && !story.Element("thumbnail").Attribute("src").Value.EndsWith(".gif")
                     select new DiggStory
                     {
                         Id = (int)story.Attribute("id"),
                         Title = ((string)story.Element("title")).Trim(),
                         Description = ((string)story.Element("description")).Trim(),
                         ThumbNail = (string)story.Element("thumbnail").Attribute("src").Value,
                         HrefLink = new Uri((string)story.Attribute("link")),
                         NumDiggs = (int)story.Attribute("diggs"),
                         UserName = (string)story.Element("user").Attribute("name").Value,
                     };

        return result;
    }
}

I won’t post the xaml file because it’s a bit too long. I will however post the app.rb file and offer a little bit more explanation on that file.

The app.rb file:

require "System"
require "Silverlight"
require "System.ServiceModel.Web"

require "Contract"

include System
include System::Net
include System::Windows

class App < SilverlightApplication
  use_xaml

  def get_topic_url(topic)
	  "http://services.digg.com/stories/topic/#{topic}?count=20&appkey=http%3A%2F%2Fflanders.co.nz%2Fblog"
  end

  def initialize

	  search_button.click do |sender, e|
		topic = search_textbox.text

		client = WebClient.new
		client.download_string_completed do |sender, args|
			return unless args.error.nil?

			stories = DiggStory.parse_feed(args.result);
			stories_list.selected_index = -1;
			stories_list.items_source = stories;
		end
		client.download_string_async Uri.new(get_topic_url(topic))

	  end

	  stories_list.selection_changed do |sender, args|
		  story = stories_list.selected_item

		  story_detail.data_context = story;
		  story_detail.visibility = Visibility.Visible
	  end

	  close_button.click do |sender, args|
		story_detail.visibility = Visibility.Collapsed

	  end
  end
end

App.new

The above code contains all the code necessary for our application.

We first tell the application to use the app.xaml file. Next we define a method that builds us the request url for the digg api. I chose to use the xml format to stay as close as possible to the sample of Scott.

Like in the C# sample we’re also downloading the file asynchronous. In the callback for when the request completes we parse the returned string into a list of stories and set that as the item source of our Listbox in the app.xaml file.

We handle the selection_changed event of the ListBox to display a detail view on a story with a link to the digg page.

More explanation on the xaml can be found on Scotts blog.

Make sure you get the bits you need from the dynamic silverlight website needed for running dynamic silverlight applications.

Put the path to chiron.exe in your PATH variable and extract the contents of the downloaded sample. open a command prompt and go to the location where you extracted the files to. type chiron /b and click on index.html

Download the complete code

kick it on DotNetKicks.com