<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://www.dotnetmafia.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Kevin Williams .NET and Stuff : lolcats, XAML</title><link>http://www.dotnetmafia.com/blogs/kevin/archive/tags/lolcats/XAML/default.aspx</link><description>Tags: lolcats, XAML</description><dc:language>en</dc:language><generator>CommunityServer 2007.1 (Build: 20917.1142)</generator><item><title>Silverlight Day 2</title><link>http://www.dotnetmafia.com/blogs/kevin/archive/2008/02/22/silverlight-day-2.aspx</link><pubDate>Sat, 23 Feb 2008 02:53:00 GMT</pubDate><guid isPermaLink="false">ceb7fe2a-c56b-4d85-99e6-8dd548580538:515</guid><dc:creator>Kevin</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://www.dotnetmafia.com/blogs/kevin/rsscomments.aspx?PostID=515</wfw:commentRss><comments>http://www.dotnetmafia.com/blogs/kevin/archive/2008/02/22/silverlight-day-2.aspx#comments</comments><description>&lt;p&gt;Today&amp;#39;s goal is to display an image and react to a mouse click in some way.&amp;nbsp; I actually started by reading the QuickStart that gets installed along with the Silverlight SDK.&amp;nbsp; You can also find it at &lt;a title="http://silverlight.net/quickstarts/silverlight10/default.aspx" href="http://silverlight.net/quickstarts/silverlight10/default.aspx"&gt;http://silverlight.net/quickstarts/silverlight10/default.aspx&lt;/a&gt; if you still don&amp;#39;t have the SDK installed (and why don&amp;#39;t you?&amp;nbsp; Huh?)&lt;/p&gt;
&lt;p&gt;It looks like all I need to display an image is an object set to fill with an ImageBrush.&amp;nbsp; Something like this should work:&lt;/p&gt;
&lt;div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:courier new;"&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR:#a31515;"&gt;Ellipse&lt;/span&gt;&lt;span style="COLOR:red;"&gt; Height&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;=&amp;quot;100&amp;quot;&lt;/span&gt;&lt;span style="COLOR:red;"&gt; Width&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;=&amp;quot;200&amp;quot;&lt;/span&gt;&lt;span style="COLOR:red;"&gt; Canvas.Top&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;=&amp;quot;10&amp;quot;&lt;/span&gt;&lt;span style="COLOR:red;"&gt; Canvas.Left&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;=&amp;quot;10&amp;quot;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:#a31515;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR:#a31515;"&gt;Ellipse.Fill&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:#a31515;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR:#a31515;"&gt;ImageBrush&lt;/span&gt;&lt;span style="COLOR:red;"&gt; ImageSource&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;=&amp;quot;lolcat_quadcore.jpg&amp;quot; /&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:#a31515;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="COLOR:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="COLOR:#a31515;"&gt;Ellipse.Fill&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="COLOR:#a31515;"&gt;Ellipse&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;
&lt;p&gt;Bingo, that did the trick.&amp;nbsp; It looks like this:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.killeverything.com/zak/blogimages/SilverlightDay2_125AC/image.png"&gt;&lt;img height="116" alt="lolcat ellipse" src="http://www.killeverything.com/zak/blogimages/SilverlightDay2_125AC/image_thumb.png" width="216" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;Oh wait, I just found the Image object - no brush needed if you do it like this:&lt;/p&gt;
&lt;div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:courier new;"&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR:#a31515;"&gt;Image&lt;/span&gt;&lt;span style="COLOR:red;"&gt; Source&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;=&amp;quot;lolcat_quadcore.jpg&amp;quot;&lt;/span&gt;&lt;span style="COLOR:red;"&gt; Canvas.Top&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;=&amp;quot;120&amp;quot;&lt;/span&gt;&lt;span style="COLOR:red;"&gt; Canvas.Left&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;=&amp;quot;10&amp;quot;&lt;/span&gt;&lt;span style="COLOR:red;"&gt; Height&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;=&amp;quot;100&amp;quot; /&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;
&lt;p&gt;Which looks like this:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.killeverything.com/zak/blogimages/SilverlightDay2_125AC/image_3.png"&gt;&lt;img style="BORDER-TOP-WIDTH:0px;BORDER-LEFT-WIDTH:0px;BORDER-BOTTOM-WIDTH:0px;BORDER-RIGHT-WIDTH:0px;" height="113" alt="lolcat image" src="http://www.killeverything.com/zak/blogimages/SilverlightDay2_125AC/image_thumb_3.png" width="137" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;Note that in the first case, the image was automatically stretched to fit the dimensions of the ellipse and in the second, I only specified a height - so the image was automatically scaled proportionally.&amp;nbsp; Cool!&lt;/p&gt;
&lt;p&gt;Now, processing mouse clicks.&amp;nbsp; This appears to be amazingly easy as well.&amp;nbsp; First, you need to declare the event handler in the XAML mark-up like this:&lt;/p&gt;
&lt;div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:courier new;"&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="COLOR:#a31515;"&gt;Image&lt;/span&gt;&lt;span style="COLOR:red;"&gt; Source&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;=&amp;quot;lolcat_quadcore.jpg&amp;quot;&lt;/span&gt;&lt;span style="COLOR:red;"&gt; Canvas.Top&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;=&amp;quot;120&amp;quot;&lt;/span&gt;&lt;span style="COLOR:red;"&gt; Canvas.Left&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;=&amp;quot;10&amp;quot;&lt;/span&gt;&lt;span style="COLOR:red;"&gt; Height&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;=&amp;quot;100&amp;quot;&lt;/span&gt;&lt;span style="COLOR:red;"&gt; MouseLeftButtonDown&lt;/span&gt;&lt;span style="COLOR:blue;"&gt;=&amp;quot;ChangeLolcat&amp;quot; /&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;
&lt;p&gt;Then you just need a javascript function to handle the event - mine looks like this:&lt;/p&gt;
&lt;div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:courier new;"&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:blue;"&gt;function&lt;/span&gt; ChangeLolcat( sender, args )&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;{&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;if&lt;/span&gt; ( sender.Source == &lt;span style="COLOR:#a31515;"&gt;&amp;quot;lolcat_quadcore.jpg&amp;quot;&lt;/span&gt; )&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sender.Source = &lt;span style="COLOR:#a31515;"&gt;&amp;quot;lolcat_schroedinger.jpg&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;else&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sender.Source = &lt;span style="COLOR:#a31515;"&gt;&amp;quot;lolcat_quadcore.jpg&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;}&lt;/p&gt;&lt;/div&gt;
&lt;p&gt;In this case, because the MouseLeftButtonDown event is on the Image object, that&amp;#39;s the object that gets passed in to the javascript function as &amp;quot;sender&amp;quot;.&amp;nbsp; I am simply changing the Source property from how it was defined in the XAML in response to the mouse click.&amp;nbsp; And it works! (sorry I can&amp;#39;t show you - I don&amp;#39;t have a good place to host .NET code at the moment)&amp;nbsp; I think I&amp;#39;m going to like working with Silverlight.&lt;/p&gt;
&lt;p&gt;In case you want to download the code for this project and try it out yourself, I will ZIP it up and attach it to this blog post.&lt;/p&gt;&lt;img src="http://www.dotnetmafia.com/aggbug.aspx?PostID=515" width="1" height="1"&gt;</description><enclosure url="http://www.dotnetmafia.com/blogs/kevin/attachment/515.ashx" length="93593" type="application/x-zip-compressed" /><category domain="http://www.dotnetmafia.com/blogs/kevin/archive/tags/XAML/default.aspx">XAML</category><category domain="http://www.dotnetmafia.com/blogs/kevin/archive/tags/Silverlight/default.aspx">Silverlight</category><category domain="http://www.dotnetmafia.com/blogs/kevin/archive/tags/lolcats/default.aspx">lolcats</category></item></channel></rss>