Archive for the 'Code' Category

A JustInTime class

Saturday, August 30th, 2008

I discovered in my code a redundant pattern :
private SomeType _something;
private SomeType something { get { if (_something == null) _something = new SomeType(….); return _something;} }

Usefull in Silverlight, when your Blender always say “Can’t compile ! Strange errors ! but compiles when i comment your WebClient… in ctor of that class…”
So i use it to have members builded the first time they are used, “Just in time”.
/!\ It’s not a Singleton ! i can have others instance of this class new-ing it directly !
/!\ Singleton is Evil // feed the Troll

(more…)

The art of Events

Friday, August 29th, 2008

The art of using events to build more independent classes.
/* Found a better example */
Imagine you have a class A and a class B.
A builds B, and B have to communicate with A (call methods … ?)
Some developers (Boooo (I’ve done it … (Booo >> me))) will pass a reference of A to B
“A(){B my_B = new B(this);}” /* Please note the newB private joke */
But what happens when you want an other class, C, who don’t know A to build B ?

So we have to remove the A’s reference in the B ctor.
How B should communicate now ?
– Using Events !
B exposes an event, the builder of B can register on it.
– B can speak to A throwing this event.
– Every class can build a B, and can, if it needs, register on its events, doing whatever he wants when the event is thrown.
B is now fully reusable !

Howto invoke an event via reflection

Friday, August 29th, 2008

Why this article ? because of this note found on the msdn’s EventInfo page:
“EventInfo is not intended to be used to raise events. An object raises events as dictated by its internal state. ”

Let’s try to raise an event with reflection …

(more…)

How to make a fake 3D Storyboard with Expression Blend

Monday, August 25th, 2008

In this “how to” i will show you how to make a StoryBoard like this:

This is not hard but you have to be famialiar with Blend.

download this video

Silverlight Cursors

Saturday, August 16th, 2008

Today just a little article about Silverlight Cursors, made because there’s no screenshots of cursors on the msdn.
So, first, how to change the cursor when hovering over a UI element ?
- Use the XAML Cursor property of this FrameworkElement :

Unable to get source code, check your URL given in [viewcode].

Download this code: cursors.xml

Here is the full list and you can test them :

Then if you want to change the cursor programmatically, use the same property in this way :

Unable to get source code, check your URL given in [viewcode].

Download this code: cursors.cs

Finally, if you want to create a custom cursor, the only way i found is to set the cursor to None and load an image, following the cursor programatically (use the MouseMove event).

Silverlight – Password field no longer missing

Thursday, August 14th, 2008

EDIT : There’s now a real PasswordField, our tricks is no longer needed, just read :
http://silverlight.net/blogs/msnow/archive/2008/09/29/silverlight-tip-of-the-day-47-how-to-implement-a-password-box.aspx

Everybody developping in Silverlight knows that there’s no password textbox, and some of us rewrite a UserControl to replace it… yeark !!
There’s a so simple way to create a password textbox :
Use a TextBox and change the font with a home made font in which all characters are ‘*’ …
0 lines of code, do it in XAML

Unable to get source code, check your URL given in [viewcode].

Download this code: password.xml

FontFamily can download a file and pick a font from this file, the syntaxe is given “file.ttf#fontname” in my password.ttf the font name is “Password”, with the .ttf have to be in your ClientBin (relatives or absolute URI can be used with nothing, the root directory is checked, so put it in your ClientBin)
According to TimHeuer’s comment, after beta 2, embedding fonts will not work with this way, so watch his video Using custom fonts in silverlight to do it with the right way :)

Dont want to create a .ttf ? download mine. password.ttf

Javascript Foncteur

Thursday, May 8th, 2008

I just discovered, this morning, how to create a Visitor in Javascript :
A visitor is an objet who can be used like a function :

Unable to get source code, check your URL given in [viewcode].

Download this code: js_foncteur.js

Implementing it in C++ is extremly easy, using operator overloading, so surcharging the () operator of an object, we have a visitor, but in Javascript, in the current version, we do not have operator overloading.
So we have the following trick :

Unable to get source code, check your URL given in [viewcode].

Download this code: js_foncteur2.js

Enjoy !