Create your own behaviors with Silverlight

Aucun commentaire

If you've used Expression Blend, it is quite possible that you have used behavior during your projects. They are beneficial on several levels: first, they can add interaction without coding in C # or VB on the other hand, they provide valuable assistance when it is important to decouple the model and view-model. Finally, they play an essential role in production as they are developed with an objective of reuse and are easily configurable in Expression Blend.

There are two main families of behaviors, simple behaviors and action triggered behaviors. The simple behaviors are represented by a gear icon, MouseDragElementBehavior that you find in the assets panel, is of this kind. Once you settle on the object, it acquires additional capacity accessible. In an other way, action triggered behaviors only act when an event is broadcast. They are identified by the icon of the gear topped by a spire of reading.

To create a new behavior in Blend, you have to add an item to a project of type Behavior or Action (via the Add Item menu ...). We will create a behavior to create a copy of a visual object. This will allow us to generate a cool effect as shown in the Silverlight project below.

Install Microsoft Silverlight

Create a new project and add an Action behavior. Let the class inherit TargetedTriggerAction<UIElement>. This is convenient because we can target an object that you want to replicate the visual. You get the code below:

public class SnapshotAction : TargetedTriggerAction&lt;UIElement&gt;
{
	public SnapshotAction()
	{
 
	}
 
	protected override void Invoke(object o)
	{
 
	}
 
}

This is the overridden method Invoke of interest. This will be executed each time an event of your choice will be announced. We will now obtain an image of the component targeted then applied to the component with the behavior.

protected override void Invoke(object o)
{
      //on déclare une nouvelle image en écriture
      WriteableBitmap Wb;
 
      //on l'instancie en lui passant l'objet
      //elle doit créer un instantanné
      Wb = new WriteableBitmap(this.Target, null);
 
      //on crée un nouveau pinceau d'image
      ImageBrush Ib = new ImageBrush();
 
      Ib.AlignmentX = AlignmentX.Left;
 
      Ib.Stretch = Stretch.None;
      //on affecte l'image binaire de l'instantanné à
      //la propriété ImageSource du pinceau d'image
      Ib.ImageSource = Wb;
 
      //Ensuite on teste le type de l'objet ciblé et on affecte
      //la bonne propriété en fonction de ce dernier
      if (AssociatedObject is Shape) ((Shape)AssociatedObject).Fill = Ib;
      else if (AssociatedObject is Panel) ((Panel)AssociatedObject).Background = Ib;
      else if (AssociatedObject is Control) ((Control)AssociatedObject).Background = Ib;
      //nous pourrions utiliser un peu de réflexion pour améliorer ce test
}

The Target property of the behavior corresponds to the object targeted by the behavior. The property AssociatedObject corresponds to the object on which the component is deposited. In our case, we simply test the type of object to apply for the capture bitmap as brush filling.

That's it, just compile the project once to access the behavior in the assets panel, then place it on an instance of Shape (for example) as shown in the figure below.

The behavior is finalized here. I've added the ability to trigger replication bitmap mode automatically, via use of a DispatcherTimer. It is also possible to change the refresh rate to avoid loss of performance (see figure below).

Finally, know that with Silverlight, 4 it is now possible to create behaviors directly integrating the command pattern. This is directly related to the design model MVVM but that's another story.

Bookmark and Share

Leave a Reply

Security Code: