<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Shazaml Design, LLC &#187; BaseBehavior</title>
	<atom:link href="http://www.shazaml.com/archives/tag/basebehavior/feed" rel="self" type="application/rss+xml" />
	<link>http://www.shazaml.com</link>
	<description>Design and Development for Windows Phone 7</description>
	<lastBuildDate>Thu, 27 Oct 2011 19:33:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Hidden Object: Episode 7 – Use an Action to Toggle the Magnifier Behavior</title>
		<link>http://www.shazaml.com/archives/hidden-object-episode-7</link>
		<comments>http://www.shazaml.com/archives/hidden-object-episode-7#comments</comments>
		<pubDate>Sat, 26 Sep 2009 13:03:42 +0000</pubDate>
		<dc:creator>Mark Tucker</dc:creator>
				<category><![CDATA[Hidden Object Game]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Action]]></category>
		<category><![CDATA[BaseBehavior]]></category>
		<category><![CDATA[Behavior]]></category>
		<category><![CDATA[Blend]]></category>
		<category><![CDATA[games]]></category>
		<category><![CDATA[MagnifierOverBehavior]]></category>
		<category><![CDATA[SetInteractionPropertyAction]]></category>
		<category><![CDATA[Trigger]]></category>

		<guid isPermaLink="false">http://www.shazaml.com/?p=186</guid>
		<description><![CDATA[Using the magnifier-styled CheckBox from the last episode along with the Behavior base classes and SetInteractionPropertyAction we finish the magnification feature.]]></description>
			<content:encoded><![CDATA[<p>This is episode 7 of <a href="http://www.shazaml.com/archives/creating-a-hidden-object-game-in-silverlight-3">Creating a Hidden Object Game is Silverlight 3</a>.</p>
<p>In the last few posts we have been working on the magnifier feature of the game which we will finish in this episode.</p>
<p>Please review the following posts:</p>
<ul>
<li><a href="http://www.shazaml.com/archives/hidden-object-episode-5">Episode 5 – Add a Magnifier Behavior</a></li>
<li><a href="http://www.shazaml.com/archives/base-classes-for-custom-behaviors">Base Classes for Custom Behaviors</a></li>
<li><a href="http://www.shazaml.com/archives/setinteractionpropertyaction">Creating an Action to set Properties on Actions &amp; Behaviors</a></li>
<li><a href="http://www.shazaml.com/archives/hidden-object-episode-6">Episode 6 – Create a CheckBox from an Image</a></li>
</ul>
<p>First we will add in the following files into the ClutteredCube project:</p>
<ul>
<li>
<div>Interactivity folder (from <a href="http://www.shazaml.com/archives/base-classes-for-custom-behaviors">Base Classes for Custom Behaviors</a>)</div>
<ul>
<li>BaseBehavior.cs</li>
<li>BaseBehaviorT.cs</li>
</ul>
</li>
<li>
<div>Interactivity\SetInteractionPropertyAction folder (from <a href="http://www.shazaml.com/archives/setinteractionpropertyaction">Creating an Action to set Properties on Actions &amp; Behaviors</a>)</div>
<ul>
<li>ConverterHelper.cs</li>
<li>SetInteractionPropertyAction.cs</li>
</ul>
</li>
</ul>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/09/092609_1303_HiddenObjec1.png" alt="" /></p>
<p>The first thing we want to do is to change MagnifierOverBehavior so that it inherits from our custom behavior base class thus inheriting the IsEnabled property:</p>
<pre class="brush: csharp; light: true; title: ; notranslate">
public class MagnifierOverBehavior : BaseBehavior&lt;FrameworkElement&gt;
</pre>
<p>The only other change we need to make this behavior is not set the Effect property on MouseEnter but instead in MouseMove based on the value of the IsEnabled property:</p>
<pre class="brush: csharp; light: true; title: ; notranslate">
private void AssociatedObject_MouseEnter( object sender, MouseEventArgs e )
{
  this.AssociatedObject.MouseMove += new MouseEventHandler( AssociatedObject_MouseMove );
  //this.AssociatedObject.Effect = this.magnifier;
}

private void AssociatedObject_MouseMove( object sender, MouseEventArgs e )
{
  if (IsEnabled)
  {
    if (this.AssociatedObject.Effect != this.magnifier)
    {
      this.AssociatedObject.Effect = this.magnifier;
    }
    ...
  }
}
</pre>
<p>In Blend, open the MainPage and select the MagnifierOverBehavior in the Object tree under <em>magnifierCanvas</em>. Name the behavior <em>magnifierBehavior</em> and set IsEnabled to false:</p>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/09/092609_1303_HiddenObjec2.png" alt="" /></p>
<p>From the Assets tab drag two instances of SetInteractionPropertyAction onto the CheckBox magnifier control:</p>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/09/092609_1303_HiddenObjec3.png" alt="" /></p>
<p>On the first one, set the properties as follows:</p>
<ul>
<li>EventName: Checked</li>
<li>TargetName: magnifierCanvas</li>
<li>ObjectName: magnifierBehavior</li>
<li>PropertyName: IsEnabled</li>
<li>Value: true</li>
</ul>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/09/092609_1303_HiddenObjec4.png" alt="" /></p>
<p>Set the second the same as the first, except:</p>
<ul>
<li>EventName: Unchecked</li>
<li>Value: false</li>
</ul>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/09/092609_1303_HiddenObjec5.png" alt="" /></p>
<p>Make sure that the CheckBox control IsChecked property is false to match the InEnabled value of false for <em>magniferBehavior</em>.</p>
<p>Run the game and verify that magnification is only turned on when the CheckBox is checked.</p>
<p>Let&#8217;s do one more thing to finish this episode. Notice that magnifier.png has a white background behind the glass part of the magnifying glass. The image should be on top of something white like a notepad. Import the notepad image and drop it at the bottom of LayoutRoot.</p>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/09/notepad.png" alt="notepad" /></p>
<p>Position it how you want it and move the magnifier CheckBox on top of it.</p>
<p><a href="http://www.shazaml.com/downloads/ClutteredCubeSource7.zip">Source Code</a></p>
<p><a href="http://www.shazaml.com/hidden-object-episode-7-demo">Demo</a></p>
<p>There are many features to add so I will keep the <a href="http://www.shazaml.com/archives/hidden-object-episode-8">next episode</a> contents a surprise.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shazaml.com/archives/hidden-object-episode-7/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Base Classes for Custom Behaviors</title>
		<link>http://www.shazaml.com/archives/base-classes-for-custom-behaviors</link>
		<comments>http://www.shazaml.com/archives/base-classes-for-custom-behaviors#comments</comments>
		<pubDate>Tue, 22 Sep 2009 00:22:06 +0000</pubDate>
		<dc:creator>Mark Tucker</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[BaseBehavior]]></category>
		<category><![CDATA[Behavior]]></category>
		<category><![CDATA[Blend]]></category>

		<guid isPermaLink="false">http://www.shazaml.com/?p=119</guid>
		<description><![CDATA[Creating a base behavior class is a great way to add common properties to your custom behaviors.]]></description>
			<content:encoded><![CDATA[<p>Behaviors are a powerful way to encapsulate functionality and make it available for designers to use in Blend 3. As your library of custom behaviors grows, you might notice a number of properties that most of your behaviors use. You know that duplication of each of these properties in each of your behaviors is not the right way to go, so you want to create a base class that all your custom behaviors use.</p>
<p>The TriggerAction base class includes an IsEnabled property which I would like to include in all my custom behaviors. Instead of adding the DependencyProperty and associated code to each custom behavior, let&#8217;s create a base behavior class.</p>
<p>The existing behavior base classes are:</p>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/09/092209_0022_BaseClasses1.png" alt="" /></p>
<p>If we create a custom behavior, MyBehavior, that inherits from Behavior&lt;DependencyObject&gt; and in Blend 3 drop the behavior on the LayoutRoot Grid, we get a behavior with no properties defined:</p>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/09/092209_0022_BaseClasses2.png" alt="" /></p>
<p> </p>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/09/092209_0022_BaseClasses3.png" alt="" /></p>
<p> </p>
<p>To create a base class for our custom behaviors, create an abstract BaseBehavior class that inherits from Behavior&lt;T&gt;. This is where we add the IsEnabled property and any other common properties we want our custom behaviors to have. Next derive a BaseBehavior&lt;T&gt; class from BaseBehavior. It is from BaseBehavior&lt;T&gt; that we will derive our custom behaviors:</p>
<p> </p>
<p><a href="http://www.shazaml.com/wp-content/uploads/2009/09/092209_0022_BaseClasses4.png"><img class="alignnone size-medium wp-image-117" title="092209_0022_BaseClasses4.png" src="http://www.shazaml.com/wp-content/uploads/2009/09/092209_0022_BaseClasses4-300x224.png" alt="092209_0022_BaseClasses4.png" width="300" height="224" /></a></p>
<p>The code for BaseBehavior simply derives from Behavior&lt;T&gt; and defines the property, dependency property, and callback method for the IsEnabled property:</p>
<p> </p>
<pre class="brush: csharp; gutter: false; title: ; wrap-lines: false; notranslate">
public abstract class BaseBehavior : Behavior&lt;DependencyObject&gt;
{
    internal BaseBehavior()
    {
    }

    #region IsEnabled (Dependency Property)

    [Category(&quot;Common Properties&quot;)]
    public bool IsEnabled
    {
        get
        {            
            return (bool)base.GetValue(IsEnabledProperty);
        }
        set
        {
            base.SetValue(IsEnabledProperty, value);
        }
    }

    public static readonly DependencyProperty IsEnabledProperty =
        DependencyProperty.Register(
            &quot;IsEnabled&quot;,
            typeof(bool),
            typeof(BaseBehavior),
            new PropertyMetadata(true, new PropertyChangedCallback(OnIsEnabledChanged)));

    private static void OnIsEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ((BaseBehavior)d).OnIsEnabledChanged(e);
    }

    protected virtual void OnIsEnabledChanged(DependencyPropertyChangedEventArgs e)
    {
    }
    #endregion
}
</pre>
<p>The BaseBehavior&lt;T&gt; class derives from BaseBehavior and redefines the AssociatedObject property using the <em>new</em> keyword. We do this so that the AssociatedObject can be typed when we derive our custom behavior from it:</p>
<pre class="brush: csharp; gutter: false; title: ; notranslate"> 
public abstract class BaseBehavior&lt;T&gt;
    : BaseBehavior where T : DependencyObject
{
    protected BaseBehavior() : base() {}

    protected new T AssociatedObject
    {
        get
        {
            return (T)base.AssociatedObject;
        }
    }
}
</pre>
<p>In MyBehavior, change the class definition from:</p>
<pre class="brush: csharp; light: true; title: ; notranslate"> 
public class MyBehavior : Behavior&lt;DependencyObject&gt;
</pre>
<p>to</p>
<pre class="brush: csharp; light: true; title: ; notranslate">
public class MyBehavior : BaseBehavior&lt;DependencyObject&gt;
</pre>
<p>BaseBehavior&lt;T&gt; is a drop-in replacement for Behavior&lt;T&gt;. When we recompile and view the behavior&#8217;s properties in Blend, we see that the IsEnabled property has been added:</p>
<p> </p>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/09/092209_0022_BaseClasses5.png" alt="" /></p>
<p>  </p>
<p>As needed, we can add additional properties to BaseBehavior.</p>
<p>What other properties do you think should be added to BaseBehavior?</p>
<p><a href="http://www.shazaml.com/downloads/BaseBehaviors.zip">Source code</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.shazaml.com/archives/base-classes-for-custom-behaviors/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

