Customize Drawing

Top Previous Topic Next Topic  Print this topic

If we want to create a custom visual element from an ObjectData must create a new plugin which implement IVisualObjectFactory interface.

 

Example

 

Create a new Silverlight Class Library .

 

Add a reference to Ecrion.Silverlight .

 

Implement the IAddin interface: create a new class named Addin which implements IAddin.

 

  public class Addin : IAddin

   {

      public Addin()

       {

       }

   

       // return a new instance of a IVisualObjectFactory

      public IVisualObjectFactory GetVisualObjectFactory()

       {

          return new VisualObjectFactory();

       }

 

       // return null because we don't have mouse event functionality

      public IActionHandler GetActionHandler()

       {

          return null;

       }

   }

 

 

Implement the IVisualObjectFactory interface: create a new class named VisualObjectFactory which implements IVisualObjectFactory.

 

    public class VisualObjectFactory : IVisualObjectFactory

   {

      public VisualObjectFactory()

       {

 

       }

 

      /// <summary>

      /// Builds a custom visual rectangle with round corners from ObjectData with RectangleData type

      /// </summary>

      /// <param name="objectData">an ObjectData</param>

      /// <returns>

      /// Visual element if the type of ObjectData is what we need or null otherwise.

      /// </returns>

      public DependencyObject GetVisualElement(ObjectData objectData)

       {

          if (objectData.getObjectType == typeof(RectangleData))

           {

              // set reference to a rectangle data

              RectangleData rectangleData = (RectangleData)objectData;

 

              //create visual element named Rectangle

              Rectangle visualElement = new Rectangle();

 

              //set rectangle size

               visualElement.Width = rectangleData.Width;

               visualElement.Height = rectangleData.Height;

 

              //anchor rectangle to left-top relative to page

               visualElement.HorizontalAlignment = HorizontalAlignment.Left;

               visualElement.VerticalAlignment = VerticalAlignment.Top;

 

              //tranform rectangle origin using matrix tranform

              Point pointOrigin = rectangleData.Transform.Transform(rectangleData.TopLeftPoint);

 

              //set rectangle fill and brush

               visualElement.Fill = rectangleData.Fill;

               visualElement.Stroke = rectangleData.Stroke;

 

              //set stroke thickness and stroke dash

               visualElement.StrokeThickness = rectangleData.StrokeWidth;

               visualElement.StrokeDashArray = rectangleData.StrokeDash;

 

              //set radius x and y to 10 value

              //this is a visual customization only because ObjectData remain the same

               visualElement.RadiusX = 10;

               visualElement.RadiusY = 10;

 

              //create new matrix tranform by set x offset and y offset to origin point x and y

              MatrixTransform matrixTransform = new MatrixTransform();

               matrixTransform.Matrix = new Matrix(rectangleData.Transform.M11, rectangleData.Transform.M12, rectangleData.Transform.M21, rectangleData.Transform.M22, pointOrigin.X, pointOrigin.Y);

               visualElement.RenderTransform = matrixTransform;

 

              if (rectangleData.Clip != null)

               {

                   visualElement.Clip = rectangleData.Clip;

                   visualElement.Clip.Transform = Util.inverse(matrixTransform); //calculate inverse of a matrixTranform

               }

 

              return visualElement;

           }

 

          return null;

       }

 

      /// <summary>

      /// has no functionality because we don't want to customize an ObjectData

      /// </summary>

      /// <param name="objectData">an ObjectData </param>

      /// <returns>

      /// return true because we want to allow other plugins to make their own customizations

      /// </returns>

      public Boolean CustomizeObjectData(ObjectData objectData)

       {

          return true;

       }

   }

 

GetVisualObject - creates a custom rectangle with round corners.

 

Build the project: put the DLL file in the Ecrion.Silverlight XAP archive and edit AppManifest.xaml to register the plugin.