Oct 21

Almost 90% of the samples in this blog are working again. Some samples simply doesn’t work in IE but ok in Safari, I will check over it tomorrow.

By the way, the update is very smooth and easy. I just open the old project with Visual Studio and compile it again. That is. I am satisfied with Silverlight this time.

The Gallery will also be updated by the end of this week.

Oct 18

Today, I try to upgrade my first sample in this blog to Silverlight 2. Out of my expectation, the code work perfectly and actually I can run the application directly after copy and paste the files.

Anyway, there are some interesting findings when updating the code. I will try to list them all during the upgrade.

Comparison

What’s the difference?

  • DispatcherTimer vs CompositionTarget.Rendering

Source codes

Silverlight

DispatcherTimer vs CompositionTarget.Rendering

In Silverlight 2, it’s much simpler to simulate the  enter frame event by using CompositionTarget.Rednering. It enables you to modify the frame rate easily as well.

// C#
// New Method
Application.Current.Host.Settings.MaxFrameRate = fps;
CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);

void CompositionTarget_Rendering(object sender, EventArgs e)
{
	// do your stuff here
}

// Old Method
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0, 0, 0, 0, 1000/fps);
timer.Tick +=new EventHandler(_timer_Tick);
timer.Start();

void  timer_Tick(object sender, EventArgs e)
{
	// do your stuff here
}
Oct 16

Today, I installed and uninstalled the Silverlight Tool many times. It was because I couldn’t open Silverlight Project after updating all the patches. The Visual Studio always show this message “The project type is not supported by this installation?”.  But finally, I found that I can actually use the Visual Web Developer 2008 to create Silverlight Application.

Does it mean that I can’t use Visual Studio anymore? I am not sure. Since I can continue my Silverlight development, I don’t care anymore.

By the way, the sample below is my first Silverlight 2 application. The logic is a little bit advanced, hope you will like it.

Lastly, I got a good news for all of you. I am going to update all the sample in my blog. Hopefully, all the samples should be ready be the end of this month.

Vote for this sample

Flash is Better! (329 votes)
Silverlight is Better? (298 votes)

Comparison

Flash implementation: 35 minutes
Silverlight implementation: 80 minutes (Implemented First)
What’s the difference?

  • HSL to RGB [AS3] vs [C#]

Source codes

Flash

Silverlight

HSL to RGB [AS3] vs [C#]

If you are going to implement drawing application, then you would probably need to handle HSL and RGB value. Below is a function that convert HSL to RGB. If you want to convert RGB to HSL, download and read the sample code above.

// AS3
// Convert HSL to RGB
public function hslToRgb(H : Number, S : Number, L : Number) : Array {
	var p1:Number;
	var p2 : Number;
	var rgb : Array = new Array(3);

	if (L<=0.5) p2 = L*(1+S);
	else p2 = L+S-(L*S);

	p1 = 2*L-p2;

	if (S == 0) {
		rgb[0] = L;
		rgb[1] = L;
		rgb[2] = L;
	} else {
		rgb[0] = toRgb(p1, p2, H+120);
		rgb[1] = toRgb(p1, p2, H);
		rgb[2] = toRgb(p1, p2, H-120);
	}
	rgb[0] *= 255;
	rgb[1] *= 255;
	rgb[2] *= 255;
	return rgb;
}

// Calculate the RGB Value
public function toRgb(q1 : Number, q2 : Number, hue : Number) : Number {
	if (hue>360) hue = hue-360;
	if (hue<0) hue = hue+360;
	if (hue<60) return (q1+(q2-q1)*hue/60);
	else if (hue<180) return (q2);
	else if (hue<240) return (q1+(q2-q1)*(240-hue)/60);

	return (q1);
}

Mathematical Syntax in C# is very similar to AS3. Most of the code can be reused by directly copy and paste.

// C#
// Convert HSL to RGB
private double [] hslToRgb(double H, double S, double L) {
	double [] rgb = new double [3];
	double p1 = 0;
	double p2 = 0;

	if (L<=0.5) p2 = L*(1+S);
	else p2 = L+S-(L*S);

	p1 = 2*L-p2;

	if (S == 0) {
		rgb[0] = L;
		rgb[1] = L;
		rgb[2] = L;
	} else {
		rgb[0] = toRgb(p1, p2, H+120);
		rgb[1] = toRgb(p1, p2, H);
		rgb[2] = toRgb(p1, p2, H - 120);
	}
	rgb[0] *= 255;
	rgb[1] *= 255;
	rgb[2] *= 255;
	return rgb;
}

// Calculate the RGB Value
private double toRgb(double q1, double q2, double hue) {
	if (hue>360) hue = hue-360;
	if (hue<0) hue = hue+360;
	if (hue<60) return (q1+(q2-q1)*hue/60);
	else if (hue<180) return (q2);
	else if (hue<240) return (q1+(q2-q1)*(240-hue)/60);

	return (q1);
}
Oct 14

image2Microsoft released the official version of Silverlight 2 today (14 Oct 2008). You could download the latest runtime in the link below.

http://www.microsoft.com/silverlight/default.aspx

The removal of the word “beta” in the release version means that the technology is much more mature and has lesser bugs (I hope).

However, after installing the new version, I found that all the samples in my blog no longer working anymore. Well, I am not amazed since I know Poor Backward Compatibility is one of the Silverlight Features.

I don’t have any plan to upgrade all the samples yet. Instead, I may take this chance to revamp the whole blog.

Nevertheless, the next post will probably be implemented using the latest SDK. Hence, download Silverlight 2 now and keep your eyes on this blog!