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 :
Unable to get source code, check your URL given in [viewcode].
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
Unable to get source code, check your URL given in [viewcode].
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 :
Unable to get source code, check your URL given in [viewcode].