Where do you "event"?

From JohnPapa.net, “Siliverlight 2-Tip – Declaring Events.”

He thinks that the place to wire up an event is always in code rather than in markup (like XAML).

“What’s my opinion? I firmly believe the handlers belong in the code and not in the XAML.”

I’d suggest that’s not always right either (and maybe not the right question to ask). This technique doesn’t allow the designer to have freedom to decide how to build the interface freely. If you wire up to a “button” click, then the designer has to implement a certain UI feature as a button. Maybe that’s not what they wanted. The UI/code are wound together extremely tightly when using this pattern.

A command/message driven architecture that splits the actual implementation as much as possible is the best pattern for growth and true separation of roles. That being said, it’s not always practical to build a system like that.

I like the CONCEPT of WPF’s RoutedCommand system which is a way in WPF to handle this pattern, but I don’t care for the way that things are enabled and disabled via the CanExecute event handlers. The event handlers can be called hundreds of times a minute depending on your user interface.

The bad news is that Silverlight doesn’t lend itself easily to a message/command driven architecture (as the RoutedCommand system isn’t available in Silverlight 2.0). So what’s a developer to do? :)

I like the ‘toolability’ of doing inline (in XAML) wire-up of events. If the developer just provides a common set of end-points (events) to the UI designer, then they may have an opportunity to more freely control and adjust the user interface as needed without requiring code changes.

So in John’s example code, instead of

btnAddToCard.Click += new RoutedEventHandler(btnAddToCard_Click);

It might be better to provide a method called:

AddToCard(object sender, RoutedEventArgs e) {

}

That way, the designer can wire-up any way they want.

Or, I suppose one could build a rudimentary message/command system by using attached properties and a common event handler which handles all command driven user interface elements. (And now that I think about that more — I think that could end up being quite slick if done carefully).

Google Chrome ….

Google’s running their own ads? :)  (An Ad from Google that appeared on my web site in the past few days).

image

Also, if you’d like to read the whole Google Chrome comic, it’s available from Google here. There are some PDFs as well floating around, but they’re not higher quality (in terms of resolution).

C# Extension methods and strings …

Thomas posted an interesting experiment regarding the use of extension methods on strings. He noticed that it’s possible to add an extension method to a string, and that it’s still called even when the string object is null. Given that it’s just syntactic sugar, it makes perfect sense, but it’s so odd to see code like this:

image

I wouldn’t use this as it just feels wrong to call a method on an object that is null. But, it’s a nifty trick (and something you should watch out for in your extension methods!).

Coding Challenge #21

Coding Challenge Series / Technical Interview Series

Create a function which copies byte data from a source location to a destination location (you might use pointers or an array depending on the programming language). The function must copy using this pattern: 8 bytes, skip 8 bytes, 4 bytes, skip 8 bytes, 8 bytes, … repeat (+8,-8,+4,-8….).

Bonus points for really nailing the performance of this one.

 

(Are you enjoying this series? I’d appreciate a comment if you’d like me to continue the series. It takes more time than I’d like to admit to create and post these entries every week.)

Google Chrome … it’s here (for Windows)

Downloaded and installed … it’s fast (and one of the easiest installs I’ve seen for any application. Kudos to Google for bucking the trend of awful application installers).

image 

The maximized mode is particularly clean — it doesn’t use a Windows title bar at all – so the first browser tab is the element in the upper left corner of the screen.

As described in the Chrome comic, it is using a process for every tab:

image

It would be nice if I could easily tell which browser tab was represented by each process (for memory analysis). I randomly killed one of the processes. When I clicked on the browser tabs, Chrome displayed this in the place of the tab’s content that I had killed:

image

(Aw, Snap! Something went wrong while displaying this webpage. To continue, press Reload or go to another page.)

:)

Chrome is available for XP/Vista only for now and may be downloaded here.

 

Update: about:memory in the address bar of Chrome takes you to the “stats for nerds”:

image

Or you can right click on the task bar icon, select Task Manager, and click the “Stats for Nerds” link at the bottom of the dialog:

image