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
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 !
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. ”
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 :
Then if you want to change the cursor programmatically, use the same property in this way :
using System.Windows.Input;
/*** Cursors is a Static Class in System.Windows.Input** defining a set of default mouse pointers** usable with the Cursor Class*/
myFrameworkElement.Cursor = Cursors.Arrow;
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).
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
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
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 :
/* Soyez propre, ne polluez pas le scope global */window.votre_scope = {};
votre_scope.Un_Foncteur = function()
{/* On se prepare a faire une closure */var that = this;
this.some_data = "";
this.foncteur = function()
{/* ** Ici le this est celui de l'appelant, il ne nous interesse probablement ** pas, le that est la closure, il nous permet de remonter vers notre this ** a nous, et d'y recuperer notre data. */alert(that.some_data);
}/* ** On ajoute autant de methodes a notre foncteur que l'on desire */this.foncteur.set_data = function(data)
{/* ** Idem pour la closure ici, on va chercher notre this, car le this d'ici ** est local a la fonction */
that.some_data = data;
}return (this.foncteur);
}/* Enjoy */var test = new votre_scope.Un_Foncteur();
test.set_data(42);
test();