<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>A blog of questions and answers</title>
	<atom:link href="http://markcandelora.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://markcandelora.wordpress.com</link>
	<description>Hopefully more answers than questions</description>
	<lastBuildDate>Fri, 12 Aug 2011 18:00:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='markcandelora.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>A blog of questions and answers</title>
		<link>http://markcandelora.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://markcandelora.wordpress.com/osd.xml" title="A blog of questions and answers" />
	<atom:link rel='hub' href='http://markcandelora.wordpress.com/?pushpress=hub'/>
		<item>
		<title>SharePoint 2010 &amp; HTTPS network connections</title>
		<link>http://markcandelora.wordpress.com/2011/08/12/sharepoint-2010-https-network-connections/</link>
		<comments>http://markcandelora.wordpress.com/2011/08/12/sharepoint-2010-https-network-connections/#comments</comments>
		<pubDate>Fri, 12 Aug 2011 15:57:31 +0000</pubDate>
		<dc:creator>Mark Candelora</dc:creator>
				<category><![CDATA[SharePoint]]></category>
		<category><![CDATA[SharePoint Administration]]></category>
		<category><![CDATA[SSL]]></category>
		<category><![CDATA[Trusts]]></category>

		<guid isPermaLink="false">http://markcandelora.wordpress.com/?p=105</guid>
		<description><![CDATA[It&#8217;s really frustrating sometimes how Microsoft&#8217;s whitepapers are so specific to individual cases that they fail to apply to any useful scenario &#8211; particularly those scenarios that involve custom development.  Such is the case for making HTTPS web service calls from SharePoint 2010 (to external endpoints). The Goal Custom built web parts deployed into SharePoint [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=markcandelora.wordpress.com&amp;blog=15191860&amp;post=105&amp;subd=markcandelora&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s really frustrating sometimes how Microsoft&#8217;s whitepapers are so specific to individual cases that they fail to apply to any useful scenario &#8211; particularly those scenarios that involve custom development.  Such is the case for making HTTPS web service calls from SharePoint 2010 (to external endpoints).</p>
<h2>The Goal</h2>
<p>Custom built web parts deployed into SharePoint 2010 need to make web service calls to a 3rd party API which used an HTTPS endpoint.</p>
<h2>The Error</h2>
<p>Could not establish trust relationship for the SSL/TLS secure channel with authority &#8216;[some url]&#8216;</p>
<h2>My Flailings</h2>
<p>The typical cause of this is that the root certificate that the SSL endpoint uses is not trusted by the computer that is attempting to make the web service connection.  The typical resolution?  Open the certificates snap-in through the Windows Management Console and add the root certificate (or even the certificate for the SSL endpoint) to the Trusted Authorities node and you&#8217;re all good.  You can even test this by navigating to the endpoint with your browser; if you get a certificate warning it didn&#8217;t work, otherwise it&#8217;s gravy.</p>
<p>The issue (and the reason for this blog post) is that this doesn&#8217;t work; apparently SharePoint completely ignores the machine settings for the trusted root certificates.</p>
<h2>The Solution</h2>
<p>It&#8217;s actually quite simple &#8211; open Central Administration and go to the security page.  Find and click the Manage Trusts link. Add a new trust, and in the window that opens upload your root server certificate, give it a good name and boom, things start working &#8211; just like magic.</p>
<p>I also found a nice little PowerShell script that will automate the trust creation for you:</p>
<pre>$root = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2("C:\MyRootCertificate.cer");
New-SPTrustedRootAuthority -Name "my root certificate" -Certificate $root;</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/markcandelora.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/markcandelora.wordpress.com/105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/markcandelora.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/markcandelora.wordpress.com/105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/markcandelora.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/markcandelora.wordpress.com/105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/markcandelora.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/markcandelora.wordpress.com/105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/markcandelora.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/markcandelora.wordpress.com/105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/markcandelora.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/markcandelora.wordpress.com/105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/markcandelora.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/markcandelora.wordpress.com/105/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=markcandelora.wordpress.com&amp;blog=15191860&amp;post=105&amp;subd=markcandelora&amp;ref=&amp;feed=1" width="1" height="1" /><div class="sharedaddy sd-like-enabled"></div>]]></content:encoded>
			<wfw:commentRss>http://markcandelora.wordpress.com/2011/08/12/sharepoint-2010-https-network-connections/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ae3dd762634974aaff170ea7b8090a45?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mcandelo</media:title>
		</media:content>
	</item>
		<item>
		<title>Date parsing and formatting in XSL</title>
		<link>http://markcandelora.wordpress.com/2011/03/18/date-parsing-and-formatting-in-xsl/</link>
		<comments>http://markcandelora.wordpress.com/2011/03/18/date-parsing-and-formatting-in-xsl/#comments</comments>
		<pubDate>Fri, 18 Mar 2011 21:19:53 +0000</pubDate>
		<dc:creator>Mark Candelora</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://markcandelora.wordpress.com/?p=88</guid>
		<description><![CDATA[One task I&#8217;ve found to be the bane of my existence when dealing with XSL is date formatting.  There&#8217;s no a good way to do it, and it makes things extremely difficult always needing to write new XSL because no 2 dates ever have the same format. Since I&#8217;ve written more than my share of [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=markcandelora.wordpress.com&amp;blog=15191860&amp;post=88&amp;subd=markcandelora&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>One task I&#8217;ve found to be the bane of my existence when dealing with XSL is date formatting.  There&#8217;s no a good way to do it, and it makes things extremely difficult always needing to write new XSL because no 2 dates ever have the same format.</p>
<p>Since I&#8217;ve written more than my share of date parsing templates, I decided that I would try to do it once more, and this would be the last time I&#8217;d ever write one.  The DateFormatting.xsl contains callable templates for most common operations.</p>
<h3>Format Strings</h3>
<p>The majority of the templates require a format string for input and/or output.  The syntax for the string is as follows:</p>
<p><strong>m </strong>- the numeric value for the month i.e. <em>8</em>, <em>9</em>, or <em>10</em></p>
<p><strong>mm </strong>- the 2 digit padded numeric value for the month i.e. <em>08</em>, <em>09</em>, or <em>10</em></p>
<p><strong>mmm </strong>- the short month name i.e. <em>Aug</em>, <em>Sept</em>, or <em>Oct</em></p>
<p><strong>mmmm </strong>- the long month name i.e. <em>August</em>, <em>September</em>, or <em>October</em></p>
<p><strong>d </strong>- the numeric value for the date i.e. <em>8</em>, <em>9</em>, or <em>10</em></p>
<p><strong>dd </strong>- the 2 digit padded numeric value for the date i.e. <em>08</em>, <em>09</em>, or <em>10</em></p>
<p><strong>yy </strong>- the 2 digit century i.e. <em>08</em>, <em>09</em>, or <em>10</em></p>
<p><strong>yyy </strong>- the 4 digit century i.e. <em>2008</em>, <em>2009</em>, or <em>2010</em></p>
<p><strong>ww </strong>- the short day of the week i.e. <em>Sun</em>, <em>Mon</em>, or <em>Tue</em></p>
<p><strong>www</strong> &#8211; the full name for the day of the week i.e. <em>Sunday</em>, <em>Monday</em>, or <em>Tuesday</em></p>
<p>Any other characters are considered delimiters and are not evaluated as part of the date.</p>
<p>So the date <em>01-Jan-2011</em> would use the format &#8220;dd-mmm-yyy&#8221;</p>
<h2>Templates</h2>
<p>The templates are callable templates.  There are internal templates that begin with an underscore.  The main templates (with no underscore) are mostly wrappers around the internal templates and provide the ability to call the templates with strings instead of structured xml dates &#8211; I&#8217;ll explain the main ones below:</p>
<p><strong>FormatDate</strong> &#8211; takes in a date string and returns a new date string in the specified format.  It accepts 3 parameters: inputFormatString, outputFormatString, &amp; dateTimeString.  The inputFormatString specifies the format of the dateTimeString parameter.  The outputFormatString specifies the desired format of the returned date.  The dateTimeString contains the actual date to be formatted (pretty obvious I would think, but you never know).</p>
<p>Example:</p>
<p><a href="http://markcandelora.files.wordpress.com/2011/03/formatdateexample.jpg"><img class="alignleft size-full wp-image-92" title="FormatDateExample" src="http://markcandelora.files.wordpress.com/2011/03/formatdateexample.jpg?w=481&#038;h=79" alt="" width="481" height="79" /></a></p>
<p>.</p>
<p>.</p>
<p>.</p>
<p>Result:   20100130</p>
<p><strong>ParseDate</strong> &#8211; parses a date and returns a new XML structure containing the raw date values.  Input params are the format string and the date string.</p>
<p>Ex:</p>
<p><a href="http://markcandelora.files.wordpress.com/2011/03/parsedateexample.jpg"><img class="alignleft size-full wp-image-93" title="ParseDateExample" src="http://markcandelora.files.wordpress.com/2011/03/parsedateexample.jpg?w=461&#038;h=63" alt="" width="461" height="63" /></a></p>
<p>.</p>
<p>.</p>
<p>.</p>
<p>Result:</p>
<p><a href="http://markcandelora.files.wordpress.com/2011/03/parsedateresult.jpg"><img class="alignleft size-full wp-image-94" title="ParseDateResult" src="http://markcandelora.files.wordpress.com/2011/03/parsedateresult.jpg?w=296&#038;h=79" alt="" width="296" height="79" /></a></p>
<p><strong>.</strong></p>
<p><strong>.</strong></p>
<p><strong>.</strong></p>
<p><strong>CalculateDayOfWeek</strong> &#8211; parses a date and returns the numeric day of the week. Sunday = 1, Monday = 2, etc&#8230;</p>
<p>Ex:</p>
<p><a href="http://markcandelora.files.wordpress.com/2011/03/calculatedayofweekexample.jpg"><img class="alignleft size-full wp-image-98" title="CalculateDayOfWeekExample" src="http://markcandelora.files.wordpress.com/2011/03/calculatedayofweekexample.jpg?w=434&#038;h=64" alt="" width="434" height="64" /></a></p>
<p>.</p>
<p>.</p>
<p>.</p>
<p>Result:  4</p>
<p>** The rest of these follow a similar format and return a number or formatted string so I will not include the examples or results for the sake of brevity&#8230;</p>
<p><strong><span style="font-weight:normal;"><strong><span style="font-weight:normal;"><strong>CalculateTotal</strong></span></strong></span>DaysSinceEpoch</strong> &#8211; parses a date and returns the number of days since January 1, 0000.</p>
<p><strong><strong><strong><strong>CalculateTotal</strong></strong></strong>SecondsSinceEpoch</strong> &#8211; this method is somewhat pointless since the script does not take into account time &#8211; only dates.  So right now this just takes the DaysSinceEpoch and multiplies it by the seconds in a day.</p>
<p><strong>MonthShortString </strong>- returns the short name of the provided month.  i.e. &#8220;Jan&#8221;, &#8220;Feb&#8221;&#8230;</p>
<p><strong>MonthLongString </strong>- returns the full name of the provided month. i.e. &#8220;January&#8221;, &#8220;February&#8221;&#8230;</p>
<p><strong>MonthFromShortName </strong>- returns the month number from the provided short name.</p>
<p><strong>MonthFromLongName </strong>- returns the month number from the provided long name.</p>
<p><strong>WeekDayLongName </strong>- returns the full name of the provided weekday number.</p>
<p><strong>WeekDayShortName </strong>- returns the short name of the provided weekday number.</p>
<p><strong>WeekDayFromLongName </strong>- returns the day number from the full weekday name.</p>
<p><strong>WeekDayFromShortName </strong>- returns the day number from the short weekday name.</p>
<p>You can find the full XSL code along with a test stylesheet and some sample data <a title="XSL Date Formatter" href="https://docs.google.com/leaf?id=0BxcGUKoQiXmJMGI1MDM4MzYtMGFhNS00ZjFkLTgxMmYtMmJlZmQwMjk1NTc0&amp;hl=en">here</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/markcandelora.wordpress.com/88/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/markcandelora.wordpress.com/88/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/markcandelora.wordpress.com/88/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/markcandelora.wordpress.com/88/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/markcandelora.wordpress.com/88/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/markcandelora.wordpress.com/88/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/markcandelora.wordpress.com/88/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/markcandelora.wordpress.com/88/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/markcandelora.wordpress.com/88/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/markcandelora.wordpress.com/88/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/markcandelora.wordpress.com/88/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/markcandelora.wordpress.com/88/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/markcandelora.wordpress.com/88/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/markcandelora.wordpress.com/88/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=markcandelora.wordpress.com&amp;blog=15191860&amp;post=88&amp;subd=markcandelora&amp;ref=&amp;feed=1" width="1" height="1" /><div class="sharedaddy sd-like-enabled"></div>]]></content:encoded>
			<wfw:commentRss>http://markcandelora.wordpress.com/2011/03/18/date-parsing-and-formatting-in-xsl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ae3dd762634974aaff170ea7b8090a45?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mcandelo</media:title>
		</media:content>

		<media:content url="http://markcandelora.files.wordpress.com/2011/03/formatdateexample.jpg" medium="image">
			<media:title type="html">FormatDateExample</media:title>
		</media:content>

		<media:content url="http://markcandelora.files.wordpress.com/2011/03/parsedateexample.jpg" medium="image">
			<media:title type="html">ParseDateExample</media:title>
		</media:content>

		<media:content url="http://markcandelora.files.wordpress.com/2011/03/parsedateresult.jpg" medium="image">
			<media:title type="html">ParseDateResult</media:title>
		</media:content>

		<media:content url="http://markcandelora.files.wordpress.com/2011/03/calculatedayofweekexample.jpg" medium="image">
			<media:title type="html">CalculateDayOfWeekExample</media:title>
		</media:content>
	</item>
		<item>
		<title>SharePoint 2010 &#8211; Cannot complete this action. Please Try again.</title>
		<link>http://markcandelora.wordpress.com/2011/02/11/cannot-complete-this-action-please-try-again/</link>
		<comments>http://markcandelora.wordpress.com/2011/02/11/cannot-complete-this-action-please-try-again/#comments</comments>
		<pubDate>Fri, 11 Feb 2011 14:00:35 +0000</pubDate>
		<dc:creator>Mark Candelora</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[SharePoint]]></category>
		<category><![CDATA[WebPart]]></category>

		<guid isPermaLink="false">http://markcandelora.wordpress.com/?p=81</guid>
		<description><![CDATA[Recently while debugging a web part in SharePoint 2010, I received this error message.  I had never seen it before but after doing some searching, I found that apparently this happens quite a lot in SharePoint. First thing I tried was to open the page in ?contents=1 mode and found the following: Once the erroneous [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=markcandelora.wordpress.com&amp;blog=15191860&amp;post=81&amp;subd=markcandelora&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Recently while debugging a web part in SharePoint 2010, I received this error message.  I had never seen it before but after doing some searching, I found that apparently this happens quite a lot in SharePoint.</p>
<p><a href="http://markcandelora.files.wordpress.com/2011/02/fullstacktrace.jpg"><img class="alignnone size-medium wp-image-84" title="FullStackTrace" src="http://markcandelora.files.wordpress.com/2011/02/fullstacktrace.jpg?w=300&#038;h=125" alt="" width="300" height="125" /></a></p>
<p>First thing I tried was to open the page in ?contents=1 mode and found the following:</p>
<p><a href="http://markcandelora.files.wordpress.com/2011/02/errorwebpart.jpg"><img class="alignnone size-medium wp-image-83" title="ErrorWebPart" src="http://markcandelora.files.wordpress.com/2011/02/errorwebpart.jpg?w=300&#038;h=114" alt="" width="300" height="114" /></a></p>
<p>Once the erroneous web parts were removed, I redeployed my solution and added my new webpart onto the page, but alas, still getting the same error.</p>
<p>So, I decided to take a closer look at my code, more specifically, the list I was deploying, and the code used to query it.  After more than a few hours of staring at the answer (and even almost giving up and putting a post for help on stackoverflow.com) I noticed this:</p>
<pre><code>&lt;Where&gt;
  &lt;FieldRef Name="Source" /&gt;
  &lt;Value Type="Text"&gt;http://dev01:80/Pages/default.aspx&lt;/Value&gt;
&lt;/Where&gt;</code></pre>
<p>Damn! Poor CAML syntax &#8211; the bane of my existance.  After changing the CAML so it was formatted properly:</p>
<pre><code>&lt;Where&gt;
  &lt;Eq&gt;
    &lt;FieldRef Name="Source" /&gt;
    &lt;Value Type="Text"&gt;http://dev01:80/Pages/default.aspx&lt;/Value&gt;
  &lt;/Eq&gt;
&lt;/Where&gt;</code></pre>
<p>Everything started working as it should.  Why Microsoft decided to stick us with this crappy query language then provide us with no means of validating it <em>and</em> give us completely ambiguous errors and COM errors on top of it all (or below it all depending on how you look at it) just makes me question why I ever decided to use Microsoft products to begin with.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/markcandelora.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/markcandelora.wordpress.com/81/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/markcandelora.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/markcandelora.wordpress.com/81/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/markcandelora.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/markcandelora.wordpress.com/81/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/markcandelora.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/markcandelora.wordpress.com/81/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/markcandelora.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/markcandelora.wordpress.com/81/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/markcandelora.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/markcandelora.wordpress.com/81/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/markcandelora.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/markcandelora.wordpress.com/81/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=markcandelora.wordpress.com&amp;blog=15191860&amp;post=81&amp;subd=markcandelora&amp;ref=&amp;feed=1" width="1" height="1" /><div class="sharedaddy sd-like-enabled"></div>]]></content:encoded>
			<wfw:commentRss>http://markcandelora.wordpress.com/2011/02/11/cannot-complete-this-action-please-try-again/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ae3dd762634974aaff170ea7b8090a45?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mcandelo</media:title>
		</media:content>

		<media:content url="http://markcandelora.files.wordpress.com/2011/02/fullstacktrace.jpg?w=300" medium="image">
			<media:title type="html">FullStackTrace</media:title>
		</media:content>

		<media:content url="http://markcandelora.files.wordpress.com/2011/02/errorwebpart.jpg?w=300" medium="image">
			<media:title type="html">ErrorWebPart</media:title>
		</media:content>
	</item>
		<item>
		<title>SharePoint Themes and how to make them work</title>
		<link>http://markcandelora.wordpress.com/2010/11/11/sharepoint-themes-and-how-to-make-them-work/</link>
		<comments>http://markcandelora.wordpress.com/2010/11/11/sharepoint-themes-and-how-to-make-them-work/#comments</comments>
		<pubDate>Thu, 11 Nov 2010 17:42:42 +0000</pubDate>
		<dc:creator>Mark Candelora</dc:creator>
				<category><![CDATA[SharePoint]]></category>
		<category><![CDATA[UI]]></category>

		<guid isPermaLink="false">http://markcandelora.wordpress.com/?p=75</guid>
		<description><![CDATA[I&#8217;ll start at the end because I think I&#8217;m going to forget this the most often: How to get a CSS file to be compiled by the theme engine First of all, use the CssRegistration tag.  You&#8217;ll be happy you did when you don&#8217;t have to fight with SharePoint to get your CSS file to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=markcandelora.wordpress.com&amp;blog=15191860&amp;post=75&amp;subd=markcandelora&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ll start at the end because I think I&#8217;m going to forget this the most often:</p>
<h2>How to get a CSS file to be compiled by the theme engine</h2>
<p>First of all, use the CssRegistration tag.  You&#8217;ll be happy you did when you don&#8217;t have to fight with SharePoint to get your CSS file to link properly.</p>
<pre style="font-family:arial;font-size:12px;border:1px dashed #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;"><code style="color:#000000;word-wrap:normal;"> &lt;SharePoint:CssRegistration Name="&lt;%$SPurl:~SiteCollection/Style Library/en-us/Themable/MyHome.css%&gt;" After="corev4.css" runat="server" /&gt;
</code></pre>
<p>The &#8220;&lt;%$SPurl&#8230;&#8221; ensures that the url points to the current version (themed or non themed) of the css file.  Be sure to put this tag right after the SharePoint:CssLink tag, I&#8217;ve tried putting it in other places and for some unknown reason it breaks things pretty badly.</p>
<p>Place your CSS file under the themeable directory under the Style Library like the URL shown in the CssRegistration tag.</p>
<p>And now the secret sauce &#8211; CHECK IN AND PUBLISH YOUR CSS AND IMAGE FILES!</p>
<p>The theme engine will not pick up any non-published versions, you MUST check in your CSS and image files.</p>
<p>Finally, apply a theme &#8211; any old theme.</p>
<p>You&#8217;ll notice that when you apply the theme, and look at the page source your css files will have moved into a different directory:</p>
<pre style="font-family:arial;font-size:12px;border:1px dashed #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;"><code style="color:#000000;word-wrap:normal;"> &lt;link rel="stylesheet" type="text/css" href="/_catalogs/theme/Themed/9262B884/MyHome-4277C871.css?ctag=44"&gt;
</code></pre>
<p>Weird huh?</p>
<p>When you applied the style, the theme engine picked up all the style sheets and images and performed its magic on them.  Then it saved them to this separate location.</p>
<p>This means that any changes you now make to your original css or image files will not be reflected in your newly themed site &#8211; yes, that&#8217;s right, you need to go back and reapply your theme to the site so the theme engine will recompile the CSS and image files.  Man that&#8217;s annoying isn&#8217;t it?!</p>
<h2>Using themes in your CSS files</h2>
<p>Below are a list of the theme commands you can use in your CSS file.  The commands will replace CSS attributes or recolor images as appropriate if used properly.  But first, there&#8217;s a mysterious comment at the top of the corev4.css file that I&#8217;ve noticed:</p>
<p>/* _lcid=&#8221;1033&#8243; _version=&#8221;14.0.4762&#8243;<br />
_LocalBinding */</p>
<p>I don&#8217;t know what it does, and as far as I can tell, this page is the first reference to this comment anywhere on the interwebs.  For now I&#8217;ll leave this at the top, because it&#8217;s there in corev4.css and I want my style sheet to be just like corev4.css when it grows up.</p>
<p>Next &#8211; there are a few commands that can be used to theme-up your stylesheet:</p>
<p><strong>ReplaceColor(themeColor)</strong> &#8211; should be put before a color attribute (obviously), this will replace the attribute color with a color from your theme.</p>
<p><strong>ReplaceFont(themeFont)</strong> &#8211; this guy works like ReplaceColor, except with fonts.</p>
<p><strong>RecolorImage(<span style="font-weight:normal;"><strong>themeColor</strong></span>, method, includeRectangle)</strong> &#8211; should be put before an image reference &#8211; ie. url(&lt;image url&gt;).  This will color the image in the manner specified.  The only required parameter is the color, the other 2 params are optional:</p>
<p><em><span style="white-space:pre;"> </span>themeColor </em>- this one&#8217;s obvious, the color to use to re-color the image.<br />
<em><span style="white-space:pre;"> </span>method</em> &#8211; options here are: Filling, Blending, Tinting<br />
<em><span style="white-space:pre;"> </span>includeRectangle</em> &#8211; defines the section of the image to recolor i.e. {x:0,y:0,width:15,height:15} defines a rectangle starting at 0, 0 pixels, and is 15px high by 15px wide.</p>
<p>All of the above is pretty self-explanitory with the exception of the method parameter on the RecolorImage &#8211; for this, I refer you to an <a href="http://blogs.msdn.com/b/sharepointdesigner/archive/2010/04/09/working-with-the-sharepoint-theming-engine.aspx" target="_blank">excellent blog post from Microsoft&#8217;s SharePoint team</a> in which he shows examples of each recolor method, as well as all the permutations of the different colors.</p>
<p>One last thing &#8211; I&#8217;ve found themes to be not an extremely awful experience, just a painful one.  However, they can be useful every once in a while in limited situations.  For these limited situations it&#8217;s probably necessary to undo all the theming that&#8217;s in the corev4.css so that you can implement themes for only the elements that you wish.  The <a href="https://docs.google.com/leaf?id=0BxcGUKoQiXmJOTBlMjU4MTctYTQ4ZS00NGU5LTg2MDYtZjFmMWJkNDc4MTg1&amp;hl=en" target="_blank">ThemeOverride.css</a> stylesheet will override all the style rules in corev4.css, so that no theme colors or fonts will display.</p>
<p>To use the ThemeOverride.css stylesheet, just insert it between corev4.css and your custom css like so:</p>
<pre style="font-family:arial;font-size:12px;border:1px dashed #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;"><code style="color:#000000;word-wrap:normal;">      &lt;SharePoint:CssLink runat="server" Version="4" /&gt;
      &lt;SharePoint:CssRegistration Name="&lt;%$SPurl:~SiteCollection/Style Library/en-us/Themable/ThemeOverride.css%&gt;" After="corev4.css" runat="server" /&gt;
      &lt;SharePoint:CssRegistration Name="&lt;%$SPurl:~SiteCollection/Style Library/en-us/Themable/MyHome.css%&gt;"
           After="&lt;%$SPurl:~SiteCollection/Style Library/en-us/Themable/ThemeOverride.css%&gt;" runat="server" /&gt;
</code></pre>
<p>Be sure that you specify the ThemeOverride.css in the After attribute of your custom stylesheet.  Otherwise the ThemeOverride.css will be moved after your custom style sheet and will override all of your beautiful CSS work.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/markcandelora.wordpress.com/75/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/markcandelora.wordpress.com/75/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/markcandelora.wordpress.com/75/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/markcandelora.wordpress.com/75/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/markcandelora.wordpress.com/75/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/markcandelora.wordpress.com/75/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/markcandelora.wordpress.com/75/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/markcandelora.wordpress.com/75/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/markcandelora.wordpress.com/75/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/markcandelora.wordpress.com/75/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/markcandelora.wordpress.com/75/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/markcandelora.wordpress.com/75/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/markcandelora.wordpress.com/75/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/markcandelora.wordpress.com/75/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=markcandelora.wordpress.com&amp;blog=15191860&amp;post=75&amp;subd=markcandelora&amp;ref=&amp;feed=1" width="1" height="1" /><div class="sharedaddy sd-like-enabled"></div>]]></content:encoded>
			<wfw:commentRss>http://markcandelora.wordpress.com/2010/11/11/sharepoint-themes-and-how-to-make-them-work/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ae3dd762634974aaff170ea7b8090a45?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mcandelo</media:title>
		</media:content>
	</item>
		<item>
		<title>The path specified cannot be used at this time. Exception from (HRESULT: 0&#215;80070094)</title>
		<link>http://markcandelora.wordpress.com/2010/11/08/the-path-specified-cannot-be-used-at-this-time-exception-from-hresult-0x80070094/</link>
		<comments>http://markcandelora.wordpress.com/2010/11/08/the-path-specified-cannot-be-used-at-this-time-exception-from-hresult-0x80070094/#comments</comments>
		<pubDate>Mon, 08 Nov 2010 03:54:10 +0000</pubDate>
		<dc:creator>Mark Candelora</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://markcandelora.wordpress.com/?p=73</guid>
		<description><![CDATA[Recently, while attempting to deploy a new WSP file to my dev environment, I was presented with another exceptionally helpful error message: Error: The path specified cannot be used at this time. Exception from (HRESULT: 0&#215;80070094) It took me a little bit to figure out, but basically, I reset the SP Timer service and all [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=markcandelora.wordpress.com&amp;blog=15191860&amp;post=73&amp;subd=markcandelora&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Recently, while attempting to deploy a new WSP file to my dev environment, I was presented with another exceptionally helpful error message:</p>
<blockquote style="color:red;"><p>Error: The path specified cannot be used at this time. Exception from (HRESULT: 0&#215;80070094)</p></blockquote>
<p>It took me a little bit to figure out, but basically, I reset the SP Timer service and all was well again.</p>
<p>What&#8217;s interesting is that even after getting this error, SharePoint claimed that the solution was properly deployed &#8211; though I didn&#8217;t want to take any chances with finding bugs due to a partially deployed solution.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/markcandelora.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/markcandelora.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/markcandelora.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/markcandelora.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/markcandelora.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/markcandelora.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/markcandelora.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/markcandelora.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/markcandelora.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/markcandelora.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/markcandelora.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/markcandelora.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/markcandelora.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/markcandelora.wordpress.com/73/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=markcandelora.wordpress.com&amp;blog=15191860&amp;post=73&amp;subd=markcandelora&amp;ref=&amp;feed=1" width="1" height="1" /><div class="sharedaddy sd-like-enabled"></div>]]></content:encoded>
			<wfw:commentRss>http://markcandelora.wordpress.com/2010/11/08/the-path-specified-cannot-be-used-at-this-time-exception-from-hresult-0x80070094/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ae3dd762634974aaff170ea7b8090a45?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mcandelo</media:title>
		</media:content>
	</item>
		<item>
		<title>Setup incoming email for your SharePoint development VM</title>
		<link>http://markcandelora.wordpress.com/2010/09/03/setup-incoming-email-for-your-sharepoint-development-vm/</link>
		<comments>http://markcandelora.wordpress.com/2010/09/03/setup-incoming-email-for-your-sharepoint-development-vm/#comments</comments>
		<pubDate>Fri, 03 Sep 2010 22:53:05 +0000</pubDate>
		<dc:creator>Mark Candelora</dc:creator>
				<category><![CDATA[SharePoint]]></category>

		<guid isPermaLink="false">http://markcandelora.wordpress.com/?p=50</guid>
		<description><![CDATA[So today I was trying to figure out how to debug an event handler on an email-enabled list.  My dev environment is running on a VM on my laptop so I didn&#8217;t really have an exchange server I could hook up to.  So, configuring the SMTP service on my Windows 2008 machine was the way [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=markcandelora.wordpress.com&amp;blog=15191860&amp;post=50&amp;subd=markcandelora&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>So today I was trying to figure out how to debug an event handler on an email-enabled list.  My dev environment is running on a VM on my laptop so I didn&#8217;t really have an exchange server I could hook up to.  So, configuring the SMTP service on my Windows 2008 machine was the way to go.</p>
<p>In server manager, under features, click <em>Add Feature</em>, then select the SMTP service.  Accept the feature dependencies and finish the install.</p>
<p>﻿<a href="http://markcandelora.files.wordpress.com/2010/09/1-addfeature.jpg"><img class="alignnone size-medium wp-image-51" title="1-AddFeature" src="http://markcandelora.files.wordpress.com/2010/09/1-addfeature.jpg?w=300&#038;h=208" alt="" width="300" height="208" /></a></p>
<p>Open the IIS 6.0 manager, and right-click your new SMTP Virtual Server node, then click properties.  Under access, ensure your authentication method is set to annonymous and your relay restrictions allow anyone.  Remember, this is just a development setup, so hopefully noone will be hacking into your new SMTP server to send out spam for Viagra.</p>
<p><a href="http://markcandelora.files.wordpress.com/2010/09/2-authentication.jpg"><img class="alignnone size-medium wp-image-52" title="2-Authentication" src="http://markcandelora.files.wordpress.com/2010/09/2-authentication.jpg?w=300&#038;h=296" alt="" width="300" height="296" /></a> <a href="http://markcandelora.files.wordpress.com/2010/09/3-relay.jpg"><img class="alignnone size-medium wp-image-53" title="3-Relay" src="http://markcandelora.files.wordpress.com/2010/09/3-relay.jpg?w=300&#038;h=292" alt="" width="300" height="292" /></a></p>
<p>Also, you may want to assign an alias for your domain, but that&#8217;s not required.</p>
<p>Next, go into Central Administration -&gt; <em>System Settings</em> -&gt; <em>Configure incoming e-mail settings</em>.  Select advanced settings, and at the bottom plug in your machine name and the email drop folder &#8216;c:\inetpub\mailroot\drop&#8217; then hit OK.</p>
<p><a href="http://markcandelora.files.wordpress.com/2010/09/4-1-emailsettings.jpg"><img class="alignnone size-medium wp-image-54" title="4.1-EmailSettings" src="http://markcandelora.files.wordpress.com/2010/09/4-1-emailsettings.jpg?w=300&#038;h=100" alt="" width="300" height="100" /></a></p>
<p><a href="http://markcandelora.files.wordpress.com/2010/09/4-2-emailsettings.jpg"><img class="alignnone size-medium wp-image-55" title="4.2-EmailSettings" src="http://markcandelora.files.wordpress.com/2010/09/4-2-emailsettings.jpg?w=300&#038;h=202" alt="" width="300" height="202" /></a></p>
<p>Once that&#8217;s done we just need to enable the list to receive emails.  Just open your list (or create a new one) and go to list settings.  Under the communication section is a link for <em>Incoming e-mail settings</em>.  Select that and choose the settings appropriate for your list.  Note that you may want to configure this through a feature receiver or a list deployed in your solution (after all, isn&#8217;t that what we&#8217;re here for?).</p>
<p><a href="http://markcandelora.files.wordpress.com/2010/09/5-list-settings.jpg"><img class="alignnone size-full wp-image-56" title="5-List Settings" src="http://markcandelora.files.wordpress.com/2010/09/5-list-settings.jpg?w=480&#038;h=107" alt="" width="480" height="107" /></a></p>
<p>Now lets deploy and debug our event receiver.  In your Visual Studio solution, click run to deploy and begin debugging the solution.  Now, although normal list event receivers are run on the <em>w3wp.exe</em> process, list event receivers running on e-mail enabled lists will be run by the <em>OWSTIMER.EXE</em> process, so use the <em>Debug</em> -&gt; <em>Attach to Process&#8230;</em> option to find it.  You may need to check the <em>Show processes from all users</em> option.</p>
<p><a href="http://markcandelora.files.wordpress.com/2010/09/6-debug.jpg"><img class="alignnone size-full wp-image-57" title="6-Debug" src="http://markcandelora.files.wordpress.com/2010/09/6-debug.jpg?w=420&#038;h=284" alt="" width="420" height="284" /></a></p>
<p>Once we&#8217;ve got our event wired up and the proper process being debugged, we just need to fire up and email and see if this thing works.  So, unless you have an email client (or want to install one) on your virutal machine, back out to your host machine.  You&#8217;ll need to make sure that your host machine can connect to your VM over the network.  Now, in Outlook, go to <em>Tools</em> -&gt; <em>Account settings&#8230;</em> In the email tab, click on New&#8230; and select the <em>Microsoft Exchange, POP3, IMAP or HTTP</em> option.  On the next screen, don&#8217;t enter anything, but check the box at the bottom of the screen specifying that you want to manually configure your settings, and next choose the <em>Internet email</em> option.</p>
<p>On the next screen you&#8217;ll be asked for a bunch of settings for the SMTP server, your email address, user name and password. The important thing here is to set the SMTP server to your VM server address, we don&#8217;t have a POP3 or IMAP server set up on the machine, so incoming mail won&#8217;t work, but that shouldn&#8217;t matter since we just want to send mail to our new SMTP server.  Enter in some test email, and the username and password of your development user.  Click the <em>Test Account Settings&#8230;</em> button and you should receive the results I got below.</p>
<p><a href="http://markcandelora.files.wordpress.com/2010/09/7-email.jpg"><img class="alignnone size-medium wp-image-58" title="7-Email" src="http://markcandelora.files.wordpress.com/2010/09/7-email.jpg?w=300&#038;h=293" alt="" width="300" height="293" /></a></p>
<p>If you didn&#8217;t, it&#8217;s most likely because your VM isn&#8217;t responding at the address you plugged in for your <em>Outoing mail server (SMTP)</em> value.  Remember, this value doesn&#8217;t need to have the domain your VM is on, it just needs to be able to find the SMTP service at that address.  One extra way to test it is to open the mail drop folder (c:\inetpub\mailroot\drop) on your VM and watch the test email file show up there, then promptly disappear when the timer job runs.</p>
<p>Now just create a new email and send it to your new list email address &#8211; be sure to use the new account you just created.  Type in your subject and email body and send away.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/markcandelora.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/markcandelora.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/markcandelora.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/markcandelora.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/markcandelora.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/markcandelora.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/markcandelora.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/markcandelora.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/markcandelora.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/markcandelora.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/markcandelora.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/markcandelora.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/markcandelora.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/markcandelora.wordpress.com/50/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=markcandelora.wordpress.com&amp;blog=15191860&amp;post=50&amp;subd=markcandelora&amp;ref=&amp;feed=1" width="1" height="1" /><div class="sharedaddy sd-like-enabled"></div>]]></content:encoded>
			<wfw:commentRss>http://markcandelora.wordpress.com/2010/09/03/setup-incoming-email-for-your-sharepoint-development-vm/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ae3dd762634974aaff170ea7b8090a45?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mcandelo</media:title>
		</media:content>

		<media:content url="http://markcandelora.files.wordpress.com/2010/09/1-addfeature.jpg?w=300" medium="image">
			<media:title type="html">1-AddFeature</media:title>
		</media:content>

		<media:content url="http://markcandelora.files.wordpress.com/2010/09/2-authentication.jpg?w=300" medium="image">
			<media:title type="html">2-Authentication</media:title>
		</media:content>

		<media:content url="http://markcandelora.files.wordpress.com/2010/09/3-relay.jpg?w=300" medium="image">
			<media:title type="html">3-Relay</media:title>
		</media:content>

		<media:content url="http://markcandelora.files.wordpress.com/2010/09/4-1-emailsettings.jpg?w=300" medium="image">
			<media:title type="html">4.1-EmailSettings</media:title>
		</media:content>

		<media:content url="http://markcandelora.files.wordpress.com/2010/09/4-2-emailsettings.jpg?w=300" medium="image">
			<media:title type="html">4.2-EmailSettings</media:title>
		</media:content>

		<media:content url="http://markcandelora.files.wordpress.com/2010/09/5-list-settings.jpg" medium="image">
			<media:title type="html">5-List Settings</media:title>
		</media:content>

		<media:content url="http://markcandelora.files.wordpress.com/2010/09/6-debug.jpg" medium="image">
			<media:title type="html">6-Debug</media:title>
		</media:content>

		<media:content url="http://markcandelora.files.wordpress.com/2010/09/7-email.jpg?w=300" medium="image">
			<media:title type="html">7-Email</media:title>
		</media:content>
	</item>
		<item>
		<title>WritableBitmap finally usable, and very cool</title>
		<link>http://markcandelora.wordpress.com/2010/08/14/writablebitmap-finally-usable-and-very-cool/</link>
		<comments>http://markcandelora.wordpress.com/2010/08/14/writablebitmap-finally-usable-and-very-cool/#comments</comments>
		<pubDate>Sat, 14 Aug 2010 16:13:18 +0000</pubDate>
		<dc:creator>Mark Candelora</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://markcandelora.wordpress.com/?p=47</guid>
		<description><![CDATA[I just found a very cool set of extension methods that actually make Silverlight&#8217;s WritableBitmap class usable. The Codeplex project It&#8217;s going to allow for a much faster particle generator as well as some other very cool effects. Other links: http://blogs.silverarcade.com/silverlight-games-101/15/silverlight-blitting-and-blending-with-silverlights-writeablebitmap/ http://kodierer.blogspot.com/2009/11/drawing-shapes-silverlight.html<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=markcandelora.wordpress.com&amp;blog=15191860&amp;post=47&amp;subd=markcandelora&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I just found a very cool set of extension methods that actually make Silverlight&#8217;s WritableBitmap class usable.</p>
<p><a title="The Codeplex projct" href="http://writeablebitmapex.codeplex.com/" target="_self">The Codeplex project</a></p>
<p>It&#8217;s going to allow for a much faster particle generator as well as some other very cool effects.</p>
<p>Other links:</p>
<p>http://blogs.silverarcade.com/silverlight-games-101/15/silverlight-blitting-and-blending-with-silverlights-writeablebitmap/</p>
<p>http://kodierer.blogspot.com/2009/11/drawing-shapes-silverlight.html</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/markcandelora.wordpress.com/47/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/markcandelora.wordpress.com/47/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/markcandelora.wordpress.com/47/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/markcandelora.wordpress.com/47/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/markcandelora.wordpress.com/47/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/markcandelora.wordpress.com/47/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/markcandelora.wordpress.com/47/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/markcandelora.wordpress.com/47/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/markcandelora.wordpress.com/47/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/markcandelora.wordpress.com/47/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/markcandelora.wordpress.com/47/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/markcandelora.wordpress.com/47/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/markcandelora.wordpress.com/47/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/markcandelora.wordpress.com/47/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=markcandelora.wordpress.com&amp;blog=15191860&amp;post=47&amp;subd=markcandelora&amp;ref=&amp;feed=1" width="1" height="1" /><div class="sharedaddy sd-like-enabled"></div>]]></content:encoded>
			<wfw:commentRss>http://markcandelora.wordpress.com/2010/08/14/writablebitmap-finally-usable-and-very-cool/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ae3dd762634974aaff170ea7b8090a45?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mcandelo</media:title>
		</media:content>
	</item>
		<item>
		<title>A Silverlight tree control that only loads content when needed…</title>
		<link>http://markcandelora.wordpress.com/2010/08/13/a-silverlight-tree-control-that-only-loads-content-when-needed/</link>
		<comments>http://markcandelora.wordpress.com/2010/08/13/a-silverlight-tree-control-that-only-loads-content-when-needed/#comments</comments>
		<pubDate>Fri, 13 Aug 2010 21:48:00 +0000</pubDate>
		<dc:creator>Mark Candelora</dc:creator>
				<category><![CDATA[Silverlight 4]]></category>
		<category><![CDATA[UI]]></category>
		<category><![CDATA[The secret sauce in creating a tree control that loads content as needed.]]></category>

		<guid isPermaLink="false">http://markcandelora.wordpress.com/2010/08/13/a-silverlight-tree-control-that-only-loads-content-when-needed</guid>
		<description><![CDATA[I&#8217;ve had to re-write this thing a few times now, so I thought I&#8217;d make a generic one and post it here for my reference.  The control is a normal tree control that will query child elements from a web service or some other thing when that node&#8217;s parent has expanded. So, when the control [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=markcandelora.wordpress.com&amp;blog=15191860&amp;post=14&amp;subd=markcandelora&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div class="WordSection1">
<p class="MsoNormal">I&#8217;ve had to re-write this thing a few times now, so I  thought I&#8217;d make a generic one and post it here for my reference.  The  control is a normal tree control that will query child elements from a web  service or some other thing when that node&#8217;s parent has expanded. So, when the  control is first loaded, we will go out and query for the nodes that should  show at the root of the tree.  Next, we want to query for all the children  of those root elements and stop there.  Once a user decides to expand one  of the nodes, we have all of its children, so that&#8217;s fine, but we want the  children&#8217;s children (or 2 down).  That way the user doesn&#8217;t need to wait 2  or 3 seconds for the node&#8217;s contents to populate.</p>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal">There&#8217;s a few things to doing this that are not really  straight forward.  The first and most obvious is getting an event raised  when a node is expanded.  To do this, we need to customize the TreeView  and TreeViewItem.</p>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal">We&#8217;ll start with the TreeViewItem:</p>
<pre style="font-family:arial;font-size:12px;border:1px dashed #CCCCCC;width:99%;height:350px;overflow:auto;background:#f0f0f0;"><code style="color:#000000;word-wrap:normal;">1:    public class CustomTreeViewItem : TreeViewItem {
2:      public CustomTreeView ParentTreeView { set; get; }
3:      public CustomTreeViewItem() {
4:        this.Expanded += new RoutedEventHandler(CustomTreeViewItem_Expanded);
5:        this.Collapsed += new RoutedEventHandler(CustomTreeViewItem_Collapsed);
6:        this.MouseLeftButtonDown += new MouseButtonEventHandler(CustomTreeViewItem_MouseLeftButtonDown);
7:      }
8:      void CustomTreeViewItem_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
9:        this.ParentTreeView.InvokeContainerClicked(sender, e);
10:      }
11:      void CustomTreeViewItem_Collapsed(object sender, RoutedEventArgs e) {
12:        this.ParentTreeView.InvokeContainerCollapsed(sender, e);
13:      }
14:      void CustomTreeViewItem_Expanded(object sender, RoutedEventArgs e) {
15:        this.UpdateLayout();
16:        this.ParentTreeView.InvokeContainerExpanded(sender, e);
17:      }
18:      protected override bool IsItemItsOwnContainerOverride(object item) {
19:        return item is CustomTreeViewItem;
20:      }
21:      protected override DependencyObject GetContainerForItemOverride() {
22:        return new CustomTreeViewItem();
23:      }
24:      protected override void PrepareContainerForItemOverride(DependencyObject element, object item) {
25:        CustomTreeViewItem treeViewItemExtended = (CustomTreeViewItem)element;
26:        treeViewItemExtended.ParentTreeView = this.ParentTreeView;
27:        base.PrepareContainerForItemOverride(element, item);
28:      }
29:    }
</code></pre>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal">The routed event handlers are set up upon construction of the  object and when fired, they call the appropriate event on the ParentTreeView.   The other GetContainer &amp; PrepareContainer methods just wire up the control  for customization.</p>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal">Now the TreeView:</p>
<pre style="font-family:arial;font-size:12px;border:1px dashed #CCCCCC;width:99%;height:350px;overflow:auto;background:#f0f0f0;"><code style="color:#000000;word-wrap:normal;">1:    public class CustomTreeView : TreeView {
2:      protected override bool IsItemItsOwnContainerOverride(object item) {
3:        return item is CustomTreeViewItem;
4:      }
5:      protected override DependencyObject GetContainerForItemOverride() {
6:        return new CustomTreeViewItem();
7:      }
8:      protected override void PrepareContainerForItemOverride(DependencyObject element, object item) {
9:        CustomTreeViewItem treeViewItemExtended = (CustomTreeViewItem)element;
10:        treeViewItemExtended.ParentTreeView = this;
11:        base.PrepareContainerForItemOverride(element, item);
12:        this.InvokeRootItemCreated(element, new RoutedEventArgs());
13:      }
14:      public event RoutedEventHandler RootItemCreated;
15:      internal void InvokeRootItemCreated(object sender, RoutedEventArgs e) {
16:        if (this.RootItemCreated != null)
17:          this.RootItemCreated(sender, e);
18:      }
19:      public event RoutedEventHandler ContainerExpanded;
20:      internal void InvokeContainerExpanded(object sender, RoutedEventArgs e) {
21:        RoutedEventHandler expanded = ContainerExpanded;
22:        if (expanded != null)
23:          expanded(sender, e);
24:      }
25:      public event RoutedEventHandler ContainerCollapsed;
26:      internal void InvokeContainerCollapsed(object sender, RoutedEventArgs e) {
27:        RoutedEventHandler collapsed = ContainerCollapsed;
28:        if (collapsed != null)
29:          collapsed(sender, e);
30:      }
31:      public event MouseButtonEventHandler ContainerClicked;
32:      internal void InvokeContainerClicked(object sender, MouseButtonEventArgs e) {
33:        MouseButtonEventHandler clicked = ContainerClicked;
34:        if (clicked != null)
35:          clicked(sender, e);
36:      }
37:    }
</code></pre>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal">This class is the control we&#8217;ll use, so we need to expose  the TreeViewItem&#8217;s events through here.  Again, the other methods just  wire up the control for customization.</p>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal">The second not-so-obvious item is hidden within the CustomTreeViewItem.CustomTreeViewItem_Expanded()  method.  Notice the this.UpdateLayout() call?  Don&#8217;t forget this  little step before you fire the event or you&#8217;ll pull your hair out trying to  figure out why you&#8217;re getting NPE&#8217;s everywhere.  Basically, before  expanding the node, you may have assigned children to the item, but the ItemContainerGenerator  will not have generated the TreeViewItem yet.  If you debug it, you can  take a look at the ItemContainerGenerator&#8217;s _awaitingLoadIndices (Non-Public  member).  You&#8217;ll see it has a few items in it, and every call you make to ContainerFromItem()  or ContainerFromIndex() will result in NULL.</p>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal">Ok, everything else is pretty simple, so I&#8217;ll just end this  here.  The sample I&#8217;m including has a dummied Timer and item generation  method that you&#8217;ll want to replace with your web service call (or whatever  other call you need to make).</p>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal">Enjoy!</p>
<p class="MsoNormal"><a href="https://docs.google.com/leaf?id=0BxcGUKoQiXmJNDI1MjE0ZjUtMTliNi00ZDE1LTllYjktZmU3YmQ5YmRhZTI2&amp;hl=en" target="_self">Download the full project here</a></p>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/markcandelora.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/markcandelora.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/markcandelora.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/markcandelora.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/markcandelora.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/markcandelora.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/markcandelora.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/markcandelora.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/markcandelora.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/markcandelora.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/markcandelora.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/markcandelora.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/markcandelora.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/markcandelora.wordpress.com/14/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=markcandelora.wordpress.com&amp;blog=15191860&amp;post=14&amp;subd=markcandelora&amp;ref=&amp;feed=1" width="1" height="1" /><div class="sharedaddy sd-like-enabled"></div>]]></content:encoded>
			<wfw:commentRss>http://markcandelora.wordpress.com/2010/08/13/a-silverlight-tree-control-that-only-loads-content-when-needed/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ae3dd762634974aaff170ea7b8090a45?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mcandelo</media:title>
		</media:content>
	</item>
		<item>
		<title>Renaming a SharePoint dev server</title>
		<link>http://markcandelora.wordpress.com/2010/08/13/renaming-a-sharepoint-dev-server/</link>
		<comments>http://markcandelora.wordpress.com/2010/08/13/renaming-a-sharepoint-dev-server/#comments</comments>
		<pubDate>Fri, 13 Aug 2010 15:42:00 +0000</pubDate>
		<dc:creator>Mark Candelora</dc:creator>
				<category><![CDATA[SharePoint]]></category>

		<guid isPermaLink="false">http://markcandelora.wordpress.com/2010/08/13/renaming-a-sharepoint-dev-server</guid>
		<description><![CDATA[When setting up a dev server (or any server for that matter) you should always rename the machine after you&#8217;ve installed Windows and before setting up any other applications.  This is particularly true if you&#8217;re setting up a SharePoint server, as renaming the server after you&#8217;ve set up SharePoint has disastrous effects. &#160; However, if [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=markcandelora.wordpress.com&amp;blog=15191860&amp;post=15&amp;subd=markcandelora&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div class="WordSection1">
<p class="MsoNormal">When setting up a dev server (or any server for that matter)  you should always rename the machine after you&#8217;ve installed Windows and before  setting up any other applications.  This is particularly true if you&#8217;re  setting up a SharePoint server, as renaming the server after you&#8217;ve set up SharePoint  has disastrous effects.</p>
<p class="MsoNormal"><a href="http://markcandelora.files.wordpress.com/2010/08/error.png"><img class="alignnone size-medium wp-image-67" title="error" src="http://markcandelora.files.wordpress.com/2010/08/error.png?w=300&#038;h=231" alt="" width="300" height="231" /></a></p>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal">However, if you&#8217;ve forgotten to rename your server (like I  did) and you just installed SharePoint and are getting sick of typing in names  like <em>http://win-87efgyaklq (like I am)</em>, there is a way out &#8211; as long as  you&#8217;re willing to part with your content databases.  There&#8217;s probably a  way to do it without losing all your content, but I haven&#8217;t figured that one  out yet&#8230;</p>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal"><span class="MsoBookTitle">Step 1. Detach the SharePoint  server from the farm.</span></p>
<p class="MsoNormal">Open the SharePoint Configuration Wizard and select &#8220;Disconnect  from this server Farm&#8221;.</p>
<p class="MsoNormal"><a href="http://markcandelora.files.wordpress.com/2010/08/detach.png"><img class="alignnone size-medium wp-image-66" title="detach" src="http://markcandelora.files.wordpress.com/2010/08/detach.png?w=300&#038;h=255" alt="" width="300" height="255" /></a></p>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal">Click Next, Yes, then Finish.</p>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal"><span class="MsoBookTitle">Step 2. Rename the Server</span></p>
<p class="MsoNormal">Don&#8217;t forget to do it this time&#8230; it&#8217;s the whole reason  we&#8217;re going through this mess.</p>
<p class="MsoNormal">Now we want to recreate the farm, but first we want to clean  out all the content databases, since we really can&#8217;t use them (they have  references to the old machine name).</p>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal"><span class="MsoBookTitle">Step 3. Delete the SharePoint  content &amp; configuration databases</span></p>
<p class="MsoNormal">Fire up SQL Server Management Studio and log into your  database.  Open the Object Explorer Details and highlight all of your SharePoint  Databases.  Note my Forms Based Auth. database below can stay as we can  still use this after the machine name changes.</p>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal"><a href="http://markcandelora.files.wordpress.com/2010/08/deletedb.png"><img class="alignnone size-medium wp-image-65" title="deleteDB" src="http://markcandelora.files.wordpress.com/2010/08/deletedb.png?w=300&#038;h=246" alt="" width="300" height="246" /></a></p>
<p class="MsoNormal">Right Click -&gt; Delete.  You&#8217;ll probably want to  check the box that says to close all existing connections before deleting  otherwise you&#8217;ll get a few errors (SharePoint keeps DB connections in its connection  pool open for a while).</p>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal"><span class="MsoBookTitle">Step 4. Recreate your farm</span></p>
<p class="MsoNormal">Fire up your SharePoint configuration wizard again and this  time you want to create a new farm.</p>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal">And that&#8217;s it, you&#8217;re done.  Well, after you create  your web app and site collections, but you should already know how to do that &#8211;  <em>right</em>?</p>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/markcandelora.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/markcandelora.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/markcandelora.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/markcandelora.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/markcandelora.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/markcandelora.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/markcandelora.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/markcandelora.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/markcandelora.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/markcandelora.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/markcandelora.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/markcandelora.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/markcandelora.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/markcandelora.wordpress.com/15/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=markcandelora.wordpress.com&amp;blog=15191860&amp;post=15&amp;subd=markcandelora&amp;ref=&amp;feed=1" width="1" height="1" /><div class="sharedaddy sd-like-enabled"></div>]]></content:encoded>
			<wfw:commentRss>http://markcandelora.wordpress.com/2010/08/13/renaming-a-sharepoint-dev-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ae3dd762634974aaff170ea7b8090a45?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mcandelo</media:title>
		</media:content>

		<media:content url="http://markcandelora.files.wordpress.com/2010/08/error.png?w=300" medium="image">
			<media:title type="html">error</media:title>
		</media:content>

		<media:content url="http://markcandelora.files.wordpress.com/2010/08/detach.png?w=300" medium="image">
			<media:title type="html">detach</media:title>
		</media:content>

		<media:content url="http://markcandelora.files.wordpress.com/2010/08/deletedb.png?w=300" medium="image">
			<media:title type="html">deleteDB</media:title>
		</media:content>
	</item>
		<item>
		<title>&quot;Simple&quot; Particle Generator Using Silverlight 4&#8230; Part 1</title>
		<link>http://markcandelora.wordpress.com/2010/08/11/simple-particle-generator-using-silverlight-4-part-1/</link>
		<comments>http://markcandelora.wordpress.com/2010/08/11/simple-particle-generator-using-silverlight-4-part-1/#comments</comments>
		<pubDate>Wed, 11 Aug 2010 20:44:00 +0000</pubDate>
		<dc:creator>Mark Candelora</dc:creator>
				<category><![CDATA[Expression Blend]]></category>
		<category><![CDATA[Silverlight 4]]></category>
		<category><![CDATA[UI]]></category>

		<guid isPermaLink="false">http://markcandelora.wordpress.com/2010/08/11/simple-particle-generator-using-silverlight-4-part-1</guid>
		<description><![CDATA[Since this is my first blog post, I thought I&#8217;d start with something simple.  Today I&#8217;m going to walk through creating a particle generator using Silverlight 4.  Now, I have a few requirements when creating this thing, and if anyone has any other stuff that I didn&#8217;t think of, feel free to call me out. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=markcandelora.wordpress.com&amp;blog=15191860&amp;post=16&amp;subd=markcandelora&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Since this is my first blog post, I thought I&#8217;d start with something simple.  Today I&#8217;m going to walk through creating a particle generator using Silverlight 4.  Now, I have a few requirements when creating this thing, and if anyone has any other stuff that I didn&#8217;t think of, feel free to call me out.</p>
<ol>
<li>The most extremely, very, super important requirement is that it has to be <em>fast</em>.  If this thing is going to be usable at all, we&#8217;ll need it to be able to scale up to animating thousands of particles at frame rates of over 60fps.</li>
<li> The control has to be customizable.  We want people to be able to apply their own algorithms for how the particles move (or don&#8217;t move), what they bounce off of, etc.</li>
<li>Oh, now I got a bit ahead of myself &#8211; the particle generator must be a control in a separate class library.  One that can be referenced in any project and dropped into a page, control or otherwise.</li>
<li>Now I&#8217;m going to throw a little wrench in and say that we want to animate the particles in 3D.  Now, I know what you&#8217;re thinkin&#8217; (or maybe I don&#8217;t), but it&#8217;s not that big a deal &#8211; Silverlight has some nifty tricks that will help us out with this one.</li>
</ol>
<p>Now to accomplish the first task, we&#8217;re going to do a few things that are a bit different than how we would normally play with Silverlight. First, most of our control is going to be written in C#, with only bits and pieces done with XAML.  Second, we are going to use structs wherever it makes sense instead of using classes.  This keeps our memory management simple, and prevents the interpreter from needing to dereference objects.  For similar reasons, we want to use Arrays instead of lists or any other data structure as much as possible.  Third, we&#8217;re going to make this sucker multi-threaded.  Now, this has some limitations that I&#8217;m not very fond of, but keeping the majority of our calculations off the UI thread will make things much faster.</p>
<p>The second and third tasks are fairly straight forward, but we&#8217;ll walk through how to do them and why later.  The fourth requirement however, is a bit tricky.  If you&#8217;re new to Silverlight, I&#8217;m not exactly sure why you&#8217;d be reading this, but there&#8217;s a transformation you can apply to your controls that will move them in 3D space, then project them back into 2D to fit on your screen.  This is the part that Silverlight will do for us, so that makes our lives much simpler.</p>
<p>However, Silverlight is basically a 2D application, and has no other real facilities for doing complex calculations in 3D space, so this means we&#8217;ll need to create the objects, and do the calculations ourselves.  Luckily, this is a very well understood area of mathematics, so be prepared to put your Trigonometry hat on.</p>
<p>The basics of a particle generator are very simple:</p>
<ol>
<li>Create a bunch of UI elements along with a few other variables to track direction, speed (together called velocity) and a few other variables.</li>
<li>Create a timer that will fire a few times a second (well, hopefully many times per second) to move your particles.</li>
<li>Each call of the timer will loop through all the elements and update the velocity (speed and/or direction), then apply the velocity to the element&#8217;s current position.</li>
</ol>
<p>Now, normally a super-duper simple rendition of this <em>is <span style="font-style:normal;">actually </span></em>pretty simple to write (probably about 50 or so lines of code for the entire deal).  But some of our requirements put a big dent into that &#8220;super-duper simple&#8221; description &#8211; so we&#8217;re going to modify that algorithm I just ran through (note, this algorithm will change many times over the course of this little tutorial, so don&#8217;t worry if you think this sounds too simple):</p>
<ol>
<li>Create a bunch of UI elements along with a few other variables to track direction, speed and a few other variables.</li>
<li>Create a timer that will fire <em>on a separate thread</em> a few times a second (well, hopefully many times per second) to move your particles.</li>
<li>Each call of the timer will loop through all the elements and update the velocity, then apply the velocity to the <span style="text-decoration:line-through;">element&#8217;s</span><span style="color:#666666;"> </span>current position <em>variable</em>.</li>
<li><em>Submit a job to the UI thread to apply the current position to the projection transform of the UI element.</em></li>
</ol>
<p>Note the timer in step two: every time it ticks, the timeout method will kick off on a different thread other than the UI thread.  Now threading in Silverlight is a bit different than threading with normal Windows applications &#8211; if we&#8217;re not on the UI thread, we cannot touch any UI controls.  Hence the changes to step 3 and 4: calculations done on the timer thread are saved to a set of variables, then we submit a job to the UI thread (through the control&#8217;s dispatcher) that will update the elements on the form.</p>
<p>Now that we&#8217;ve got the basic algorithm figured out, lets dig into some code and see how this works&#8230;</p>
<p>Open up Visual Studio, then select Silverlight on the left and the Silverlight Class Library on the right:</p>
<div class="separator" style="clear:both;text-align:center;"><a style="margin-left:1em;margin-right:1em;" href="http://markcandelora.files.wordpress.com/2010/08/visualstudiotemplate1.gif"><img src="http://markcandelora.files.wordpress.com/2010/08/visualstudiotemplate1.gif?w=300" border="0" alt="" /></a></div>
<div class="separator" style="clear:both;text-align:left;">Name the project what ever you&#8217;d like.  I&#8217;ve named mine &#8220;ParticleGenerator&#8221;.  I know, creative right?  Be sure to select Silverlight 4 for the runtime, though with some minor tweaks you could probably do this with Silverlight 3 as well.</div>
<div class="separator" style="clear:both;text-align:left;">Delete the random class that the template provides for you.  We&#8217;re going to start by creating some of the supporting classes (and structs) we&#8217;ll need to keep track of things: Point3D &amp; Particle.</div>
<div class="separator" style="clear:both;text-align:left;">Before we get into the code, I have one disclaimer: to keep this blog as short as possible, I haven&#8217;t added in any error handling.  I could rant a little bit about how important error handling is, but again, I&#8217;m <em>attempting</em> to keep this short.</div>
<div class="separator" style="clear:both;text-align:left;"><span style="font-size:x-large;"><strong>Point3D</strong></span></div>
<div class="separator" style="clear:both;text-align:left;">The Point3D struct will serve two main functions.  It will be a normal point in 3D space, holding the X, Y, and Z positions as well as some functions that allow us to move the point around.  It will also be a vector.  A vector is a point in reference to something else.  So we can use a vector to hold our velocity.  More on this in a little bit.</div>
<pre style="font-family:arial;font-size:12px;border:1px dashed #CCCCCC;width:90%;height:350px;overflow:auto;background:#f0f0f0;"><code style="color:#000000;word-wrap:normal;">1:  using System;
2:  namespace ParticleGenerator {
3:    public struct Point3D {
4:      public double x { get; set; }
5:      public double y { get; set; }
6:      public double z { get; set; }
7:      public double length {
8:        get {
9:          double returnValue;
10:          double x2 = this.x * this.x;
11:          double y2 = this.y * this.y;
12:          double z2 = this.z * this.z;
13:          returnValue = Math.Sqrt(x2 + y2 + z2);
14:          return returnValue;
15:        }
16:      }
17:      public void add(Point3D p) {
18:        this.x += p.x;
19:        this.y += p.y;
20:        this.z += p.z;
21:      }
22:      public void subtract(Point3D p) {
23:        this.x -= p.x;
24:        this.y -= p.y;
25:        this.z -= p.z;
26:      }
27:      public void scale(double n) {
28:        this.x *= n;
29:        this.y *= n;
30:        this.z *= n;
31:      }
32:      public void lerp(Point3D p, double ratio) {
33:        if (p.x != this.x) {
34:          double x = ((p.x - this.x) * ratio) + this.x;
35:          this.y = this.y + (x - this.x) * ((p.y - this.y) / (p.x - this.x));
36:          this.z = this.z + (x - this.x) * ((p.z - this.z) / (p.x - this.x));
37:          this.x = x;
38:        } else if (p.y != this.y) {
39:          double y = ((p.y - this.y) * ratio) + this.y;
40:          this.x = this.x + (y - this.y) * ((p.x - this.x) / (p.y - this.y));
41:          this.z = this.z + (y - this.y) * ((p.z - this.z) / (p.y - this.y));
42:          this.y = y;
43:        } else if (p.z != this.z) {
44:          double z = ((p.z - this.z) * ratio) + this.z;
45:          this.x = this.x + (z - this.z) * ((p.x - this.x) / (p.z - this.z));
46:          this.y = this.y + (z - this.z) * ((p.y - this.y) / (p.z - this.z));
47:          this.z = z;
48:        }
49:      }
50:      public override string ToString() {
51:        return string.Format("{3} {0}, {1}, {2} {4}", this.x.ToString("#0.0"), this.y.ToString("#0.0"), this.z.ToString("#0.0"), '{', '}');
52:      }
53:      public static Point3D add(Point3D p1, Point3D p2) {
54:        Point3D returnValue = new Point3D();
55:        returnValue.x = p1.x + p2.x;
56:        returnValue.y = p1.y + p2.y;
57:        returnValue.z = p1.z + p2.z;
58:        return returnValue;
59:      }
60:      public static Point3D subtract(Point3D p1, Point3D p2) {
61:        Point3D returnValue = new Point3D();
62:        returnValue.x = p1.x - p2.x;
63:        returnValue.y = p1.y - p2.y;
64:        returnValue.z = p1.z - p2.z;
65:        return returnValue;
66:      }
67:      public static Point3D lerp(Point3D p1, Point3D p2) {
68:        return Point3D.lerp(p1, p2, 0.5);
69:      }
70:      public static Point3D lerp(Point3D p1, Point3D p2, double ratio) {
71:        Point3D returnValue = p1;
72:        returnValue.lerp(p2, ratio);
73:        return returnValue;
74:      }
75:      public static Point3D scale(Point3D p, double n) {
76:        Point3D returnValue = p;
77:        returnValue.scale(n);
78:        return returnValue;
79:      }
80:    }
81:  }
</code></pre>
<p>Ok, lets review this a piece at a time.  First, we have 3 public properties &#8211; x, y, z.  Pretty obvious, they hold our coordinates in 3D space.</p>
<p>Next, we have 2 sets of methods, static methods and public methods.  Yes, they&#8217;re redundant &#8211; with small classes like this I like to have another set of static methods for ease of use. You don&#8217;t have to implement them if you don&#8217;t want, or you can just copy and paste.  Either way.</p>
<p>I&#8217;ll lightly brush over what these methods do, but the inner workings behind these guys are out of scope of this blog&#8230;</p>
<p><strong>Length</strong> &#8211; Applys only to vectors.  This will measure the distance from {0, 0, 0} to {x, y, z}.  We&#8217;ll use this to find our speed.<br />
<strong>Add</strong> &#8211; Applys to both vectors and points.  This simply adds the x1+x2, y1+y2, z1+z2.  We&#8217;ll use this to apply our velocity to our current position (along with a few other minor things).<br />
<strong>Subtract</strong> &#8211; Opposite of add.  We won&#8217;t actually use this one (I don&#8217;t think), but I have it in here for posterity.<br />
<strong>Scale</strong> &#8211; Applys only to vectors.  This one is very handy.  We&#8217;ll need this for scaling our speed down to the amount of time between timer calls.  We&#8217;ll get into this in a bit.<br />
<strong>Lerp</strong> &#8211; Applys to two points.  Known as Linear Interpolation.  This fun little guy will help us find a point somewhere between the two points provided. I.E. drawing a straight line between p1 &amp; p2, then go (for example) 0.1 percent of the way across that line towards p2.  Where you end up is your answer.<br />
<strong>ToString</strong> &#8211; Obvious I would think &#8211; used for debugging.</p>
<p><strong><span style="font-size:x-large;">Particle</span></strong><br />
Ok, moving on, we&#8217;ve got our Particle class.. erum &#8211; struct.</p>
<pre style="font-family:arial;font-size:12px;border:1px dashed #CCCCCC;width:90%;height:350px;overflow:auto;background:#f0f0f0;"><code style="color:#000000;word-wrap:normal;">1:  using System;
2:  using System.Collections.Generic;
3:  using System.Linq;
4:  using System.Text;
5:  using System.Windows;
6:  using System.Windows.Media;
7:  using System.Windows.Media.Media3D;
8:  namespace ParticleGeneratorControl {
9:    public struct Particle {
10:      private Dictionary _vars;
11:      public Point3D position;
12:      public Point3D velocity;
13:      public UIElement element;
14:      public TimeSpan lifespan;
15:      public TimeSpan age;
16:      public Dictionary vars {
17:        get {
18:          if (this._vars == null)
19:            this._vars = new Dictionary();
20:          return this._vars;
21:        }
22:      }
23:      public bool isAlive {
24:        get {
25:          return this.age &lt; this.lifespan;
26:        }
27:      }
28:      public PlaneProjection GetElement3dProjection() {
29:        PlaneProjection returnValue = (PlaneProjection)this.element.Projection;
30:        if (returnValue == null) {
31:          returnValue = new PlaneProjection();
32:          this.element.Projection = returnValue;
33:        }
34:        return returnValue;
35:      }
36:      public override string ToString() {
37:        StringBuilder returnValue = new StringBuilder();
38:        string format = "{0}: {1}";
39:        returnValue
40:        .AppendFormat(format, "Position", this.position).Append(", ")
41:        .AppendFormat(format, "Vector", this.vector).Append(", ")
42:        .AppendFormat(format, "Speed", this.vector.length.ToString("#0.0")).Append(", ")
43:        .AppendFormat(format, "Age", this.age.TotalSeconds.ToString("#0.0"));
44:        return returnValue.ToString();
45:      }
46:    }
47:  }
</code></pre>
<p>So once again, first we have the public properties:<br />
<strong>position</strong> &#8211; a point, the current position of our particle.<br />
<strong>velocity</strong> &#8211; a vector, the speed and direction of our particle.<br />
<strong>element</strong> &#8211; the visual representation of our particle.  Note, for now this will be an ellipse, but later we will be able to make it any UIElement we desire.<br />
<strong>lifespan</strong> &#8211; the amount of time our particle will live for.  This can be infinite, but it is much more typical for this to be a few seconds.<br />
<strong>age &#8211; </strong>the current age of our particle.  Once they get too old, we&#8217;ll remove the particle, hide the element and recycle it for later use (this is a very green particle generator &#8211; very small carbon footprint).<br />
<strong>vars</strong> &#8211; a dictionary for storing other values that might be of use to someone customizing this control.<br />
<strong>isAlive &#8211; </strong>a readonly property telling us if the age is greater than the lifespan.</p>
<p>I&#8217;m going to skip the method description for now.  But before I move on, you&#8217;ll notice there are only 2 methods in this class and one of them is the ToString() method.  The reason for this is that the entire lifecycle of this class is handled externally.  This is so that those implementing this class can override the lifecycle functions directly on the ParticleGenerator control without needing to worry about its interaction with other classes.  We&#8217;ll get into how this works later.</p>
<p><strong><span style="font-size:x-large;">Particle Generator</span></strong><br />
And now for the big kahuna: the particle generator class. Add a new Silverlight User Control.  Here&#8217;s what the control looks like:</p>
<p>XAML:</p>
<pre style="font-family:arial;font-size:12px;border:1px dashed #CCCCCC;width:90%;height:200px;overflow:auto;background:#f0f0f0;"><code style="color:#000000;word-wrap:normal;"> &lt;UserControl
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  mc:Ignorable="d"
  x:Class="ParticleGenerator.ParticleGenerator"
  d:DesignWidth="640" d:DesignHeight="480"&gt;
</code></pre>
<p>Man, that was sort of a let-down huh?  Like I mentioned, everything will be done in C#.  So lets crack open the C# code behind.  First, we&#8217;re going to implement a few properties and private fields:</p>
<p>C#:</p>
<pre style="font-family:arial;font-size:12px;border:1px dashed #CCCCCC;width:90%;height:200px;overflow:auto;background:#f0f0f0;"><code style="color:#000000;word-wrap:normal;">   public partial class ParticleGenerator : UserControl {
     private Random _random = new Random();
     private Timer _timer;
     private Particle[] _particles;
     private DateTime _lastTick;
     private int _activeParticles = 0;
     public int maxParticles { get; set; }
     public TimeSpan particleLifespan { get; set; }
     public double particleMinSpeed { get; set; }
     public double particleMaxSpeed { get; set; }
     public Point3D particleInitialPosition { get; set; }
     public Point3D particleInitialPositionRadius { get; set; }
     public Point3D particleInitialVelocity { get; set; }
     public double particleInitialVelocityScaleRange { get; set; }
     public double particleInitialVelocityYaw { get; set; }
     public double particleInitialVelocityPitch { get; set; }
     public double particleInitialVelocityRoll { get; set; }
</code></pre>
<p>I won&#8217;t describe every property and field in here, but the big ones are:<br />
<strong>_random</strong> &#8211; used to make the particle generation randomly distributed.<br />
<strong>_timer</strong> &#8211; which will fire every millisecond and allow us to move the particles.<br />
<strong>_particles</strong> &#8211; the array that will hold all of our particles.<br />
<strong>maxParticles</strong> &#8211; determines the size of the _particles array.  This property can not be changed once the control is initialized.<br />
<strong>particleInitialPosition / Radius</strong> &#8211; properties that determine the position of the particle when first generated.<br />
particleInitialVelocity.<br />
<strong>particleInitialVelocity / ScaleRange / Yaw / Pitch / Roll</strong> &#8211; properties that determine the particles speed and direction upon generation.  The speed will be units per second.</p>
<p>Our control constructor will look like this:</p>
<pre style="font-family:arial;font-size:12px;border:1px dashed #CCCCCC;width:90%;height:200px;overflow:auto;background:#f0f0f0;"><code style="color:#000000;word-wrap:normal;"> public ParticleGenerator() {
   InitializeComponent();
   this.maxParticles = 30;
   this.particleLifespan = TimeSpan.FromSeconds(2);
   this.particleInitialPosition = new Point3D() { x = 0, y = 0, z = 0 };
   this.particleInitialPositionRadius = new Point3D() { x = 10, y = 10, z = 10 };
   this.particleInitialVelocity = new Point3D() { x = 0, y = -500, z = 0 };
   this.particleInitialVelocityScaleRange = 0.2;
   this.particleInitialVelocityYaw = 0.5;
   this.particleInitialVelocityPitch = 0.5;
   this.particleInitialVelocityRoll = 0.5;
 }
</code></pre>
<p>Basically, we just want to set some reasonable defaults for our values.  We haven&#8217;t yet initialized the timer or the particles array because we need to wait for the user&#8217;s properties to be set.</p>
<pre style="font-family:arial;font-size:12px;border:1px dashed #CCCCCC;width:90%;height:200px;overflow:auto;background:#f0f0f0;"><code style="color:#000000;word-wrap:normal;"> public void init() {
   this._particles = new Particle[this.maxParticles];
   for (int i = 0; i &lt; this.maxParticles; i++) {
     this._particles[i] = this.initParticle();
   }
   this._lastTick = DateTime.Now;
   this._timer = new Timer(this.timerTick, this._particles, 1, 1);
 }
</code></pre>
<p>In our init method, we&#8217;re going to initialize the array, loop through each item in the array and initialize the particles.  Last, we&#8217;re going to set the lastTick property to now (you&#8217;ll see why we do this in a minute), and initialize the timer.</p>
<p>Now, lets take a look at that initParticle() method:</p>
<pre style="font-family:arial;font-size:12px;border:1px dashed #CCCCCC;width:90%;height:200px;overflow:auto;background:#f0f0f0;"><code style="color:#000000;word-wrap:normal;"> protected virtual Particle initParticle() {
   Particle returnValue = new Particle() {
       element = this.getParticleVisualElement()
     };
   this.LayoutRoot.Children.Add(returnValue.element);
   return returnValue;
 }
 protected virtual UIElement getParticleVisualElement() {
   return new Ellipse() {
       Fill = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0)),
       Height = 10,
       Width = 10
     };
 }
</code></pre>
<p>All this method is doing is creating a new particle structure, adding the visual element to the control&#8217;s Canvas (LayoutRoot), then making sure the particle is hidden.</p>
<p>So, back to the top&#8230; remember that timer we initialized?  Lets take a look at what that&#8217;s doing&#8230;</p>
<pre style="font-family:arial;font-size:12px;border:1px dashed #CCCCCC;width:90%;height:auto;overflow:auto;background:#f0f0f0;"><code style="color:#000000;word-wrap:normal;"> this._timer = new Timer(this.timerTick, this._particles, 1, 20)
</code></pre>
<p>This timer is set up to call a method called timerTick(object particleArray) every 20 milliseconds and pass in the array of particles.  Now, we can easily update thousands of particles in that time, but this may cause an issue if we&#8217;re debugging, as timer ticks are answered by a pool of threads and Visual Studio is no good at handling multi-threaded debugging.</p>
<p>Lets take a look at the timerTick() method:</p>
<pre style="font-family:arial;font-size:12px;border:1px dashed #CCCCCC;width:90%;height:auto;overflow:auto;background:#f0f0f0;"><code style="color:#000000;word-wrap:normal;"> private void timerTick(object particleArray) {
   lock (this) {
     Particle[] particles = (Particle[])particleArray;
     TimeSpan deltaTime = this.getTimeSinceLastTick();
     //update all particles
     for (int i = 0; i &lt; particles.Length; i++) {
       if (particles[i].isAlive) {
         particles[i] = this.updateParticle(i, particles[i], deltaTime);
       }
     }
     //draw particles
     this.Dispatcher.BeginInvoke(new UpdateUIDelegate(this.updateUI), particles, deltaTime);
   }
 }
</code></pre>
<p>What are we doing here?  The first lock statement prevents race conditions and just general confusion.  The second statement casts the object into an array of particles so we can use the typed instance of it.</p>
<p>Next we allocate a deltaTime variable.  This variable will hold the amount of time it took since the last tick occurred.  Lets see how this is done:</p>
<pre style="font-family:arial;font-size:12px;border:1px dashed #CCCCCC;width:90%;height:auto;overflow:auto;background:#f0f0f0;"><code style="color:#000000;word-wrap:normal;"> private TimeSpan getTimeSinceLastTick() {
   TimeSpan returnValue;
   DateTime now = DateTime.Now;
   returnValue = now - this._lastTick;
   this._lastTick = now;
   return returnValue;
 }
</code></pre>
<p>Straight forward right? Now minus the time of the last tick equals the time between.  Save now, return time between (a.k.a deltaTime) and we&#8217;re done.</p>
<p>Back to our timerTick() method.  Next, we just loop through the array of particles.  For each particle that is alive (age &lt; lifespan) we update the particle.</p>
<p>Ok, so what&#8217;s that last BeginInvoke() statement there?  That is simply the statement that will submit a job to the UI thread to update the visual elements of the particles.  Remember, if we&#8217;re not in the UI thread, we can&#8217;t touch UIElements &#8211; you&#8217;ll get an InvalidCrossThreadAccess exception.</p>
<p>So let&#8217;s dive into the updateUI method.  (By the way, notice it&#8217;s using an UpdateUIDelegate?  Normally you can pass function references just by leaving off the parens, but for some reason this wouldn&#8217;t work &#8211; I had to create a delegate then pass that in&#8230; again, not sure why)</p>
<pre style="font-family:arial;font-size:12px;border:1px dashed #CCCCCC;width:90%;height:auto;overflow:auto;background:#f0f0f0;"><code style="color:#000000;word-wrap:normal;"> private delegate void UpdateUIDelegate(Particle[] particles, TimeSpan deltaTime);
 private void updateUI(Particle[] particles, TimeSpan deltaTime) {
   int activeParticles = 0;
   //update the particles
   for (int i = 0; i &lt; particles.Length; i++) {
     if (particles[i].isAlive) {
       activeParticles++;
     }
     particles[i] = this.updateElement(i, particles[i], deltaTime);
   }
   this._activeParticles = activeParticles;
 }
</code></pre>
<p>Well, the updateUI() method really just loops through the active elements, counting them then calling updateElement() on all the elements.  This is important as we need to update inactive elements as well.  Fairly straight forward really&#8230; NEXT!</p>
<pre style="font-family:arial;font-size:12px;border:1px dashed #CCCCCC;width:90%;height:auto;overflow:auto;background:#f0f0f0;"><code style="color:#000000;word-wrap:normal;"> public Particle updateElement(int index, Particle particle, TimeSpan deltaTime) {
   Particle returnValue = particle;
   if (returnValue.age &gt;= returnValue.lifespan) {
     //the particle is old, kill off
     returnValue.element.Visibility = Visibility.Collapsed;
   } else {
     returnValue.element.Visibility = Visibility.Visible;
     //update the position
     PlaneProjection projection = returnValue.GetElement3dProjection();
     projection.GlobalOffsetX = returnValue.position.x;
     projection.GlobalOffsetY = returnValue.position.y;
     projection.GlobalOffsetZ = returnValue.position.z;
   }
   return returnValue;
 }
</code></pre>
<div style="margin:0;">This method is also pretty simple (I do try to keep my methods very simple in case you haven&#8217;t noticed)&#8230; First we check the age of the particle, if it&#8217;s too old we set the element&#8217;s visibility = Collapsed and exit out.  If not, we set the visibility to Visible.  Next we get the element&#8217;s 3D Projection transformation.  This is the transformation that will allow us to pretend we&#8217;re working in 3D space.  The method is pretty simple (you saw it in the Particle code above.  We just take our current position x, y, and z coordinates and assign them to the corresponding GlobalOffset property.  Just for posterity, I&#8217;ll quickly review the GetElement3dProjection() method:</div>
<pre style="font-family:arial;font-size:12px;border:1px dashed #CCCCCC;width:90%;height:auto;overflow:auto;background:#f0f0f0;"><code style="color:#000000;word-wrap:normal;"> public PlaneProjection GetElement3dProjection() {
   PlaneProjection returnValue = (PlaneProjection)this.element.Projection;
   if (returnValue == null) {
     returnValue = new PlaneProjection();
     this.element.Projection = returnValue;
   }
   return returnValue;
 }
</code></pre>
<div style="margin:0;">Get the PlaneProjection transform, if it&#8217;s null assign a new one then return it.</div>
<p>We&#8217;re missing something&#8230; what is it?? Ahh, yes, particle generation!  When we&#8217;ve first initialized all the particles, they&#8217;re hidden.  We want to spit out particles a few at a time, so we&#8217;re going to have to make a few updates to some of the methods to accomplish this.  First, we need something to tell us when it&#8217;s time:</p>
<p>Private fields:</p>
<pre style="font-family:arial;font-size:12px;border:1px dashed #CCCCCC;width:90%;height:auto;overflow:auto;background:#f0f0f0;"><code style="color:#000000;word-wrap:normal;"> #region Particle generation rate
 private int _rateNumberOfParticlesGenerated;
 private Queue _particlesToGenerate = new Queue();
 private TimeSpan _rateTimeSinceLastGenerated;
 private TimeSpan _rateTimeBetweenGeneratedParticles;
 private double _particleRate;
 #endregion
</code></pre>
<p>particleRate property:</p>
<pre style="font-family:arial;font-size:12px;border:1px dashed #CCCCCC;width:90%;height:auto;overflow:auto;background:#f0f0f0;"><code style="color:#000000;word-wrap:normal;"> public double particleRate {
   get {
     return this._particleRate;
   }
   set {
     this._particleRate = value;
     this._rateTimeBetweenGeneratedParticles = (value &gt; 0) ? TimeSpan.FromSeconds(1 / value) : TimeSpan.Zero;
   }
 }
</code></pre>
<p>Because the particleRate is a bit of a complex property, we can&#8217;t just use a normal <em>prop { get; set; }</em> to define it.  Anytime it changes, we need to modify all the underlying values.  The real driving factor for how frequently we generate new particles is the _rateTimeBetweenGeneratedParticles field.  This field tells us that we need to generate a new particle every X seconds.  So if the rate is not zero, we&#8217;ll set that field to <em>1/value</em> seconds.  I.E. if the particleRate is set to 3 particles per second, we want to genarate a new particle every 1/3rd second.</p>
<pre style="font-family:arial;font-size:12px;border:1px dashed #CCCCCC;width:90%;height:auto;overflow:auto;background:#f0f0f0;"><code style="color:#000000;word-wrap:normal;"> private int newParticlesToGenerate(TimeSpan deltaTime) {
   int returnValue = 0;
   this._rateTimeSinceLastGenerated += deltaTime;
   while (this._rateTimeSinceLastGenerated &gt; this._rateTimeBetweenGeneratedParticles) {
     this._rateTimeSinceLastGenerated -= this._rateTimeBetweenGeneratedParticles;
     returnValue++;
   }
   return returnValue;
 }
</code></pre>
<p>The newParticlesToGenerate() method returns the number of new particles to generate.  It figures this out by calculating the amount of time since the last particle was generated, and while that time is greater than the timeBetweenGeneratedParticles (calculated above in the particleRate setter) we add a new particle to generate and subtract the timeBetween from the timeSince.</p>
<p>Now that we&#8217;ve gotten that all squared away, we just need to call that from the timerTick() method:</p>
<pre style="font-family:arial;font-size:12px;border:1px dashed #CCCCCC;width:90%;height:350px;overflow:auto;background:#f0f0f0;"><code style="color:#000000;word-wrap:normal;"> private void timerTick(object particleArray) {
   lock (this) {
     Particle[] particles = (Particle[])particleArray;
     TimeSpan deltaTime = this.getTimeSinceLastTick();
     //generate new particles
     int newParticles = this.newParticlesToGenerate(deltaTime);
     for (int i = 0; i &lt; newParticles; i++) {
       if (this._particlesToGenerate.Count &gt; 0) {
         int index = this._particlesToGenerate.Dequeue();
         particles[index] = this.generateParticle(particles[index]);
       }
     }
     //update all particles
     for (int i = 0; i &lt; particles.Length; i++) {
       if (particles[i].isAlive) {
         particles[i] = this.updateParticle(i, particles[i], deltaTime);
         //If no longer alive, add to list of reusable particles
         if (!particles[i].isAlive) {
           this._particlesToGenerate.Enqueue(i);
         }
       }
     }
     //draw particles
     this.Dispatcher.BeginInvoke(new UpdateUIDelegate(this.updateUI), particles, deltaTime);
   }
 }
</code></pre>
<p>Notice the new code here.  Before we dive down the particleGeneration rat hole, lets look at the second change, with the comment &#8220;If no longer alive&#8230;&#8221;.  if the particle.isAlive property returns false after the updateParticle() call, that method must have caused the particle to age over it&#8217;s lifespan, so this should be the first time we&#8217;ve seen this particle croak, so we&#8217;re going to enqueue it in the _particlesToGenerate queue.</p>
<p>Ok, so back to the first change with the comment &#8220;Generate new particles&#8221;.  First, we get the number of new particles to generate, then just loop the number of times provided by the return value, grab the next available particle from the _particlesToGenerate queue (if there is one) and call generateParticle():</p>
<pre style="font-family:arial;font-size:12px;border:1px dashed #CCCCCC;width:90%;height:auto;overflow:auto;background:#f0f0f0;"><code style="color:#000000;word-wrap:normal;"> protected virtual Particle generateParticle(Particle particle) {
   Particle returnValue = this.resetParticle(particle);
   this._rateNumberOfParticlesGenerated++;
   return returnValue;
 }
private Particle resetParticle(Particle particle) {
    particle.position = this.getNewParticlePosition();
    particle.velocity = this.getNewParticleVelocity();
    particle.lifespan = this.particleLifespan;
    particle.age = TimeSpan.Zero;
    return particle;
}
private Point3D getNewParticlePosition() {
    Point3D returnValue = new Point3D();
    returnValue.x = this.particleInitialPosition.x + (this.plusOrMinus() * this.particleInitialPositionRadius.x * this._random.NextDouble());
    returnValue.y = this.particleInitialPosition.y + (this.plusOrMinus() * this.particleInitialPositionRadius.y * this._random.NextDouble());
    returnValue.z = this.particleInitialPosition.z + (this.plusOrMinus() * this.particleInitialPositionRadius.z * this._random.NextDouble());
    return returnValue;
}
</code></pre>
<div style="margin:0;"><span style="font-size:small;">Please note, if any of you purists want to call me out on the fact that the initialPositionRadius calculation here is not a true radius, don&#8217;t, its just a simplification on my part (both in naming and calculation).</span></div>
<pre style="font-family:arial;font-size:12px;border:1px dashed #CCCCCC;width:90%;height:auto;overflow:auto;background:#f0f0f0;"><code style="color:#000000;word-wrap:normal;"> private Point3D getNewParticleVelocity() {
   Point3D returnValue = this.particleInitialVelocity;
   returnValue.scale(1 + this.plusOrMinus() * this.particleInitialVelocityScaleRange * this._random.NextDouble());
   double yaw = this.particleInitialVelocityYaw * this._random.NextDouble();
   double pitch = this.particleInitialVelocityPitch * this._random.NextDouble();
   double roll = this.particleInitialVelocityRoll * this._random.NextDouble();
   returnValue = MatrixGeometry.transform(returnValue, yaw, pitch, roll, 0, 0, 0);
   return returnValue;
 }
 private int plusOrMinus() {
   if (this._random.NextDouble() &gt; 0.5) {
     return 1;
   } else {
     return -1;
   }
 }
</code></pre>
<div style="margin:0;">Almost forgot about the last supporting class: <em>MatrixGeometry</em></div>
<pre style="font-family:arial;font-size:12px;border:1px dashed #CCCCCC;width:90%;height:350px;overflow:auto;background:#f0f0f0;"><code style="color:#000000;word-wrap:normal;"> using System;
 using System.Windows.Media.Media3D;
 namespace ParticleGenerator {
   public class MatrixGeometry {
     public static Matrix3D YawPitchRollMatrix(double yaw, double pitch, double roll) {
       return MatrixGeometry.TransformMatrix(yaw, pitch, roll, 0, 0, 0);
     }
     public static Matrix3D TranslationMatrix(double x, double y, double z) {
       return new Matrix3D() { OffsetX = x, OffsetY = y, OffsetZ = z };
     }
     public static Matrix3D TransformMatrix(double yaw, double pitch, double roll, double x, double y, double z) {
       Matrix3D returnValue;
       //Math translations: yaw = a, pitch = b, roll = y
       //Common calculations
       double cosA = Math.Cos(yaw);
       double cosB = Math.Cos(pitch);
       double cosY = Math.Cos(roll);
       double sinA = Math.Sin(yaw);
       double sinB = Math.Sin(pitch);
       double sinY = Math.Sin(roll);
       //matrix elements
       double m11 = cosA * cosB;
       double m12 = sinA * cosB;
       double m13 = -sinB;
       double m14 = 0;
       double m21 = cosA * sinB * sinY - sinA * cosY;
       double m22 = sinA * sinB * sinY + cosA * cosY;
       double m23 = cosB * sinY;
       double m24 = 0;
       double m31 = cosA * sinB * cosY + sinA * sinY;
       double m32 = sinA * sinB * cosY - cosA * sinY;
       double m33 = cosB * cosY;
       double m34 = 0;
       double m41 = x;
       double m42 = y;
       double m43 = z;
       double m44 = 1;
       returnValue = new Matrix3D(m11, m12, m13, m14, m21, m22, m23, m24, m31, m32, m33, m34, m41, m42, m43, m44);
       return returnValue;
     }
     public static Point3D transform(Point3D point, Matrix3D matrix) {
       Point3D returnValue = point;
       returnValue.x = point.x * matrix.M11 + point.y * matrix.M21 + point.z * matrix.M31 + matrix.OffsetX;
       returnValue.y = point.x * matrix.M12 + point.y * matrix.M22 + point.z * matrix.M32 + matrix.OffsetY;
       returnValue.z = point.x * matrix.M13 + point.y * matrix.M23 + point.z * matrix.M33 + matrix.OffsetZ;
       return returnValue;
     }
     public static Point3D transform(Point3D point, double yaw, double pitch, double roll, double x, double y, double z) {
       return MatrixGeometry.transform(point, MatrixGeometry.TransformMatrix(yaw, pitch, roll, x, y, z));
     }
   }
 }
</code></pre>
<div style="margin:0;">And this one I&#8217;ll leave completely unexplained for now.  If you&#8217;d like to read up on the math behind this class, there&#8217;s a ton of information out there, but this is the best I&#8217;ve found so far:</div>
<div style="margin:0;"><a href="http://planning.cs.uiuc.edu/node96.html">http://planning.cs.uiuc.edu/node96.html</a> Thank you Steven LaValle.</div>
<div style="margin:0;">Suffice to say, yaw, pitch and roll are terms used to describe angle transformations on vectors.  Typically described in radians, they measure the rotation to apply to a vector in each given plane.  <a href="http://en.wikipedia.org/wiki/Flight_dynamics">http://en.wikipedia.org/wiki/Flight_dynamics</a> read up for more info.</div>
<div style="margin:0;">And finally, we need to enqueue all of the particles into the _particlesToGenerate queue when we first initialize them:</div>
<pre style="font-family:arial;font-size:12px;border:1px dashed #CCCCCC;width:90%;height:auto;overflow:auto;background:#f0f0f0;"><code style="color:#000000;word-wrap:normal;"> public void init() {
   this._particles = new Particle[this.maxParticles];
   for (int i = 0; i &lt; this.maxParticles; i++) {
   this._particles[i] = this.initParticle();
   this._particlesToGenerate.Enqueue(i);
 }
 ...
</code></pre>
<p><a name="more"></a><strong><span style="font-size:x-large;">Debugging</span></strong></p>
<div style="margin:0;">The final step in all of this is to get it to run.  You&#8217;ll notice that you can&#8217;t just click the run button because we can&#8217;t execute a class library.  So lets go ahead and get something we can debug.</div>
<div style="margin:0;">Right click your solution in the Solution Explorer and click Add &gt; New Project&#8230;</div>
<div style="margin:0;">Select the Silverlight Application and name the new project.  I chose &#8220;ParticleGeneratorTest&#8221;.  I know, I&#8217;ve got a way with naming things&#8230;</div>
<div style="margin:0;">Clicking OK will prompt Visual Studio to ask you if you want to host your new application in a web project.  Click OK.</div>
<div class="separator" style="clear:both;text-align:center;"><a style="margin-left:1em;margin-right:1em;" href="http://markcandelora.files.wordpress.com/2010/08/newprojectwebsitehost11.gif"><img src="http://markcandelora.files.wordpress.com/2010/08/newprojectwebsitehost11.gif?w=300" border="0" alt="" /></a></div>
<div style="margin:0;">Right click the References folder under your new Silverlight application and click Add Reference&#8230;</div>
<div style="margin:0;">Under the Projects tab chose ParticleGenerator.</div>
<div class="separator" style="clear:both;text-align:center;"><a style="margin-left:1em;margin-right:1em;" href="http://markcandelora.files.wordpress.com/2010/08/addreference1.gif"><img src="http://markcandelora.files.wordpress.com/2010/08/addreference1.gif?w=300" border="0" alt="" /></a></div>
<div style="margin:0;">Your MainPage.xaml file should already be open, if not, open it now.  Expand your Toolbox toolbar and you should see your new ParticleGenerator control.  Click and drag it onto the canvas.  Your new XAML should look like this:</div>
<pre style="font-family:arial;font-size:12px;border:1px dashed #CCCCCC;width:90%;height:auto;overflow:auto;background:#f0f0f0;"><code style="color:#000000;word-wrap:normal;"> &lt;UserControl x:Class="ParticleGeneratorTest.MainPage"
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 mc:Ignorable="d"
 d:DesignHeight="300" d:DesignWidth="400"
 xmlns:my="clr-namespace:ParticleGenerator;assembly=ParticleGenerator"&gt;
</code></pre>
<div style="margin:0;">Change some of the properties here so that your control stretches across the entire grid:</div>
<div style="margin:0;">Margin=&#8221;0,0,0,0&#8243; HorizontalAlignment=&#8221;Stretch&#8221; VerticalAlignment=&#8221;Stretch&#8221;</div>
<div style="margin:0;">Now, unfortunately, those properties we set up aren&#8217;t really the easiest to manipulate in the editor.  We can fix this later, but for now we&#8217;ll need to set them manually through the XAML:</div>
<pre style="font-family:arial;font-size:12px;border:1px dashed #CCCCCC;width:90%;height:auto;overflow:auto;background:#f0f0f0;"><code style="color:#000000;word-wrap:normal;"> &lt;my:ParticleGenerator particleRate="50" maxParticles="100"
 Name="particleGenerator1" Margin="0,0,0,0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"&gt;
</code></pre>
<div style="margin:0;">Last thing.  We need to call that pesky init() function to get things started, so open up the MainPage.xaml.cs and plug it in:</div>
<pre style="font-family:arial;font-size:12px;border:1px dashed #CCCCCC;width:90%;height:auto;overflow:auto;background:#f0f0f0;background-image:url('http://2.bp.blogspot.com/_z5ltvMQPaa8/SjJXr_U2YBI/AAAAAAAAAAM/46OqEP32CJ8/s320/codebg.gif');color:#000000;text-align:left;line-height:20px;padding:0;"><code style="color:#000000;word-wrap:normal;"> public MainPage() {
   InitializeComponent();
   this.particleGenerator1.init();
 }
</code></pre>
<p>Now lets fire up that bad Larry and see what we&#8217;ve got!</p>
<div class="separator" style="clear:both;text-align:center;"><a style="margin-left:1em;margin-right:1em;" href="http://markcandelora.files.wordpress.com/2010/08/finalproduct1.gif"><img src="http://markcandelora.files.wordpress.com/2010/08/finalproduct1.gif?w=300" border="0" alt="" /></a></div>
<p>Damn, that was a lot.  If you&#8217;re still with me, congratulations, you&#8217;ve now got a very cool particle generator.  This project will form the basis for Part 2 (which will hopefully be much much shorter).</p>
<p><a href="https://docs.google.com/leaf?id=0BxcGUKoQiXmJOTVjMDRiMTItZTYyOS00NWYwLTg0OGYtMDhjMWVlZGU5NGY1&amp;hl=en">Download the full project here</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/markcandelora.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/markcandelora.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/markcandelora.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/markcandelora.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/markcandelora.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/markcandelora.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/markcandelora.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/markcandelora.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/markcandelora.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/markcandelora.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/markcandelora.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/markcandelora.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/markcandelora.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/markcandelora.wordpress.com/16/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=markcandelora.wordpress.com&amp;blog=15191860&amp;post=16&amp;subd=markcandelora&amp;ref=&amp;feed=1" width="1" height="1" /><div class="sharedaddy sd-like-enabled"></div>]]></content:encoded>
			<wfw:commentRss>http://markcandelora.wordpress.com/2010/08/11/simple-particle-generator-using-silverlight-4-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ae3dd762634974aaff170ea7b8090a45?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mcandelo</media:title>
		</media:content>

		<media:content url="http://markcandelora.files.wordpress.com/2010/08/visualstudiotemplate1.gif?w=300" medium="image" />

		<media:content url="http://markcandelora.files.wordpress.com/2010/08/newprojectwebsitehost11.gif?w=300" medium="image" />

		<media:content url="http://markcandelora.files.wordpress.com/2010/08/addreference1.gif?w=300" medium="image" />

		<media:content url="http://markcandelora.files.wordpress.com/2010/08/finalproduct1.gif?w=300" medium="image" />
	</item>
	</channel>
</rss>
