In the last couple of moths or so, I’ve come across a number of very exciting Silverlight hobby/fun projects. Most of them are interesting in one way or other, and each of them involve a lot of creativity and innovation, and carries a lot of inspiration. Here is my ‘Top 6’ list for your reference.

Farseer Physics Engine

Farseer is a nice 2D Physics engine for Microsoft Silverlight and XNA platforms. And hey, this one is not just a ‘fun’ project, Jeff and his team is pretty serious about this. Farseer components will let you add real world effects and behaviors like Spin, Rotate, Move etc, to your objects. You can use Farseer to create pretty advanced effects in your Silverlight games.

image

Where to get?

Windows Phone 7 Puzzle Game

I simply love this. A very nice Sokoban game for Windows Phone 7, in Silverlight, from Daniel. Apart from the game, the article he has on Code project is a good starter for anyone who want to start developing apps for WM7 in Silverlight.

image

Where to get?

Bugcam Smash

Bugcam Smash let you ‘smash’ bugs in your screen using your real fingertip pointed against the web camera. Bugcam smash contains some interesting code to do motion detection of your finger, and you can see bugs getting smashed in the screen if you correctly position your finger on a pest. Nice idea from Adam Kinney.

 

image

Where to get?

Facelight

Facelight is a very interesting project from RenĂ© Schule, that can do facial recognition using a web cam and Silverlight. The face detection algorithm is not the perfect one, but it works really cool. Basically, it’ll replace your ‘normal’ face with a monkey’s face :).

image

Where to get?

 

Silverlight Facebook Client

Hope you’ve already heard about this one. I’m listing this last because I don’t thinks the source code is available yet for this one. This Silverlight client for Facebook provides a very nice presentation of your face book content. The layout and visualization aspects are truly interesting.

image

Where to get?

 

Silver Draw

This one is a real-time white board that can sync information between various participants, using Silverlight + Polling Duplex. Silverlight + WCF Polling Duplex services enables you to write Silverlight apps that can share information almost real time between users, over HTTP.

Users can draw together in the white board, and may chat with each other. Did I tell you that I developed this? lol

 

image

Where to get?

Conclusion

Oh yea, just wanted to tell that Silverlight is pretty cool.

If you have/know about an extremely interesting application that I have not yet listed here, please leave a comment!! To have a look at the source code of most of these applications, you need to have Visual Studio 2010 + Silverlight Developer tools. You can get it for free if you wish.

Read my other posts on Silverlight as well. Also, do keep in touch, follow me in Twitter @amazedsaint or Subscribe to this blog

Shout it
Read more >>

I presented the topic WPF Control Architecture at Microsoft Virtual Tech days 2010, and talked about few cool aspects of WPF Controls. Also, demoed Wingy – A bare bone, less than one hour WPF client built for viewing Bing search results :).

The presentation covered the following concepts

  • Content models in WPF
  • Data Templates and Control Templates
  • Creating user controls and custom controls
  • Creating dependency properties

You should be able to download the presentation from Microsoft Virtual tech days website soon.

And don’t miss my other WPF and Silverlight Posts :). Check out

 

Happy Coding, Enjoy.

Shout it
Read more >>

Watching online the Mix 10 Keynote from Scott Guthrie. Here we go, the key take aways listed below.

Features

  • Windows Phone 7 Silverlight runtime is almost identical to the existing Silverlight runtime (well, almost – SL For WM7 still miss things like DLR) .
    • Supports media experiences
    • Supports deep zoom
  • WP7 initially will run a superset of Silverlight 3 with phone-specific extensions like accelerometer.
  • Scott Gu mentioned that Silverlight penetration now at 60% (was 45% during PDC, this is news for me)
  • Windows Phone 7 supports 'traditional' development platforms - Visual Studio 2010, Blend.
  • Windows Phone 7 has got multi touch features, Emulator can be used for testing using Windows 7 multi touch capabilities
  • Windows Phone 7 Development tools are Free (Did I hear that right?, yes see the link below)
    • Visual Studio 2010 for Windows Phone
    • XNA Dev Platform For Windows Phone
    • Expression Blend 4.0 For Windows Phone
  • Windows Phone 7 has support for Accelerometer, Positioning.
  • Awesome demos – Netflix client, Foursquare (Location awareness), Streaming, Cloud integration for Music Lookup, Seesmic for Mac via Silverlight
  • Silverlight 4.0 RC is announced
  • ‘Same’ XNA platform, tools and skillset - for XBox, Windows Phone 7 and Desktop
  • Target market for Windows Phone 7: 38yo, 76% employed and 73% partnered

Downloads, Links

  • Download Windows Phone 7 Developer tools – Click Here
  • Windows Phone 7 Series Developer Training Kit now available on Channel 9 – Click Here
  • A guide to what has changed in the Silverlight 4 RC – Click Here
  • Check out the Silverlight customer highlight video – Click Here
  • MSDN Link - Read up on developing for Windows Phone in MSDN Click Here
  • ‘Right Mix’ – A nice post from Windows Mobile team – Click Here
  • Posts on Blend 4  Here and Blend 4 for Windows Phone – Here
Shout it
Read more >>

C# 4.0 dynamic keyword for Dummies – Under the hood

In this post, we’ll go back to the basics to uncover few interesting aspects of C# 4.0 dynamic features. Let us start with a simple experiment.

Static Typing or Early Binding

Let us start with a bare minimum program. Fire up VS2010 and try the following code.

image

It is obvious that this won’t compile. Simply because, the compile/design time checking ensures type safety - and as we don’t have a method named SomeStupidCall in our Human class, we can’t compile the same.

image

So, that is what you get.

Duck Typing or Dynamic Typing or Late Binding

Now, let us take a step back, and modify the above code like this. Change the type of variable h to ‘dynamic’. And Compile. Here is a little surprise!! You got it compiled!!  By using ‘dynamic’ keyword, you just told the compiler, “dude, don’t bother if SomeStupidCall() is there in the Human type

image

And now, run the application. The application will break for sure. But hey, that is your fault, not the compiler’s. 

image

By the way, why we call dynamic typing as ‘Duck typing’?

image

Here we go, Quoted from Wikipedia

In duck typing, one is concerned with just those aspects of an object that are used, rather than with the type of the object itself. For example, in a non-duck-typed language, one can create a function that takes an object of type Duck and calls that object's walk and quack methods. In a duck-typed language, the equivalent function would take an object of any type and call that object's walk and quack methods. If the object does not have the methods that are called then the function signals a run-time error. It is this action of any object having the correct walk and quack methods being accepted by the function that evokes the quotation and hence the name of this form of typing.

Now, why you need ‘dynamic’ at all?

I hope the above scenario is ‘good’ enough to give you an ‘evil’ impression about the dynamic features. But, wait. There are lot of scenarios where the dynamic features can really simplify things for you. For example, let us assume a reflection based scenario, where you load a type (from an external assembly or so), to invoke a member. Here is a quick example. You’ll see how to use dynamic as an easier alternative for reflection. This is much more evident when you deal with scenarios like COM interop etc. We’ll see more interesting uses later.

image

How a ‘dynamic’ type is getting compiled?

Let us get back to a basic example. Consider a human class, with a Walk() method and Age property. Now, let us create a new Human and assign this to a dynamic variable ‘h’.

image

Let us build the above application. Fire up Reflector, open the binary executable of the app, and have a look at the same. With a bit of cleanup, this is the relevant part of the disassembled code - which is the equivalent 'statically’ typed code generated by the compiler, for the ‘dynamic’ code we wrote above.

You’ll see a ‘SiteContainer’, for keeping track of the context, and two call site fields.

image

And here is the disassembled code for the member invocations. It might be interesting to note that, the variable ‘h’ is compiled as a simple CLR ‘object’. Wow, from the CLR point of few, there is nothing like a ‘dynamic’ type. Instead, all member invocations to our ‘dynamic’ object are modified; in such a way that they are piped through a dynamic Call Site. The Call Site is initialized with the Binder responsible for run time binding. In this case, you may note that the C# run time binder will be used to invoke the members(Microsoft.CSharp.RuntimeBinder.Binder ‘s InvokeMember will return a CallSite binder, that’ll be used by the Call Site).

image

You might be thinking how the code will be generated for scenarios where, you have a method that accepts or returns a ‘dynamic’ type, or a property that gets or sets a dynamic type. Let us try this out. Change our above Human class in the above example, to something like this. Note that now Walk method accepts a dynamic type, and Age property is also modified to get/set dynamic types.

image

Have a sneak peak using Reflector. You’ll note that, the compiler has generated a special [Dynamic] attribute to the input parameter of Walk() method. Also, the getter and setter of the Age property is also decorated with the [Dynamic] attribute, as shown below.

image 

Conclusion

A good follow up read is my post on ExpandoObject, which explains how to define which operations can be performed on dynamic objects and how to perform those operations. For example, you can define what happens when you try to get or set an object property, call a method, or perform standard mathematical operations such as addition and multiplication.

Also, A more interesting and useful dynamic concept I’ve recently implemented is ElasticObject, a fluent DynamicObject implementation, to deal with data formats like XML in a very easy manner.

And here is some code of the final form of our experiment. Create a Console app in C# 4.0, to play with this.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.CompilerServices;
using Microsoft.CSharp.RuntimeBinder;

namespace DynamicTest
{
    class Human
    {
        public void Walk(dynamic place)
        {
            Console.WriteLine("I'm walking to " + place);
        }
        public dynamic Age { get; set; }
    }

    class Program
    {
          // Methods
    private static void Main(string[] args)
    {
        //dynamic h = new Human();
        //h.Walk("Paris");
        //h.Age = 10;

        //will get compiled to

        object h = new Human();

        //Create the site 1 if it is null
        if (SiteContainer0.Site1 == null)
        {
            SiteContainer0.Site1 = CallSite<Action<CallSite, object, string>>.Create
                (Binder.InvokeMember(CSharpBinderFlags.ResultDiscarded, "Walk", 
                null, typeof(Program), new CSharpArgumentInfo[] 
                { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null), 
                  CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.LiteralConstant 
                  | CSharpArgumentInfoFlags.UseCompileTimeType, null) }));
        }
        SiteContainer0.Site1.Target(SiteContainer0.Site1, h, "Paris");

        //Create the site 2 if it is null
        if (SiteContainer0.Site2 == null)
        {
            SiteContainer0.Site2 = CallSite<Func<CallSite, object, int, object>>.Create
                (Binder.SetMember(CSharpBinderFlags.None, "Age", typeof(Program), 
                new CSharpArgumentInfo[] 
                { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null), 
                    CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.LiteralConstant | 
                    CSharpArgumentInfoFlags.UseCompileTimeType, null) }));
        }
        SiteContainer0.Site2.Target(SiteContainer0.Site2, h, 10);
    }
    

    // Nested Types
    [CompilerGenerated]
    private static class SiteContainer0
    {
        // Fields
        public static CallSite<Action<CallSite, object, string>> Site1;
        public static CallSite<Func<CallSite, object, int, object>> Site2;
    }

    }

}

Enjoy, Happy Coding!! Do keep in touch, follow me In Twitter @amazedsaint Or Or Subscribe this Blog

Shout it
Read more >>

Euler’s Problem 7 And A PrimeAt Extension Method For C#

This is one of those Back To Basics Post.

It is always interesting to work on Euler’s problems. Recently, Roman wanted to bring some fun, and asked us to implement Euler’s Problem 7 in C# in an effective way.

Euler’s problem 7 is pretty straight forward – Find the 10001st Prime Number :). So, here we go, here is a pretty minimal implementation with a bit of optimization.

public static class PrimeUtils
    {
        
        /// <summary>
        /// An extension method to find the prime number at n'th position
        /// </summary>
        /// <param name="position"></param>
        /// <returns></returns>
        public static int PrimeAt(this int position) 
        {
            var primes = new List<int>() { 2 }; var num = 1;
            while (primes.Count < position)
            {
                bool isPrime = true; num += 2;
                foreach (int p in primes)
                {
                    if (num % p == 0) { isPrime = false; break; }
                    if (p > (int)Math.Sqrt(num) + 1) break;
                }
                if (isPrime) primes.Add(num); 
            };
            return num;
        }
    }
And here is how you can use it.  

image

Read few other Back To Basics posts if you are interested

Shout it
Read more >>

top