Notes: The article and samples are based on Silverlight 2 Beta 1. For more details refer to Microsoft Silverlight site. Sample project requires "Microsoft Silverlight Tools Beta 1 for Visual Studio 2008". Details for installing the tools you can find here.
When I’ve downloaded and installed the first cooked beta for Silverlight 2 I was really enraptured from what I’ve seen there. I didn’t expect such a huge jump ahead in such a small term (comparing with previous versions of Silverlight). At last my dream is started to come true.
I appreciate the work done by MS guys to deliver it to the community and realize how much work is to be performed for us to get the real power Silverlight is supposed to bring.
Actually my intention was to test how much time could take me to cook anything working with Silverlight 2 beta 1. As we are facing something like WPF port I’ve decided to test how the common basic project could be ported to browser. First of all I was interested in problems rather than successful results. Anyway I was pleased much with both. As it is the first beta and serve for demonstration purposes my hands were quite tied in doing some efficient code behind. Even having nearly everything as "sealed" I must admit it is quite possible implementing a lot 🙂
It took me only one evening (approximately 3 or 4 hours of pure time) porting basics of my previous diagramming samples to Silverlight 2. Later on I’ll continue enhancing the sample to eliminate difference between my two implementations.
I’ve implemented a separate Silverlight Library for placing some common stuff there and also testing the Library project itself. I didn’t manage to find any helper class for walking visual and logical trees so I’ve implemented simple LogicalTreeHelper for my own needs, mainly hit testing the canvas and recursive parent search.
As I’ve stuck a bit with styling and templating (I’ve played a bit with generics.xaml and it’s compilation as a Resource within the Library projects but guess this part is an early beta so I couldn’t get most of it I wanted) and downgraded to introducing simply separate User Controls for each of the shape. Also I managed to do the basic class for all the shapes to suite as an example later on (according to forums there’s a lot of troubles with inheritance at Beta 1 especially with dependency objects, but we can live with it up to next betas I think).
Basic subclass of Canvas panel. Generic approach for adding children and input delegation to the underlying BasicTool for drawing straight line connections.
Drag and drop
I didn’t introduce anything here as there’s already cooked drag and drop samples that can be found in the Silverlight 2 documentation. Of course later on it can be replaced with some more efficient managers or whatever but as for now the MS sample is really good.
Connections and bindings
I’ve decided not to implement fancy bindings/multibindings for the first version as again I was interested in time more than additional facilities. I came across some issues with code behind bindings and quickly switched to INotifyPropertyChanged interface implementation. The only code behind binding sample between ToggleButton and StraightLineTool can be found in the Page.xaml.cs. Line positioning was left to owner driven layout updates on drag operations.
I must admit that it was a real pleasure preparing this sample. I’ll definitely continue enhancing the sample described alongside with preparing some more detailed overviews of issues I came across and walkarounds performed.
The source code for the article can be found at my SkyDrive