Behavior in SILVERLIGHT 3.0

Objective

This article will give a basic introduction of BEHAVIOR feature in SILVERLIGHT3.0. I will also walkthrough to create a custom behavior.

What is Behavior in SILVERLIGHT 3?

According to definition given in the product introduction of SILVERLIGHT3

A Behavior is in essence a reusable piece of interactivity that can be applied directly to user interface elements in Expression Blend. The whole product introduction on behavior can be read here.

I summarized few of the points as below about SILVERLIGHT behavior

  1. Behavior is new feature introduced in SILVERLIGHT 3.0.
  2. Behavior is a reusable piece of code, which can be attached to any object declaratively.
  3. Behavior is the way to allow designer to add functionality to XAML elements without codes.
  4. Behavior allows attaching functionality to an object without writing any code.
  5. Behavior encapsulates the functionality and can be attached to any element.
  6. Behavior can be attach to an element or object through XAML. There is no need to write C# code.
  7. A single behavior can be attached to any number of objects.
  8. Behavior is reusable piece of code can be attached to any object.
  9. Behavior comes with System.Windows.Interactivity.dll

 Creating a Custom Behavior
Step 1
Create a SILVERLIGHT Application. Two projects will get created SilverLightApplication1 and SilverLightApplication1.Web. If you are not, changing the default name. Add reference of System.Windows.Interactivity.dll in SilverLightApplication1 project.

Step 2

Right click on SILVERLIGHT project and add a class. Give any name, for my purpose; I am giving name ImageBehavior.

  1. Add the namespace using System.Windows.Interactivity;
  2. Inherit class from Behavior<T>
  3. Make T as DependencyObject
  4. Override the methods OnAttached and OnDetaching
  5. On MouseEnter
    event and MouseLeave event write the simple logic to rotate the object to which Behavior will be attached.

    ImageBehavior.cs

using System;using System.Net;

using System.Windows;using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Ink;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

using System.Windows.Interactivity;

namespace BehaviorSample1

{

public class ImageBehavior : Behavior<DependencyObject>

{

public ImageBehavior()
{
}
protected override void OnAttached()
{
base.OnAttached();
var ad = this.AssociatedObject as
FrameworkElement;
ad.MouseEnter += (sender, args) =>
{
var p = new PlaneProjection();
p.RotationZ = new Random().Next();
ad.Projection = p;
};
ad.MouseLeave += (sender, args) =>
{
ad.Projection = null;
};
}
protected override void OnDetaching()
{
base.OnDetaching();
}}}

Attaching Custom Behavior to object

Step 1

Before adding custom behavior to element, we need to add namespaces. Open the XAML page and add below namespaces.

xmlns:i=”clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity”xmlns:behavior=”clr-namespace:BehaviorSample1″

First namespace is for the System.Windows.Interactivity dll and second one is namespace of the custom behavior class.

Step 2

Since; I am going to attach the custom behavior we created with Image control. So for the source of Image control add an image to SILVERLIGHT project by Add an existing item. I have added an image and gave the name a.jpg.

Step 3

Add an Image control on XAML and attach the behavior like below.

<Image Height=”Auto” Width=”Auto” Source=”a.jpg”> <i:Interaction.Behaviors>
<behavior:ImageBehavior />

</i:Interaction.Behaviors>

</Image>

 Where I isnamespace of dll and behavior is namespace of custom behavior class.

MainPage.Xaml

<UserControl x:Class=”BehaviorSample1.MainPage”
xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation

xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml

xmlns:i=”clrnamespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity”

xmlns:behavior=”clr-namespace:BehaviorSample1″

xmlns:d=”http://schemas.microsoft.com/expression/blend/2008&#8243; xmlns:mc=http://schemas.openxmlformats.org/markup-compatibility/2006

mc:Ignorable=”d” d:DesignWidth=”640″ d:DesignHeight=”480″>

<Grid x:Name=”LayoutRoot” Height=”Auto” Width=”Auto” Background=”Black”>

<Image Height=”Auto” Width=”Auto” Source=”a.jpg”

<i:Interaction.Behaviors>

<behavior:ImageBehavior />

</i:Interaction.Behaviors>

</Image>

</Grid>
</UserControl>

 Step 4

There is no need of do any coding in MainPage.Xaml.cs

Run the application

On mouse enter to Image; Image will rotate with random degree of angle. Like below. Thanks for reading.

Dynamic type with Reflection in debug mode

Objective

This article will give simple debug mode explanation on how Reflection and Dynamic type works.

Note: Read my previous article on dynamic type here Dynamic Type

Let us create a very simple class called Student.

public class Student{

public void Print(string Name)
{
Console.WriteLine(“Student Name is “ + Name);
}}

 Now say, you don’t have information about type of the class while creating instance of this class. Or in other words you don’t have type information of the class at the compile time. So how to create instance of this class? Till c# 3.0 ; we had no choice but to take help of Reflection . Code was highly messy. Not very readable and not beautiful either.

Code will look like,

Object student = new Student();

Type studentType = student.GetType();

Object res = studentType.InvokeMember(“Print”, BindingFlags.InvokeMethod,null, student ,new
Object[] {“Dhananjay”});

Console.Read();

 Few points about above code

  1. We are creating instance of the Student class as object type. Since we don’t know type of the class at compile time.

     

 

By running in debug mode, we can see at compile time class is resolving as DynamicReflection.Student.

  1. In 2nd line of code we are finding, type using GetType method of object class.

We can see Compiler is resolving the type as Student. If you remember Student is name of our class.

  1. On the type (StudentType in our case) we are calling the InvokeMember method to call the method on the instance of the class. InvokeMember is overloaded function and we are passing four parameters to this.
  2. Since method of the class is returning void so res is NULL.

Output

 

Now if you see above code, is not it very messy? So in c# 4.0 we do have dynamic type to deal with.

dynamic dynamicStudent = new Student();

dynamicStudent.Print(“Dhananjay With Dynamimc Type “);
Console.Read();

Now again if you run the above code in debug mode, you will see type for dynamic type is getting resolved exactly the same as of reflection.

After type resolving all the other work to call method is done by compiler at the back ground. So simply we need to call the method on the dynamic type and DLR will take care of everything.

 

So, we saw in debug mode how dynamic type and reflection works. Thanks for reading.