Tips for using Silverlight with Monorail (or embedded)

Today I worked on getting a Silverlight chart embedded in my monorail application. I learned a few things.

You need to have a Silverlight application project someplace in order to create the XAP file. This is what will ultimately get used in your view.

Since it’s really running client side, the XAP files are included in your other projects like other client site stuff, that is just manually copied into a directory there.

Databinding to XML doesn’t seem to work, I think I must be doing something wrong, but I finally ended up using Linq to transfer the data into objects and bind to those. Like this:

series.ItemsSource = from n in descendants</p>

                 select <span class="kwrd">new</span> Data
                                Date = n.Attribute(<span class="str">"Date"</span>).Value,
                                Count = <span class="kwrd">int</span>.Parse(n.Attribute(<span class="str">"Responded"</span>).Value)

series.IndependentValueBinding = new Binding(“Date”) { Mode = BindingMode.OneWay };
series.DependentValueBinding = new Binding(“Count”) { Mode = BindingMode.OneWay };

public class Data

<span class="kwrd">public</span> <span class="kwrd">string</span> Date { get; set; }
<span class="kwrd">public</span> <span class="kwrd">int</span> Count { get; set; }


In your View you use embed the XAP file using the object tag like:

<object data="data:application/x-silverlight," type="application/x-silverlight-2" width="100%" height="100%">
    <param name="source" value="/assets/Charts.xap"/>
    <param name="background" value="white" />
    <param name="minRuntimeVersion" value="2.0.31005.0" />
    <param name="autoUpgrade" value="true" />
    <a href="" style="text-decoration: none;">
    <img src="" alt="Get Microsoft Silverlight" style="border-style: none"/>

This is really my first time checking out Silverlight, so I'm sure I'm missing a lot and have a ton left to learn. There is a bunch of great info on the Silverlight site.

Selecting Aggregate Data With Castle.ActiveRecord

Lets say your using Castle’s ActiveRecord and you have a complex query to aggregate some data, but you want that query to return the data in a typed class, just not one that NHibernate or ActiveRecord know about…

Here’s what I ended up doing. Creating the SQL query then using the HqlBasedQuery class. There are two important details regarding this, first the query needs to name the columns using the SQL as keyword and setting the Scalar definition. Second, you need to set a transformer using the SetResultTransformer method. Luckily, NHibernate includes a class that will map named scalars to matching properties or fields. More about that here, in section 14.1.5.

Here’s the code:

var sql = @“
select convert(varchar, published, 101) as [Date],
count(state) as [Total],
sum(case when state = 2 then 1 else 0 end) as [Responded]
from notices n
inner join sources s on n.sourceid = and s.userid = :userid
where published >= dateadd(day, -7, getdate())
group by convert(varchar, published, 101)”;</p>

HqlBasedQuery q = new HqlBasedQuery(typeof (Notice), QueryLanguage.Sql, sql); q.AddSqlScalarDefinition(NHibernateUtil.String, “Date”); q.AddSqlScalarDefinition(NHibernateUtil.Int32, “Total”); q.AddSqlScalarDefinition(NHibernateUtil.Int32, “Responded”); q.SetParameter(“userid”, Id); q.SetResultTransformer(Transformers.AliasToBean(typeof(NoticeCount)));

var results = ActiveRecordMediator.ExecuteQuery(q) as ArrayList; if (results == null)

<span class="kwrd">return</span> <span class="kwrd">new</span> NoticeCounts();

var counts = results.ToArray(typeof (NoticeCount)) as NoticeCount[];
return new NoticeCounts { Counts = counts };

IDictionary and the Brail View Engine

I spent a lot of time trying to figure out that the brail engine assumes IList on indexed properties. If you have a IDictionary you need to use the get_Item method like:





Changes Needed for Castle.MonoRail to use jQuery

To get jQuery as the JS library for monorail I need to do the following:

  1. Get the JS files from jQuery and jQuery.Validation, save those to /Content/Js/ (or where ever).

  2. Implement the IMonoRailContainerEvents and IMonoRailConfigurationEvents interfaces on my GlobalApplication.cs file.

  3. In the Initialized method add:

IAjaxProxyGenerator ajaxProxyGenerator = new JQueryAjaxProxyGenerator();
container.ServiceInitializer.Initialize(ajaxProxyGenerator, container);
container.AjaxProxyGenerator = ajaxProxyGenerator;
  1. In the Configure method add:
configuration.JSGeneratorConfiguration.AddLibrary("jquery-1.2.1", typeof(JQueryGenerator))
  1. In the layout file reference the JS files like:
<script language="javascript" type="text/javascript" src="${siteRoot}/Content/Js/jquery-1.2.6.min.js"></script>
<script language="javascript" type="text/javascript" src="${siteRoot}/Content/Js/jquery.validate.min.js"></script>


  1. Use the Castle trunk source.


Publishing video as FLV using a SWF Player

This task turned out to be much more complicated then I would have expected. First just creating the video took a fair amount of time, trial, and error. I used CamStudio to capture the screen video and then the trial of Video Editor 8.

After that was done I ended up using Any Video Converter to convert the file to FLV. Then I needed a SWF player to embed in the web page. I looked at a couple of options, but ended up with OS Flv. Once that was done and working, I ran into one last issue, resolved here. IIS doesn’t serve FLV files by default, so I needed to add the mime type.

Awesome Child Bike Seat – iBert [off topic]

This is off topic. We recently started using an iBert child seat to take our son on bike rides with us. It’s a front mounted seat, which makes it really easy to control the bike, plus the child gets a better view. Our son loves it and even puts up with a helmet on it.  You need to make sure it will mount on your bike, mountain and road bikes may not work. It requires an inch or two of open stem space on the neck of the bike. I think most hybrid bikes will work, but make sure you double check. We bought it from amazon and so far we’re all loving it!

Get the selected radio button value using prototype

Assuming you have a set of radio buttons with the same id, you can use the following to get the value of the selected/checked button.

var value = null;
$$(‘#plan_plan’).each( function(i) {</p>

<span class="kwrd">if</span> ($F(i)) { value = $F(i); }

} );

Brutalist Framework