Custom Shape in Silverlight (porting app from WPF) Custom Shape in Silverlight (porting app from WPF) wpf wpf

Custom Shape in Silverlight (porting app from WPF)


You will not be able to produce a class that works in the same way because Silverlight does not support the creation of custom elements that derive from the Shape base class.

The reason it's impossible to create a custom shape in Silveright is that Silverlight does not share WPF's "visual layer". If you want to understand fully why what you're trying is impossible, you need to understand how Silverlight is very different from WPF here. (And if you don't care, skip the next 2 paragraphs.)

In WPF, you can work at two completely different levels: the visual layer, or the framework layer. The visual layer's services are provided by WindowsBase.dll and PresentationCore.dll. This provides basic rendering and input services. But if you want things like styling, data binding, layout, templating and so on, you need the framework services, and these are provided by PresentationFramework.dll. The shape types - Rectangle, Path, and so on - are all framework types - they derive from FrameworkElement and they support data binding, layout, animation and so on. But they are implemented on top of the visual layer - if you look at any of the Shape types in Reflector or ILDASM you'll see they all override the OnRender method, and that's where the code that defines the actual shape lives. (OnRender is a visual layer function.) And because the visual layer is a fully supported and documented API, you're free to write your own shapes in WPF - you can write exactly the same sort of code as you'll find in the built-in shape classes.

Silverlight doesn't make this visual/framework distinction - in Silverlight, WPF's visual layer has essentially collapsed into the framework layer. So if you look at the shape types in Reflector or ILDASM, you'll see that they contain no OnRender method, and they're almost empty. That's because in Silverlight, the shapes are all intrinsics - the plugin has built-in special handling for Ellipse, Path, and all the other shapes. So the set of shapes is not open to extension in Silverilght. There is no OnRender method to override in Silverlight. So you simply cannot write your own custom class that derives from Shape in Silverlight.

So, either a custom Control or a UserControl will be the way to go, I'm afraid. This shouldn't stop the MouseEnter and MouseLeave from working though. Have you actually found that those don't work? Or are you just assuming that they won't work?


What if keep your existing class, lets call it CustomShape, as is, then inherent from Control with something like CustomShapeContainer? CustomShapeContainer would essentially just be a wrapper around CustomShape. You could then pass all of the events coming into CustomShapeContainer directly through into CustomShape and then return the shapes DefininingGeometry object as the Containers content.

At first glance, this seems like the path of least resistance.


You do not have the same namespaces in Silverlight. Silverlight xaml is a subset of WPF xaml and there are assemblies which are not included in Silvelright. These technologies are intended for different kind os solutions.

You might need to start anew. However, if you used the MVVM pattern, very little code behind then you might be able to re-use your ViewModel, Model and services. Maybe resources, styles would be OK to reuse "as is". But the View: start new.