1 Results for 'interactivity'

  1. 2009.05.09 Behaviors in Silverlight 2 (5)

Because, Silverlight 2 also can reference to Microsoft.Expression.Interactivity Assembly which is from Blend 3 library, you can take many advantages of Behaviors model from Silverlight 3 in Silverlight 2 as well. Yes, you can write Behaviors code without version dependency, and it can be referenced by both of Silverlight 2 and 3.

When I found that possibility, I was excited. As you know, Behaviors model brings you a great functionality which might reduce ‘bording-coding’ dramatically. All you have to do is that just copy-paste XAML from Blend 3, absolutely better than Blend 2, And even no code-behind!

I'm going to show you how to make a simple Silverlight 2 application using a simple trigger-action behavior. Create a new Silverlight project, and follow next steps.

Step 1. Referece a Microsoft.Expression.Interactivity Assembly.

Simply, copy a Microsoft.Expression.Interactivity Assembly to any folder to reference from your application. Usually, you can find it C:\Program Files\Microsoft Expression\Blend 3 Preview\Libraries\Silverlight folder if you installed Blend 3 Preview.

Notice that you must rename the assembly file, because Blend 2 can’t recongnize the name contains ‘Microsoft.Expression’. I don’t know why Blend 2 blocks specific assembly names like that. So just rename it say, Interactivy.dll or something. In this example, I renamed Microsoft.Expression.Interactivity.dll to Interactivity.dll. 

After you reference the assembly, you might be encountered an error follow:

It makes you blind in Visual Studio. Don’t panic. :) It’s  just a bug in Visual Studio Designer which already known. However Blend 2 will display it fine, if there was no other errors. Check this out further information.

Step 2. Create a simple trigger-action behavior.

Now, add a class say, ShowMessageBoxAction. It’ll show message box when trigger event raised on associated object.

public class ShowMessageBoxAction : TriggerAction< Dependencyobject>
{
    #region Message

    ///  
    /// Gets or sets the Message possible Value of the string object.
    ///  
    public string Message
    {
        get { return (string)GetValue(MessageProperty); }
        set { SetValue(MessageProperty, value); }
    }

    ///  
    /// Identifies the Message dependency property.
    ///  
    public static readonly DependencyProperty MessageProperty =
    DependencyProperty.Register(
        "Message",
        typeof(string),
        typeof(ShowMessageBoxAction),
        new PropertyMetadata("Message")
    );

    #endregion Message

    protected override void Invoke(object parameter)
    {
        MessageBox.Show(Message);
    }
}

It has a property which you want to show up, and it ovverides Invoke method which is called when trigger event raised.

Step 3. Add a button with trigger.

Make some UI to check trigger performs well, follow:

<UserControl
    x:Class="SL2Behaviors.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:i="clr-namespace:Microsoft.Expression.Interactivity;assembly=Microsoft.Expression.Interactivity"
    xmlns:local="clr-namespace:SL2Behaviors"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <Button Content="Click Me">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Click">
                    <local:ShowMessageBoxAction Message="Hello Behaviors in Silverlight 2!" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </Button>
    </Grid>
</UserControl>

Step 4. Build & run!

How is it? It works fine like Silverlight 3 does. Also you can use other Behaviors like this as well. Try it after these steps. Yes, now you can use Behaviors in your Silverlight 2 applications.

Here is source code for above. It'll be more helpful rather than my poor English. :)



저작자 표시 동일 조건 변경 허락
신고
Posted by gongdo


티스토리 툴바