BlogEngine and Silverlight

I've had a few people asking me how did I host Silverlight 3 in blog engine.

Silverlight Usercontrol

It was pretty simple really (when you know how)

Basically Silverlight3 needs an <object> tag.

So I created a user control with the object tag and some script handling for client side errors (actually VS2010 did all the hard lifting).


<%@ Control Language="C#" ClassName="SilverlightControl" %>

<script runat="server">


<script type="text/javascript" src="~/Silverlight.js"></script>
    <script type="text/javascript">
        function onSilverlightError(sender, args) {
            var appSource = "";
            if (sender != null && sender != 0) {
                appSource = sender.getHost().Source;

            var errorType = args.ErrorType;
            var iErrorCode = args.ErrorCode;

            if (errorType == "ImageError" || errorType == "MediaError") {

            var errMsg = "Unhandled Error in Silverlight Application " + appSource + "\n";

            errMsg += "Code: " + iErrorCode + "    \n";
            errMsg += "Category: " + errorType + "       \n";
            errMsg += "Message: " + args.ErrorMessage + "     \n";

            if (errorType == "ParserError") {
                errMsg += "File: " + args.xamlFile + "     \n";
                errMsg += "Line: " + args.lineNumber + "     \n";
                errMsg += "Position: " + args.charPosition + "     \n";
            else if (errorType == "RuntimeError") {
                if (args.lineNumber != 0) {
                    errMsg += "Line: " + args.lineNumber + "     \n";
                    errMsg += "Position: " + args.charPosition + "     \n";
                errMsg += "MethodName: " + args.methodName + "     \n";

            throw new Error(errMsg);

    <div id="silverlightControlHost">
    <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
    <param name="source" value="<%Silverlight.xbap%>"/>   // where <%Silverlight.xbap%> is location of silverlight xbap
    <param name="onError" value="onSilverlightError" />
    <param name="background" value="white" />
    <param name="minRuntimeVersion" value="3.0.40818.0" />
    <param name="autoUpgrade" value="true" />
    <a href="" style="text-decoration:none">
      <img src="" alt="Get Microsoft Silverlight" style="border-style:none"/>
     </object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe></div>


Windows Forms Validation

One approach for validating child controls on windows froms is to


  • Add a Validated event handler to all child controls you're interested in.
  • On a button event handler call this.ValidateChildren();

This will ensure the validation routine on all child controls will be called, if for example you've added an ErrorProvider control extender to your form you can set it up in the validated event handlers.

Inline XML Linq manipulation sample

A quick example of inline xml and Linq




void LinqToXmlSample()
  Console.WriteLine("{0} : Start", new StackTrace(0, true).GetFrame(0).GetMethod().Name);

  XDocument xDocument = new XDocument(
    new XElement("people",
      new XElement("person",
        new XAttribute("sex", "male"),
        new XElement("FirstName", "Brian"),
        new XElement("LastName", "Keating")),
      new XElement("person",
        new XAttribute("type", "female"),
        new XElement("FirstName", "Dustin"),
        new XElement("LastName", "Turkey"))));

  IEnumerable<XElement> elements =

  /*  First, I will display the source elements.*/
  foreach (XElement element in elements)
    Console.WriteLine("Source element: {0} : value = {1}",
      element.Name, element.Value);

  /*  Now, I will display the ancestor elements for each source element.*/
  foreach (XElement element in elements.Ancestors())
    Console.WriteLine("Ancestor element: {0}", element.Name);

  Console.WriteLine("{0} : Finish", new StackTrace(0, true).GetFrame(0).GetMethod().Name);




LinqToXmlSample : Start
Source element: FirstName : value = Brian
Source element: FirstName : value = Dustin
Ancestor element: person
Ancestor element: people
Ancestor element: person
Ancestor element: people
LinqToXmlSample : Finish


[Update August 2012: Little did I know when i wrote this post just how much I would use Linq to xml, it saved me from a life of xslt Smile ]

Resource Cleanup and Lambda Expressions

A neat way of always cleaning up resources is to use Lambdas as data.

Take the following



    internal interface ITryCatchReport
        void Try(Action<IServer> action);

    internal class TryCatchReport : ITryCatchReport
        public TryCatchReport(IServer server)
            _server = server;

        public void Try(Action<IServer> action)
            catch (Exception e)
                // Clean up resources
                // Report errors

        private IServer _server;    






TryCatchReport safeInvoker = new TryCatchReport(_data.Server);
safeInvoker.Try(x =>


We are now guaranteed that in the case of an exception that the resources will get cleaned up.


Usage with code blocks

If you wish to execute many statements in the action look at this sample.


private List<WFActionDefinition> GetActionDefinitions()
    if (_actionDefinitions == null)
        safeInvoker.Try(x =>

    return _actionDefinitions;


Visual Studio Project Dependencies

Adding project dependencies on Visual studio is done automatically with .NET projects when Add Reference dialog chooses another project from your solution.

However you can get Visual Studio to add project dependencies if no explicit intra project reference exists.

Right click on your solution, choose properties and choose the project dependencies option.


Blend Button Design

Here's a video from Steve White, a program manager in expression blend.

He shows how to Style a button in Silverlight using expression blend 3. Button Control Styling Tips/Default.html


Windows Workflow 4.0

Ok, forget what you learned about workflow in 3.5.

Windows Workflow 4.0

4.0 changes everything you thought you knew about workflow.

WF 4.0 represents a bottom-up rewrite with entirely new thinking. The gains are enormous: custom activities take centre stage, and authoring them is much simpler; workflows are entirely declarative; and there are three workflow flow styles that you can combine seamlessly. It's possible that you could see a 10-fold improvement in the time required to create and debug workflows, in addition to 10- to 100-fold runtime performance improvements. You'd also have better control over persistence. Additionally, there's support for partial trust, a completely rewritten designer, and a better debugging experience. One of the most important improvements is in the re-hosting experience. This allows you to create activities and to allow power users -- and others -- to build or modify workflows with the constrained set of activities you've created. Yes, this is possible in WF 3.0/3.5, but the necessary code is challenging, not to mention exceedingly difficult to debug.

The improvements to WF 4.0 come at a cost. Microsoft developed two inter-op strategies to help you over this hurdle. WF 3.0/3.5 will remain part of the framework and will run side by side with WF 4.0. This lets you manage the transition at a time that fits your organization's broader goals. The other inter-op strategy is an inter-op activity that allows you to use WF 3.0/3.5 activities in a WF 4.0 workflow. Workflows are a specialization of activities, so you'll probably be able to run entire 3.0/3.5 workflows inside WF 4.0 workflows.


Screenshot VS2010 B2


Testing Silverlight

My first (public silverlight application)..

Had quite a bit of fun getting this Silverlight 3.0 application hosted in Blog Engine .. More to come.. time for some sleep now.. 02:20am...


ERROR - UNABLE TO LOAD CONTROL : ~/User controls/SilverlightControl.ascx

Test syntax highlight

Testing the syntax highlighting option with the blog engine.

int test = 0;


Hello and welcome.
So I've started blogging... lets see how long this lasts

Why the change?

About 5 months ago I started writing my own Blog engine,

This was a great idea when the only thing (.NET wise) that I wanted to improve upon was my ASP.NET related coding.

My UX background started out with MFC, WTL and then rapidly progressed onto windows forms when I first got my hands on a beta version of .NET 1.0.; however my progress with ASP has been slower than expected as I've never worked for an ASP house, yes I come up with some useful apps that I run on intranets but they are nearly all a labour of love..

As I'm also interested in WPF/Silverlight/WCF/.NET 4.0, basically anything MS related... I find there are not enough hours in the day ..

So what I really needed was a blog engine where I could blog my findings with these new technologies along with other findings .NET related.

Think that's a longwinded way of saying I got lazy and downloaded a ready made solution.  BlogEngine.NET.

Don't expect me to offer full blown solutions and projects with each blog item.. Remember "there are not enough hours in the day..."

Recent Tweets

Note: For Customization and Configuration, CheckOut Recent Tweets Documentation


<<  March 2018  >>

View posts in large calendar

Month List