<?xml version="1.0" encoding="utf-8"?>
<feed xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xml:lang="en-us" xmlns="http://www.w3.org/2005/Atom">
  <title>Schwammy Says...</title>
  <link rel="alternate" type="text/html" href="http://www.blog.ingenuitynow.net/" />
  <link rel="self" href="http://www.blog.ingenuitynow.net/SyndicationService.asmx/GetAtom" />
  <icon>favicon.ico</icon>
  <updated>2012-01-04T22:23:16.4336304-05:00</updated>
  <author>
    <name>Andrew Schwam</name>
  </author>
  <subtitle>Whatever comes to mind.</subtitle>
  <id>http://www.blog.ingenuitynow.net/</id>
  <generator uri="http://www.dasblog.net" version="1.9.6264.0">DasBlog</generator>
  <entry>
    <title>Using Linq to Compare Lists</title>
    <link rel="alternate" type="text/html" href="http://www.blog.ingenuitynow.net/Using+Linq+To+Compare+Lists.aspx" />
    <id>http://www.blog.ingenuitynow.net/PermaLink,guid,e640a50f-9f93-4b0a-a150-c7f586d1833b.aspx</id>
    <published>2012-01-04T22:23:16.4336304-05:00</published>
    <updated>2012-01-04T22:23:16.4336304-05:00</updated>
    <category term="C#" label="C#" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,C%23.aspx" />
    <category term="LINQ" label="LINQ" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,LINQ.aspx" />
    <category term="NUnit" label="NUnit" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,NUnit.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
Linq is awesome and always makes tasks easier!  Here is a great tip that you
might not know about. 
</p>
        <p>
Today I needed to compare two generic lists… SequenceEqual() to the rescue. 
To show how it works, here is some sample code:
</p>
        <pre class="code">[<span style="color: #2b91af">Test</span>] <span style="color: blue">public
void </span>SuccessfullyMatchesTwoListsOfInts() { <span style="color: #2b91af">List</span>&lt;<span style="color: blue">int</span>&gt;
ints1 = <span style="color: blue">new </span><span style="color: #2b91af">List</span>&lt;<span style="color: blue">int</span>&gt;();
ints1.Add(1); ints1.Add(2); ints1.Add(3); <span style="color: #2b91af">List</span>&lt;<span style="color: blue">int</span>&gt;
ints2 = <span style="color: blue">new </span><span style="color: #2b91af">List</span>&lt;<span style="color: blue">int</span>&gt;();
ints2.Add(1); ints2.Add(2); ints2.Add(3); <span style="color: #2b91af">Assert</span>.That(ints1.SequenceEqual(ints2), <span style="color: #2b91af">Is</span>.True);
}</pre>
        <p>
SequenceEqual loops through the 2 lists and compares each item to make sure they match. 
Just for kicks I put it through the paces.  Here are some other unit tests that
I wrote.  This helps show what you can do:
</p>
        <pre class="code">
          <span style="color: blue">using </span>System.Collections.Generic; <span style="color: blue">using </span>System.Linq; <span style="color: blue">using </span>NUnit.Framework; <span style="color: blue">namespace </span>SequenceEqual
{ [<span style="color: #2b91af">TestFixture</span>] <span style="color: blue">public
class </span><span style="color: #2b91af">SequenceEqualTests </span>{ [<span style="color: #2b91af">Test</span>] <span style="color: blue">public
void </span>SuccessfullyMatchesTwoListsOfInts() { <span style="color: #2b91af">List</span>&lt;<span style="color: blue">int</span>&gt;
ints1 = <span style="color: blue">new </span><span style="color: #2b91af">List</span>&lt;<span style="color: blue">int</span>&gt;();
ints1.Add(1); ints1.Add(2); ints1.Add(3); <span style="color: #2b91af">List</span>&lt;<span style="color: blue">int</span>&gt;
ints2 = <span style="color: blue">new </span><span style="color: #2b91af">List</span>&lt;<span style="color: blue">int</span>&gt;();
ints2.Add(1); ints2.Add(2); ints2.Add(3); <span style="color: #2b91af">Assert</span>.That(ints1.SequenceEqual(ints2), <span style="color: #2b91af">Is</span>.True);
} [<span style="color: #2b91af">Test</span>] <span style="color: blue">public void </span>SuccessfullyFailsTwoListsOfInts()
{ <span style="color: green">// these two don't match! </span><span style="color: #2b91af">List</span>&lt;<span style="color: blue">int</span>&gt;
ints1 = <span style="color: blue">new </span><span style="color: #2b91af">List</span>&lt;<span style="color: blue">int</span>&gt;();
ints1.Add(1); ints1.Add(2); ints1.Add(3); <span style="color: #2b91af">List</span>&lt;<span style="color: blue">int</span>&gt;
ints2 = <span style="color: blue">new </span><span style="color: #2b91af">List</span>&lt;<span style="color: blue">int</span>&gt;();
ints2.Add(1); ints2.Add(4); ints2.Add(3); <span style="color: #2b91af">Assert</span>.That(ints1.SequenceEqual(ints2), <span style="color: #2b91af">Is</span>.False);
} [<span style="color: #2b91af">Test</span>] <span style="color: blue">public void </span>SuccessfullyFailsTwoListsOfIntsOutOfOrder()
{ <span style="color: green">// these two don't match. They have the same contents
but the order is changed. </span><span style="color: #2b91af">List</span>&lt;<span style="color: blue">int</span>&gt;
ints1 = <span style="color: blue">new </span><span style="color: #2b91af">List</span>&lt;<span style="color: blue">int</span>&gt;();
ints1.Add(3); ints1.Add(2); ints1.Add(1); <span style="color: #2b91af">List</span>&lt;<span style="color: blue">int</span>&gt;
ints2 = <span style="color: blue">new </span><span style="color: #2b91af">List</span>&lt;<span style="color: blue">int</span>&gt;();
ints2.Add(1); ints2.Add(2); ints2.Add(3); <span style="color: #2b91af">Assert</span>.That(ints1.SequenceEqual(ints2), <span style="color: #2b91af">Is</span>.False);
} [<span style="color: #2b91af">Test</span>] <span style="color: blue">public void </span>SuccessfullyFailsTwoListsOfStrings()
{ <span style="color: green">// these two lists of strings don't match </span><span style="color: #2b91af">List</span>&lt;<span style="color: blue">string</span>&gt;
strings1 = <span style="color: blue">new </span><span style="color: #2b91af">List</span>&lt;<span style="color: blue">string</span>&gt;();
strings1.Add(<span style="color: #a31515">"one"</span>); strings1.Add(<span style="color: #a31515">"two"</span>);
strings1.Add(<span style="color: #a31515">"three"</span>); <span style="color: #2b91af">List</span>&lt;<span style="color: blue">string</span>&gt;
strings2 = <span style="color: blue">new </span><span style="color: #2b91af">List</span>&lt;<span style="color: blue">string</span>&gt;();
strings2.Add(<span style="color: #a31515">"one"</span>); strings2.Add(<span style="color: #a31515">"lasjdflkajsdf"</span>);
strings2.Add(<span style="color: #a31515">"three"</span>); <span style="color: #2b91af">Assert</span>.That(strings1.SequenceEqual(strings2), <span style="color: #2b91af">Is</span>.False);
} [<span style="color: #2b91af">Test</span>] <span style="color: blue">public void </span>SuccessfullyMatchesTwoListsOfStrings()
{ <span style="color: green">// two matching lists of strings. </span><span style="color: #2b91af">List</span>&lt;<span style="color: blue">string</span>&gt;
strings1 = <span style="color: blue">new </span><span style="color: #2b91af">List</span>&lt;<span style="color: blue">string</span>&gt;();
strings1.Add(<span style="color: #a31515">"one"</span>); strings1.Add(<span style="color: #a31515">"two"</span>);
strings1.Add(<span style="color: #a31515">"three"</span>); <span style="color: #2b91af">List</span>&lt;<span style="color: blue">string</span>&gt;
strings2 = <span style="color: blue">new </span><span style="color: #2b91af">List</span>&lt;<span style="color: blue">string</span>&gt;();
strings2.Add(<span style="color: #a31515">"one"</span>); strings2.Add(<span style="color: #a31515">"two"</span>);
strings2.Add(<span style="color: #a31515">"three"</span>); <span style="color: #2b91af">Assert</span>.That(strings1.SequenceEqual(strings2), <span style="color: #2b91af">Is</span>.True);
} [<span style="color: #2b91af">Test</span>] <span style="color: blue">public void </span>SuccessfullyMatchesTwoListsOfObjects()
{ <span style="color: green">// works for objects too. </span><span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">TestObject</span>&gt;
objects1 = <span style="color: blue">new </span><span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">TestObject</span>&gt;();
objects1.Add(<span style="color: blue">new </span><span style="color: #2b91af">TestObject </span>{
Property1 = <span style="color: #a31515">"1" </span>}); objects1.Add(<span style="color: blue">new </span><span style="color: #2b91af">TestObject </span>{
Property1 = <span style="color: #a31515">"2" </span>}); objects1.Add(<span style="color: blue">new </span><span style="color: #2b91af">TestObject </span>{
Property1 = <span style="color: #a31515">"3" </span>}); <span style="color: #2b91af">Assert</span>.That(objects1.SequenceEqual(objects1), <span style="color: #2b91af">Is</span>.True);
} [<span style="color: #2b91af">Test</span>] <span style="color: blue">public void </span>SuccessfullyFailsTwoListsOfObjects()
{ <span style="color: green">// these two lists of objects may "look" the
same, but they aren't // the same object (reference equals!) </span><span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">TestObject</span>&gt;
objects1 = <span style="color: blue">new </span><span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">TestObject</span>&gt;();
objects1.Add(<span style="color: blue">new </span><span style="color: #2b91af">TestObject </span>{
Property1 = <span style="color: #a31515">"1" </span>}); objects1.Add(<span style="color: blue">new </span><span style="color: #2b91af">TestObject </span>{
Property1 = <span style="color: #a31515">"2" </span>}); objects1.Add(<span style="color: blue">new </span><span style="color: #2b91af">TestObject </span>{
Property1 = <span style="color: #a31515">"3" </span>}); <span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">TestObject</span>&gt;
objects2 = <span style="color: blue">new </span><span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">TestObject</span>&gt;();
objects2.Add(<span style="color: blue">new </span><span style="color: #2b91af">TestObject </span>{
Property1 = <span style="color: #a31515">"1" </span>}); objects2.Add(<span style="color: blue">new </span><span style="color: #2b91af">TestObject </span>{
Property1 = <span style="color: #a31515">"2" </span>}); objects2.Add(<span style="color: blue">new </span><span style="color: #2b91af">TestObject </span>{
Property1 = <span style="color: #a31515">"3" </span>}); <span style="color: #2b91af">Assert</span>.That(objects1.SequenceEqual(objects2), <span style="color: #2b91af">Is</span>.False);
} [<span style="color: #2b91af">Test</span>] <span style="color: blue">public void </span>SuccessfullyMatchesTwoListsOfObjectsWithCustomComparer()
{ <span style="color: green">// here I use a custom comparer to see if the // two
lists of objects have the same content. </span><span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">TestObject</span>&gt;
objects1 = <span style="color: blue">new </span><span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">TestObject</span>&gt;();
objects1.Add(<span style="color: blue">new </span><span style="color: #2b91af">TestObject </span>{
Property1 = <span style="color: #a31515">"1" </span>}); objects1.Add(<span style="color: blue">new </span><span style="color: #2b91af">TestObject </span>{
Property1 = <span style="color: #a31515">"2" </span>}); objects1.Add(<span style="color: blue">new </span><span style="color: #2b91af">TestObject </span>{
Property1 = <span style="color: #a31515">"3" </span>}); <span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">TestObject</span>&gt;
objects2 = <span style="color: blue">new </span><span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">TestObject</span>&gt;();
objects2.Add(<span style="color: blue">new </span><span style="color: #2b91af">TestObject </span>{
Property1 = <span style="color: #a31515">"1" </span>}); objects2.Add(<span style="color: blue">new </span><span style="color: #2b91af">TestObject </span>{
Property1 = <span style="color: #a31515">"2" </span>}); objects2.Add(<span style="color: blue">new </span><span style="color: #2b91af">TestObject </span>{
Property1 = <span style="color: #a31515">"3" </span>}); <span style="color: #2b91af">Assert</span>.That(objects1.SequenceEqual(objects2, <span style="color: blue">new </span><span style="color: #2b91af">TestObjectComparer</span>()), <span style="color: #2b91af">Is</span>.True);
} [<span style="color: #2b91af">Test</span>] <span style="color: blue">public void </span>SuccessfullyFailsTwoListsOfObjectsWithCustomComparer()
{ <span style="color: green">// making sure the custome comparer fails when they don't
match </span><span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">TestObject</span>&gt;
objects1 = <span style="color: blue">new </span><span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">TestObject</span>&gt;();
objects1.Add(<span style="color: blue">new </span><span style="color: #2b91af">TestObject </span>{
Property1 = <span style="color: #a31515">"1" </span>}); objects1.Add(<span style="color: blue">new </span><span style="color: #2b91af">TestObject </span>{
Property1 = <span style="color: #a31515">"2" </span>}); objects1.Add(<span style="color: blue">new </span><span style="color: #2b91af">TestObject </span>{
Property1 = <span style="color: #a31515">"3" </span>}); <span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">TestObject</span>&gt;
objects2 = <span style="color: blue">new </span><span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">TestObject</span>&gt;();
objects2.Add(<span style="color: blue">new </span><span style="color: #2b91af">TestObject </span>{
Property1 = <span style="color: #a31515">"1" </span>}); objects2.Add(<span style="color: blue">new </span><span style="color: #2b91af">TestObject </span>{
Property1 = <span style="color: #a31515">"asdfsadf" </span>}); objects2.Add(<span style="color: blue">new </span><span style="color: #2b91af">TestObject </span>{
Property1 = <span style="color: #a31515">"3" </span>}); <span style="color: #2b91af">Assert</span>.That(objects1.SequenceEqual(objects2, <span style="color: blue">new </span><span style="color: #2b91af">TestObjectComparer</span>()), <span style="color: #2b91af">Is</span>.False);
} } <span style="color: blue">public class </span><span style="color: #2b91af">TestObject </span>{ <span style="color: blue">public
string </span>Property1 { <span style="color: blue">get</span>; <span style="color: blue">set</span>;
} } <span style="color: blue">public class </span><span style="color: #2b91af">TestObjectComparer </span>:
IEqualityComparer&lt;<span style="color: #2b91af">TestObject</span>&gt; { <span style="color: blue">public
bool </span>Equals(<span style="color: #2b91af">TestObject </span>x, <span style="color: #2b91af">TestObject </span>y)
{ <span style="color: blue">return </span>(x.Property1 == y.Property1); } <span style="color: blue">public
int </span>GetHashCode(<span style="color: #2b91af">TestObject </span>obj) { <span style="color: blue">return </span>obj.GetHashCode();
} } }</pre>
        <p>
Today I needed to compare to generic lists. I guessed that Linq would provide a way
to do this and sure enough it did: SequenceEqual() to the rescue. To show how it work
</p>
        <img width="0" height="0" src="http://www.blog.ingenuitynow.net/aggbug.ashx?id=e640a50f-9f93-4b0a-a150-c7f586d1833b" />
      </div>
    </content>
  </entry>
  <entry>
    <title>How to Accept a server certificate with MSBuild and SVN</title>
    <link rel="alternate" type="text/html" href="http://www.blog.ingenuitynow.net/How+To+Accept+A+Server+Certificate+With+MSBuild+And+SVN.aspx" />
    <id>http://www.blog.ingenuitynow.net/PermaLink,guid,57f8aa3e-7b02-4a0c-845e-08b0fd9ff928.aspx</id>
    <published>2011-12-09T09:23:56.1591234-05:00</published>
    <updated>2011-12-09T09:23:56.1591234-05:00</updated>
    <category term="MSBuild" label="MSBuild" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,MSBuild.aspx" />
    <category term="Subversion" label="Subversion" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,Subversion.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
I’m using <a href="http://www.jetbrains.com/teamcity/" target="_blank">TeamCity</a>,
MSBuild, and SVN together to do some pretty cool stuff around continuous integration
and automated builds.  It’s been working well, untouched, for well over a year! 
But all of a sudden my deployment process failed. Checking the logs with TeamCity,
I found this:
</p>
        <p>
[11:18:06]:   Error validating server certificate for 'https://[xxx].com:443': 
<br />
[11:18:06]:    - The certificate is not issued by a trusted authority.
Use the 
<br />
[11:18:06]:      fingerprint to validate the certificate
manually! 
<br />
[11:18:06]:   Certificate information: 
<br />
[11:18:06]:    - Hostname: [xxx] 
<br />
[11:18:06]:    - Valid: from Mon, 29 Mar 2010 17:44:04 GMT until Thu,
26 Mar 2020 17:44:04 GMT 
<br />
[11:18:06]:    - Issuer: [xxx] 
<br />
[11:18:06]:    - Fingerprint: [xxx] 
<br />
[11:18:06]:   (R)eject, accept (t)emporarily or accept (p)ermanently? svn:
OPTIONS of 'https://[xxx]': Server certificate verification failed: issuer is not
trusted (https://[xxx].com)
</p>
        <p>
The best part about this was that, for a change, this error message was very clear. 
Although I don’t know why the certificate failed.  Did it change or expire? 
Did one of the network guys make a change to something?  I didn’t really care. 
I just wanted to get my build working again.
</p>
        <p>
Here is the line of MSBuild Script that caused the issue: (I’m using the SVN stuff
from <a href="http://msbuildtasks.tigris.org/" target="_blank">MSBuild Community Tasks</a>)
</p>
        <p>
&lt;SvnClient Command="ls $(SvnRepository)/tags/$(BuildNumber) Username="$(SvnUserName)"
password="$(SvnPassword)" ContinueOnError="true"&gt; 
<br />
  &lt;Output TaskParameter="ExitCode" PropertyName="Value"/&gt; 
<br />
&lt;/SvnClient&gt;
</p>
        <p>
Since the Command of SvnClient accepts any SVN command (in other words, anything that
I can type at a command prompt), I searched the SVN Documentation.  It wasn’t
hard to find that I just needed to update to this: 
</p>
        <p>
&lt;SvnClient Command="ls $(SvnRepository)/tags/$(BuildNumber) <font style="background-color: #ffff00">--non-interactive
--trust-server-cert</font>" Username="$(SvnUserName)" password="$(SvnPassword)"
ContinueOnError="true"&gt; 
<br />
  &lt;Output TaskParameter="ExitCode" PropertyName="Value"/&gt; 
<br />
&lt;/SvnClient&gt;
</p>
        <p>
This worked great but my build still failed.  That is because I have other SVN
actions in my script and they don’t seem to know that I accepted the certificate.
However, my other tasks are not using the very generic and flexible SvnClient Command,
they are using tasks such as SvnCopy or SvnCommit, etc.  For instance: 
</p>
        <p>
&lt;SvnCopy SourcePath="$(SvnRepository)/$(SvnProjectLocation)/" 
<br />
    DestinationPath="$(SvnRepository)/tags/$(BuildNumber)" 
<br />
    Message="Auto-tagging Revision: $(BuildNumber)" 
<br />
    Username="$(SvnUserName)" password="$(SvnPassword)"
/&gt; 
<br /></p>
        <p>
So how could I tell SVN to accept the certificate.  I checked the MSBuild Community
Tasks documentation, took a guess and got lucky on the first try…sweet.  I added
this:
</p>
        <p>
    &lt;SvnCopy SourcePath="$(SvnRepository)/$(SvnProjectLocation)/" 
<br />
        DestinationPath="$(SvnRepository)/tags/$(BuildNumber)" 
<br />
        Message="Auto-tagging Revision: $(BuildNumber)" 
<br />
        Username="$(SvnUserName)" password="$(SvnPassword)" 
<br />
        <font style="background-color: #ffff00">Arguments="--non-interactive
--trust-server-cert"</font> /&gt;
</p>
        <p>
I added the Arguments to all of my other SVN Tasks in my script and it worked perfectly!
</p>
        <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:a2ba1be3-f0e6-44e9-b821-7a8bb2239b22" class="wlWriterEditableSmartContent">Technorati
Tags: <a href="http://technorati.com/tags/TeamCity" rel="tag">TeamCity</a>,<a href="http://technorati.com/tags/Subversion" rel="tag">Subversion</a>,<a href="http://technorati.com/tags/SVN" rel="tag">SVN</a>,<a href="http://technorati.com/tags/MSBuild+Community+Tasks" rel="tag">MSBuild
Community Tasks</a>,<a href="http://technorati.com/tags/Certificate" rel="tag">Certificate</a></div>
        <img width="0" height="0" src="http://www.blog.ingenuitynow.net/aggbug.ashx?id=57f8aa3e-7b02-4a0c-845e-08b0fd9ff928" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Unit Testing Made Easy Demo</title>
    <link rel="alternate" type="text/html" href="http://www.blog.ingenuitynow.net/Unit+Testing+Made+Easy+Demo.aspx" />
    <id>http://www.blog.ingenuitynow.net/PermaLink,guid,646a5f43-0e3f-4e4b-a2f2-8347ef2b3f88.aspx</id>
    <published>2011-04-01T20:51:28.501719-04:00</published>
    <updated>2011-04-01T20:51:28.501719-04:00</updated>
    <category term=".Net" label=".Net" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,.Net.aspx" />
    <category term="C#" label="C#" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,C%23.aspx" />
    <category term="MOQ" label="MOQ" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,MOQ.aspx" />
    <category term="MVVM" label="MVVM" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,MVVM.aspx" />
    <category term="NoDeNUG.org" label="NoDeNUG.org" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,NoDeNUG.org.aspx" />
    <category term="NUnit" label="NUnit" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,NUnit.aspx" />
    <category term="Philly.Net" label="Philly.Net" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,Philly.Net.aspx" />
    <category term="Silverlight" label="Silverlight" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,Silverlight.aspx" />
    <category term="Unit Testing" label="Unit Testing" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,Unit%2BTesting.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
Last night I had the pleasure of presenting “Unit Testing Made Easy” at <a href="http://www.NoDeNUG.org" target="_blank">NoDeNUG</a> (Northern
Delaware .Net User Group).  I’ll be doing the same demo at <a href="http://codecamp.phillydotnet.org/2011-1/SitePages/Home.aspx" target="_blank">Philly.Net
Code Camp</a> next week.  The NoDeNUG crowd was great and asked good questions. 
That made the demo fun for me and I think it went very well.  I hope it goes
over great at Code Camp too.  
</p>
        <p>
I’m attaching a zip file containing my slides and Visual Studio Solution.  The
demo should work fine on it’s own, the references to <a href="http://nunit.org/" target="_blank">NUnit</a>, <a href="http://code.google.com/p/moq/" target="_blank">MOQ</a>,
and <a href="http://structuremap.net/structuremap/index.html" target="_blank">StructureMap</a> are
all included.  But if you don’t have VS2010 with Premium or Ultimate and Feature
Pack 2, the CodedUI (testing for Silverlight UIs) won’t work.  I hope it will
build and run ok though.  If not, let me know and I’ll upload the solution without
references to the Coded UI stuff.  Also, the demo uses a Silverlight application
for the front end.  If you don’t have the Silverlight tools set up, you can either
add them to Visual Studio, or simply exclude the Silverlight projects.  You’ll
still be able to run the services and the unit tests, even if you can’t see how the
UI looks.
</p>
        <p>
If you’ve just come across this and haven’t heard my presentation you have two options:
</p>
        <p>
1. Come to <a href="http://codecamp.phillydotnet.org/2011-1/SitePages/Home.aspx" target="_blank">Philly.Net
Code Camp</a> next week (April 9) and hear the presentation then!
</p>
        <p>
2. Just open the enclosed solution and have a look!  I’ve got sample code in
there to demonstrate using Dependency Injection to make services easier to unit test,
sample unit tests, and sample tests using mocking of dependencies.  I’m coding
my tests with <a href="http://www.nunit.org/" target="_blank">NUnit</a>, using <a href="http://structuremap.net/structuremap/index.html" target="_blank">StructureMap</a> for
my IOC, and <a href="http://code.google.com/p/moq/" target="_blank">MOQ</a> as my
mocking framework.
</p>
        <p>
Here is the code:  <a href="http://www.blog.ingenuitynow.net/content/binary/UnitTestingMadeEasy.zip" target="_blank">UnitTestingMadeEasy.zip</a></p>
        <p>
Again, if you have any problems with the solution, let me know and I’ll upload a copy
without the Coded UI, or even without the Silverlight part.
</p>
        <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:37d3ba7b-9df2-4da8-853f-f3fcc46ba52f" class="wlWriterEditableSmartContent">Technorati
Tags: <a href="http://technorati.com/tags/Unit+Test" rel="tag">Unit Test</a>,<a href="http://technorati.com/tags/C%23" rel="tag">C#</a>,<a href="http://technorati.com/tags/Dependency+Injection" rel="tag">Dependency
Injection</a>,<a href="http://technorati.com/tags/Mocking" rel="tag">Mocking</a>,<a href="http://technorati.com/tags/IOC" rel="tag">IOC</a></div>
        <img width="0" height="0" src="http://www.blog.ingenuitynow.net/aggbug.ashx?id=646a5f43-0e3f-4e4b-a2f2-8347ef2b3f88" />
      </div>
    </content>
  </entry>
  <entry>
    <title>WP7 Tip: Using the CameraCaptureTask for Windows Phone 7</title>
    <link rel="alternate" type="text/html" href="http://www.blog.ingenuitynow.net/WP7+Tip+Using+The+CameraCaptureTask+For+Windows+Phone+7.aspx" />
    <id>http://www.blog.ingenuitynow.net/PermaLink,guid,fc89dc0e-1ede-4121-b9df-4ed2af551da7.aspx</id>
    <published>2010-12-26T23:10:27.3081634-05:00</published>
    <updated>2010-12-26T23:10:27.3081634-05:00</updated>
    <category term=".Net" label=".Net" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,.Net.aspx" />
    <category term="C#" label="C#" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,C%23.aspx" />
    <category term="Silverlight" label="Silverlight" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,Silverlight.aspx" />
    <category term="Windows Phone 7" label="Windows Phone 7" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,Windows%2BPhone%2B7.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
In a previous post I wrote about some tips for creating WP7 apps which included using
the CameraCaptureTask.  A reader responded, asking how to save the picture. 
So I figured I’d write a more detailed response with some samples.
</p>
        <p>
In this post I’ll show 3 easy sample:
</p>
        <ol>
          <li>
How to simply use the camera and display a photo.</li>
          <li>
How to save an image to IsolatedStorage (and change the size of the photo too!)</li>
          <li>
How to save an image to the phone’s media library.</li>
        </ol>
        <p>
All of these are pretty simple.  I’m sure once you get started you can easily
modify this code to do more creative things.  
</p>
        <p>
First, you’ll need to define a CameraCaptureTask.  You should always do so as
a class level variable.   You should also wire up the Completed handler
in the constructor  These steps are important for dealing with application tombstoning. 
For more on this, <a href="http://msdn.microsoft.com/en-us/library/ff769543(v=VS.92).aspx" target="_blank">please
read this article</a>.
</p>
        <pre class="code">
          <span style="color: blue">private </span>
          <span style="color: #2b91af">CameraCaptureTask </span>_cameraCaptureTask; <span style="color: blue">public </span>MainPage()
{ InitializeComponent(); _cameraCaptureTask = <span style="color: blue">new </span><span style="color: #2b91af">CameraCaptureTask</span>();
_cameraCaptureTask.Completed += CameraCaptureTaskCompleted; }</pre>
        <h4>How to simply use the camera and display a photo:
</h4>
        <pre class="code">
          <font face="Verdana">You’ll need to kick off the camera task, I’m
using a button. </font>
        </pre>
        <pre class="code">
          <span style="color: blue">private void </span>SimpleTest_Click(<span style="color: blue">object </span>sender, <span style="color: #2b91af">EventArgs </span>e)
{ <span style="color: blue">try </span>{ _cameraCaptureTask.Show(); } <span style="color: blue">catch </span>(<span style="color: #2b91af">InvalidOperationException </span>ex)
{ <span style="color: green">// Catch the exception, but no handling is necessary. </span>}
} <font face="Verdana">In my XAML, I’ve defined an Image named MainImage</font></pre>
        <pre class="code">
          <span style="color: blue">&lt;</span>
          <span style="color: #a31515">Image </span>
          <span style="color: red">x</span>
          <span style="color: blue">:</span>
          <span style="color: red">Name</span>
          <span style="color: blue">="MainImage" </span>
          <span style="color: blue">/&gt; </span>
        </pre>
        <p>
Now when the CameraCaptureTask is complete, just bind the results of the task to the
Image as I’m doing here:
</p>
        <pre class="code">
          <span style="color: blue">void </span>CameraCaptureTaskCompleted(<span style="color: blue">object </span>sender, <span style="color: #2b91af">PhotoResult </span>e)
{ <span style="color: blue">if </span>(e.TaskResult == <span style="color: #2b91af">TaskResult</span>.OK)
{ <span style="color: green">//simply use the picture. </span><span style="color: #2b91af">BitmapImage </span>bitmapImage
= <span style="color: blue">new </span><span style="color: #2b91af">BitmapImage</span>();
bitmapImage.SetSource(e.ChosenPhoto); MainImage.Source = bitmapImage; } } <font face="Verdana">Easy,
right?</font></pre>
        <h4>How to save an image to IsolatedStorage (and change the size of the photo too!)
</h4>
        <p>
In this case I want to save the image to IsolatedStorage.  In addition, the app
I was creating didn’t need full size images.  So I figured, why waste space in
the user’s IsolatedStorage?  So I use a WritableBitmap and change the size of
the image.  You’ll notice that after I save the image, I read it back and bind
the results to another image named SmallerImage.  That’s just to prove that saving
it really worked!
</p>
        <pre class="code">
          <span style="color: blue">void </span>CameraCaptureTaskCompleted(<span style="color: blue">object </span>sender, <span style="color: #2b91af">PhotoResult </span>e)
{ <span style="color: blue">if </span>(e.TaskResult == <span style="color: #2b91af">TaskResult</span>.OK)
{ <span style="color: green">//here I save the image to Isolated Storage. Also I am
changing the size of it to not waste space! </span><span style="color: #2b91af">WriteableBitmap </span>writeableBitmap
= <span style="color: blue">new </span><span style="color: #2b91af">WriteableBitmap</span>(200,
200); writeableBitmap.LoadJpeg(e.ChosenPhoto); <span style="color: blue">string </span>imageFolder
= <span style="color: #a31515">"Images"</span>; <span style="color: blue">string </span>imageFileName
= <span style="color: #a31515">"TestImage.jpg"</span>; <span style="color: blue">using </span>(<span style="color: blue">var </span>isoFile
= <span style="color: #2b91af">IsolatedStorageFile</span>.GetUserStoreForApplication())
{ <span style="color: blue">if </span>(!isoFile.DirectoryExists(imageFolder)) { isoFile.CreateDirectory(imageFolder);
} <span style="color: blue">string </span>filePath = <span style="color: #2b91af">Path</span>.Combine(imageFolder,
imageFileName); <span style="color: blue">using </span>(<span style="color: blue">var </span>stream
= isoFile.CreateFile(filePath)) { writeableBitmap.SaveJpeg(stream, writeableBitmap.PixelWidth,
writeableBitmap.PixelHeight, 0, 100); } } <span style="color: green">//now read the
image back from storage to show it worked... </span><span style="color: #2b91af">BitmapImage </span>imageFromStorage
= <span style="color: blue">new </span><span style="color: #2b91af">BitmapImage</span>(); <span style="color: blue">using </span>(<span style="color: blue">var </span>isoFile
= <span style="color: #2b91af">IsolatedStorageFile</span>.GetUserStoreForApplication())
{ <span style="color: blue">string </span>filePath = <span style="color: #2b91af">Path</span>.Combine(imageFolder,
imageFileName); <span style="color: blue">using </span>(<span style="color: blue">var </span>imageStream
= isoFile.OpenFile( filePath, <span style="color: #2b91af">FileMode</span>.Open, <span style="color: #2b91af">FileAccess</span>.Read))
{ imageFromStorage.SetSource(imageStream); } } SmallerImage.Source = imageFromStorage;
} }</pre>
        <h4>How to save an image to the phone’s media library.
</h4>
        <p>
This one is pretty easy too.  Just remember to add a reference to Microsoft.Xna.Framework
or you can’t access the Media Library. Also, you’ll need this using statement:
</p>
        <pre class="code">
          <span style="color: blue">using </span>Microsoft.Xna.Framework.Media;</pre>
        <pre class="code">
          <span style="color: blue">void </span>CameraCaptureTaskForSavingToLibraryCompleted(<span style="color: blue">object </span>sender, <span style="color: #2b91af">PhotoResult </span>e)
{ <span style="color: blue">byte</span>[] imageBits = <span style="color: blue">new
byte</span>[(<span style="color: blue">int</span>)e.ChosenPhoto.Length]; e.ChosenPhoto.Read(imageBits,
0, imageBits.Length); e.ChosenPhoto.Seek(0, <span style="color: #2b91af">SeekOrigin</span>.Begin); <span style="color: #2b91af">MediaLibrary </span>library
= <span style="color: blue">new </span><span style="color: #2b91af">MediaLibrary</span>();
library.SavePicture(<span style="color: #a31515">"TestPhoto"</span>, imageBits);
}</pre>
        <p>
Hopefully you’ll see that using this feature is pretty easy.
</p>
        <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:84358b34-61cc-4043-a532-4cb12a6a6657" class="wlWriterEditableSmartContent">Technorati
Tags: <a href="http://technorati.com/tags/WP7" rel="tag">WP7</a>,<a href="http://technorati.com/tags/Windows+Phone+7" rel="tag">Windows
Phone 7</a>,<a href="http://technorati.com/tags/CameraCaptureTask" rel="tag">CameraCaptureTask</a>,<a href="http://technorati.com/tags/IsolatedStorage" rel="tag">IsolatedStorage</a>,<a href="http://technorati.com/tags/MediaLibrary" rel="tag">MediaLibrary</a></div>
        <img width="0" height="0" src="http://www.blog.ingenuitynow.net/aggbug.ashx?id=fc89dc0e-1ede-4121-b9df-4ed2af551da7" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Windows Phone 7 Application Development Tips</title>
    <link rel="alternate" type="text/html" href="http://www.blog.ingenuitynow.net/Windows+Phone+7+Application+Development+Tips.aspx" />
    <id>http://www.blog.ingenuitynow.net/PermaLink,guid,aa822965-c412-4eca-b0d8-189d1268dc0a.aspx</id>
    <published>2010-11-16T22:53:00-05:00</published>
    <updated>2011-01-06T22:54:10.5533861-05:00</updated>
    <category term=".Net" label=".Net" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,.Net.aspx" />
    <category term="C#" label="C#" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,C%23.aspx" />
    <category term="Silverlight" label="Silverlight" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,Silverlight.aspx" />
    <category term="Windows 7" label="Windows 7" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,Windows%2B7.aspx" />
    <content type="html">&lt;p&gt;
I recently started writing my first Windows Phone 7 Application.&amp;#160; I was shocked
at how easy it was to get started although I’ll admit that has a lot to do with my
existing knowledge of Silverlight.&amp;#160; If you don’t know Silverlight, now is a great
time to learn it.&amp;#160; You can write apps for the web, desktop, and Windows Phone
7 too!&amp;#160; 
&lt;/p&gt;
&lt;p&gt;
As I wrote my application, I compiled a list of tips to share.&amp;#160; I spent a bunch
of time tracking some of these down or figuring them out.&amp;#160; I hope this will be
helpful to other people.&amp;#160; Plus, I’ll use this as a reference for myself when
I create my next app! 
&lt;/p&gt;
&lt;p&gt;
Here you go.&amp;#160; The first two are pretty basic, but worth mentioning.
&lt;/p&gt;
&lt;h3&gt;Tip:&amp;#160; Go For It!
&lt;/h3&gt;
&lt;p&gt;
The best advice is to install the tools and try it out for yourself.&amp;#160; If you
create an application from one of the templates (you’ll get them after you install
the tools for Visual Studio) you find that the templates themselves are a great learning
tool.&amp;#160; Some include samples for styling, data binding, design time data binding,
navigation, and more!&amp;#160; I recommend trying each of the templates out.
&lt;/p&gt;
&lt;h3&gt;Tip: Install the Free Tools
&lt;/h3&gt;
&lt;p&gt;
This isn’t really a tip.&amp;#160; You can’t create apps without doing this!&amp;#160; Visit
the &lt;a href="http://create.msdn.com/en-us/home/getting_started" target="_blank"&gt;App
Hub&lt;/a&gt; to download the free tools.&amp;#160; You get all this for free: VS 2010 Express,
the Windows Phone Emulator, Expression Blend for Windows Phone, and more!&amp;#160; Also,
don’t forget to install &lt;a href="http://silverlight.codeplex.com/releases/view/55034" target="_blank"&gt;Silverlight
for Windows Phone Toolkit&lt;/a&gt; from the CodePlex site.&amp;#160; It includes some additional
controls that are great like AutoCompleteBox, ContextMenu, GestureListener and more!&amp;#160;
By the way, when you are at the App Hub, check out all the great learning resources.&amp;#160;
By the way, you should keep in mind that Windows Phone 7 is a subset of Silverlight
3!&amp;#160; That means that most features of 3 are there but not all.&amp;#160; And Silverlight
4 features are obviously not included.
&lt;/p&gt;
&lt;h3&gt;Tip: Using the Software Input Panel
&lt;/h3&gt;
&lt;p&gt;
The Software Input Panel (SIP) is the “on screen” keyboard that users use to key in
data.&amp;#160; It’s pretty cool and has some great features.&amp;#160; First, you don’t need
to turn it on.&amp;#160; If you include a TextBox in your app, when it gets focus the
SIP will appear.&amp;#160; But there are many versions of the SIP and you can easily configure
which one appears by setting the InputScope property of your TextBox.&amp;#160; You can
get a full list of the options &lt;a href="http://msdn.microsoft.com/en-us/library/system.windows.input.inputscopenamevalue(VS.95).aspx" target="_blank"&gt;here&lt;/a&gt;.&amp;#160;
If you want users to enter numbers, set InputScope to Digits.&amp;#160; For email address
entry, try EmailNameOrAddress and the keyboard will include the @ sign.&amp;#160; Some
choices are more subtle.&amp;#160; Choosing AddressCountryName may seem like the typical
keyboard but the first letter typed will be capitalized, remaining character will
be lower case.&amp;#160; There are many cool and smart features like that.&amp;#160; Also,
options like Text or Chat will include word suggestions!&amp;#160; Here are some code
samples and screen shots:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;TextBox &lt;/span&gt;&lt;span style="color: red"&gt;InputScope&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;EmailNameOrAddress&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Height&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;75&amp;quot;
/&amp;gt; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;TextBox &lt;/span&gt;&lt;span style="color: red"&gt;InputScope&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Text&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Height&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;75&amp;quot;
/&amp;gt; &lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
&lt;a href="http://www.blog.ingenuitynow.net/content/binary/Windows-Live-Writer/dfcf1ff1a700_13347/image_2.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.blog.ingenuitynow.net/content/binary/Windows-Live-Writer/dfcf1ff1a700_13347/image_thumb.png" width="190" height="244" /&gt;&lt;/a&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;a href="http://www.blog.ingenuitynow.net/content/binary/Windows-Live-Writer/dfcf1ff1a700_13347/image_4.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.blog.ingenuitynow.net/content/binary/Windows-Live-Writer/dfcf1ff1a700_13347/image_thumb_1.png" width="226" height="244" /&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
The first screenshot is the SIP when the EmailNameOrAddress version has focus.&amp;#160;
In the second screen shot (TextBox InputScope set to Text), I typed a few letters
and you can see the word suggestions!
&lt;/p&gt;
&lt;h3&gt;Tip:&amp;#160; Orientation
&lt;/h3&gt;
&lt;p&gt;
If you want your application to support both portrait and landscape mode (when the
user tilts the phone over), you need to set up each page’s SupportedOrientations property
to do so as follows.&amp;#160; Set the default value in the Orientation property.
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;phone&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;PhoneApplicationPage &lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;Class&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;WindowsPhoneApplication1.MainPage&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;phone&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;shell&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;d&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;http://schemas.microsoft.com/expression/blend/2008&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;mc&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;http://schemas.openxmlformats.org/markup-compatibility/2006&amp;quot; &lt;/span&gt;&lt;span style="color: blue"&gt; &lt;/span&gt;&lt;font style="background-color: #ffff00"&gt;&lt;span style="color: red"&gt;SupportedOrientations&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;PortraitOrLandscape&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Orientation&lt;/span&gt;&lt;/font&gt;&lt;span style="color: blue"&gt;&lt;font style="background-color: #ffff00"&gt;=&amp;quot;Portrait&amp;quot;&lt;/font&gt; &lt;/span&gt;&lt;/pre&gt;
&lt;h3&gt;Tip: UI Design
&lt;/h3&gt;
&lt;p&gt;
First, you should check out the &lt;a href="http://go.microsoft.com/fwlink/?LinkID=200145" target="_blank"&gt;UI
Design and Interaction Guide&lt;/a&gt; for tips on how to properly design and style the
look and feel of your application.&amp;#160; Also, you should try to use the standard
styles provided when you create an application.&amp;#160; Setting the style in this way
will give your application a consistent look.&amp;#160; Here are a few samples:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;TextBlock &lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;ApplicationTitle&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Text&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Demo&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Style&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{&lt;/span&gt;&lt;span style="color: #a31515"&gt;StaticResource &lt;/span&gt;&lt;span style="color: red"&gt;PhoneTextNormalStyle&lt;/span&gt;&lt;span style="color: blue"&gt;}&amp;quot;/&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;TextBlock &lt;/span&gt;&lt;span style="color: red"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;Name&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;PageTitle&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Text&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;first
page&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Style&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{&lt;/span&gt;&lt;span style="color: #a31515"&gt;StaticResource &lt;/span&gt;&lt;span style="color: red"&gt;PhoneTextTitle1Style&lt;/span&gt;&lt;span style="color: blue"&gt;}&amp;quot;/&amp;gt; &lt;/span&gt;&lt;/pre&gt;
&lt;h3&gt;Tip: Using Isolated Storage
&lt;/h3&gt;
&lt;p&gt;
For simple values, you can easily get in and out of IsolatedStorageSettings.ApplicationSettings
(Dictionary&lt;string  , Object&gt;
). For more complex data, you can put it into IsolatedStorage.&amp;#160; Since there is
no database available in Wp7, one common approach to saving data is to simple serialize
your model and save it as a file.&amp;#160; To do so, make sure you mark all of your Model’s
classes with the attribute DataContract and make sure the properties have the DataMember
attribute.&amp;#160; Here’s a sample class called ListOfStuff as well as the methods I
use to read/write it to/from IsolatedStorage: 
&lt;/p&gt;
&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;DataContract&lt;/span&gt;] &lt;span style="color: blue"&gt;public
class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ListOfStuff &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;Observable &lt;/span&gt;{
[&lt;span style="color: #2b91af"&gt;DataMember&lt;/span&gt;] &lt;span style="color: blue"&gt;public
string &lt;/span&gt;Title { &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return &lt;/span&gt;_title;
} &lt;span style="color: blue"&gt;set &lt;/span&gt;{ &lt;span style="color: blue"&gt;if &lt;/span&gt;(_title
!= &lt;span style="color: blue"&gt;value&lt;/span&gt;) { _title = &lt;span style="color: blue"&gt;value&lt;/span&gt;;
NotifyPropertyChanged(&lt;span style="color: #a31515"&gt;&amp;quot;Title&amp;quot;&lt;/span&gt;); } }
} &lt;span style="color: blue"&gt;private string &lt;/span&gt;_title; [&lt;span style="color: #2b91af"&gt;DataMember&lt;/span&gt;] &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ObservableCollection&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;ListCategory&lt;/span&gt;&amp;gt;
Categories { &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return &lt;/span&gt;_categories;
} &lt;span style="color: blue"&gt;set &lt;/span&gt;{ &lt;span style="color: blue"&gt;if &lt;/span&gt;(_categories
!= &lt;span style="color: blue"&gt;value&lt;/span&gt;) { _categories = &lt;span style="color: blue"&gt;value&lt;/span&gt;;
NotifyPropertyChanged(&lt;span style="color: #a31515"&gt;&amp;quot;Categories&amp;quot;&lt;/span&gt;);
} } } &lt;span style="color: blue"&gt;private &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ObservableCollection&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;ListCategory&lt;/span&gt;&amp;gt;
_categories = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ObservableCollection&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;ListCategory&lt;/span&gt;&amp;gt;();
}&lt;/pre&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static void &lt;/span&gt;SaveToFile(&lt;span style="color: #2b91af"&gt;ListOfStuff &lt;/span&gt;listOfStuff)
{ &lt;span style="color: blue"&gt;using &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;IsolatedStorageFile &lt;/span&gt;isolatedStorageFile
= &lt;span style="color: #2b91af"&gt;IsolatedStorageFile&lt;/span&gt;.GetUserStoreForApplication())
{ &lt;span style="color: blue"&gt;using &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;IsolatedStorageFileStream &lt;/span&gt;stream
= &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IsolatedStorageFileStream&lt;/span&gt;(&lt;span style="color: blue"&gt;string&lt;/span&gt;.Format(&lt;span style="color: #a31515"&gt;&amp;quot;{0}.dat&amp;quot;&lt;/span&gt;,
listOfStuff.Title), &lt;span style="color: #2b91af"&gt;FileMode&lt;/span&gt;.Create, isolatedStorageFile))
{ &lt;span style="color: #2b91af"&gt;DataContractSerializer &lt;/span&gt;serializer = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DataContractSerializer&lt;/span&gt;(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;ListOfStuff&lt;/span&gt;));
serializer.WriteObject(stream, listOfStuff); } } } &lt;span style="color: blue"&gt;public
static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ListOfStuff &lt;/span&gt;LoadFromFile(&lt;span style="color: blue"&gt;string &lt;/span&gt;listName)
{ &lt;span style="color: #2b91af"&gt;ListOfStuff &lt;/span&gt;listOfStuff = &lt;span style="color: blue"&gt;null&lt;/span&gt;; &lt;span style="color: blue"&gt;using &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;IsolatedStorageFile &lt;/span&gt;isf
= &lt;span style="color: #2b91af"&gt;IsolatedStorageFile&lt;/span&gt;.GetUserStoreForApplication())
{ &lt;span style="color: blue"&gt;using &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;IsolatedStorageFileStream &lt;/span&gt;stream
= &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IsolatedStorageFileStream&lt;/span&gt;(&lt;span style="color: blue"&gt;string&lt;/span&gt;.Format(&lt;span style="color: #a31515"&gt;&amp;quot;{0}.dat&amp;quot;&lt;/span&gt;,
listName), &lt;span style="color: #2b91af"&gt;FileMode&lt;/span&gt;.OpenOrCreate, isf)) { &lt;span style="color: blue"&gt;if &lt;/span&gt;(stream.Length
&amp;gt; 0) { &lt;span style="color: #2b91af"&gt;DataContractSerializer &lt;/span&gt;serializer = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DataContractSerializer&lt;/span&gt;(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;ListOfStuff&lt;/span&gt;));
listOfStuff = serializer.ReadObject(stream) &lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ListOfStuff&lt;/span&gt;;
} } } &lt;span style="color: blue"&gt;return &lt;/span&gt;listOfStuff; }&lt;/pre&gt;
&lt;h3&gt;Tip: Using the Phone’s Camera
&lt;/h3&gt;
&lt;p&gt;
Using the phone couldn’t be simpler, just use the CameraCaptureTask as follows:
&lt;/p&gt;
&lt;p&gt;
&lt;em&gt;**NOTE: As reader Andy Wilkinson pointed out in a comment to this post, I’m not
following best practices here.&amp;#160; The way I declare my CameraCaptureTask could
lead to problems with tombstoning.&amp;#160; He points out a &lt;/em&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ff769543(v=VS.92).aspx" target="_blank"&gt;&lt;em&gt;nice
article on MSDN&lt;/em&gt;&lt;/a&gt;&lt;em&gt; that explains the proper use, please read it &lt;/em&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ff769543(v=VS.92).aspx" target="_blank"&gt;&lt;em&gt;here&lt;/em&gt;&lt;/a&gt;&lt;em&gt;.&amp;#160;
Thanks, Andy, for the assistance!&lt;/em&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;em&gt;**Another Note:&amp;#160; Another user asked about saving the results of the CameraCaptureTask.&amp;#160;
So I’ve written another post that you may find useful.&amp;#160; Please check it out &lt;a href="http://www.blog.ingenuitynow.net/WP7+Tip+Using+The+CameraCaptureTask+For+Windows+Phone+7.aspx" target="_blank"&gt;here&lt;/a&gt;.&lt;/em&gt;
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;private void &lt;/span&gt;Camera_Click(&lt;span style="color: blue"&gt;object &lt;/span&gt;sender, &lt;span style="color: #2b91af"&gt;RoutedEventArgs &lt;/span&gt;e)
{ &lt;span style="color: #2b91af"&gt;CameraCaptureTask &lt;/span&gt;cameraCaptureTask = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;CameraCaptureTask&lt;/span&gt;();
cameraCaptureTask.Completed += cameraCaptureTask_Completed; cameraCaptureTask.Show();
} &lt;font face="Verdana"&gt;After the picture is taken, you can access it later, as a Stream.
You can assign it to an Image, save it, or do whatever you want.&lt;/font&gt;&lt;/pre&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;void &lt;/span&gt;cameraCaptureTask_Completed(&lt;span style="color: blue"&gt;object &lt;/span&gt;sender, &lt;span style="color: #2b91af"&gt;PhotoResult &lt;/span&gt;e)
{ &lt;span style="color: blue"&gt;var &lt;/span&gt;photoStream = e.ChosenPhoto; }&lt;/pre&gt;
&lt;h3&gt;Tip: Using Gestures
&lt;/h3&gt;
&lt;p&gt;
Of course, you want your application to react when users use common phone Gestures
such as Flick, Pinch, Hold, etc.&amp;#160; Doing so is easy too!&amp;#160; The Silverlight
Toolkit for Windows Phone (mentioned in second tip above) provides the GestureListener
that you will need.&amp;#160; You can see how simple it is to wire up in the following
example. In this case, I want the user to be able to hold their finger down on an
image to go to “edit mode” where a description TextBox will appear.&amp;#160; In the NoteHold()
method (not shown), you can imaging all I need to do is set the description TextBox
to be visible.
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Image &lt;/span&gt;&lt;span style="color: red"&gt;Source&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{&lt;/span&gt;&lt;span style="color: #a31515"&gt;Binding &lt;/span&gt;&lt;span style="color: red"&gt;Image&lt;/span&gt;&lt;span style="color: blue"&gt;}&amp;quot;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;ToolkitControls&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;GestureService.GestureListener&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;ToolkitControls&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;GestureListener &lt;/span&gt;&lt;span style="color: red"&gt;Hold&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;NoteHold&amp;quot;
/&amp;gt; &amp;lt;/&amp;lt; span&amp;gt;&lt;span style="color: #a31515"&gt;ToolkitControls&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;GestureService.GestureListener&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&amp;lt; span&amp;gt;&lt;span style="color: #a31515"&gt;Image&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt; &lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;
Now go start creating apps!
&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:03401b9b-262a-4072-9d73-5efe6f4cca93" class="wlWriterEditableSmartContent"&gt;Technorati
Tags: &lt;a href="http://technorati.com/tags/Windows+Phone+7" rel="tag"&gt;Windows Phone
7&lt;/a&gt;,&lt;a href="http://technorati.com/tags/IsolatedStorage" rel="tag"&gt;IsolatedStorage&lt;/a&gt;,&lt;a href="http://technorati.com/tags/CameraCaptureTask" rel="tag"&gt;CameraCaptureTask&lt;/a&gt;,&lt;a href="http://technorati.com/tags/GestureListener" rel="tag"&gt;GestureListener&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Software+Input+Panel" rel="tag"&gt;Software
Input Panel&lt;/a&gt;,&lt;a href="http://technorati.com/tags/WP7" rel="tag"&gt;WP7&lt;/a&gt;
&lt;/div&gt;
&lt;img width="0" height="0" src="http://www.blog.ingenuitynow.net/aggbug.ashx?id=aa822965-c412-4eca-b0d8-189d1268dc0a" /&gt;</content>
  </entry>
  <entry>
    <title>Silverlight 4: Drag and Drop DataGrids with Access to the Data Being Dropped</title>
    <link rel="alternate" type="text/html" href="http://www.blog.ingenuitynow.net/Silverlight+4+Drag+And+Drop+DataGrids+With+Access+To+The+Data+Being+Dropped.aspx" />
    <id>http://www.blog.ingenuitynow.net/PermaLink,guid,d84cf8a7-6bf7-4b21-a206-8e1201cb7b2d.aspx</id>
    <published>2010-09-22T00:18:14.4074179-04:00</published>
    <updated>2010-09-22T00:18:14.4074179-04:00</updated>
    <category term=".Net" label=".Net" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,.Net.aspx" />
    <category term="Silverlight" label="Silverlight" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,Silverlight.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
If you’ve been searching the web for a good sample of how to drag and drop data from
one DataGrid to another in Silverlight, you’ve probably found some great samples out
there, showing “how easy” it can be by just surrounding your DataGrid with DataGridDragDropTarget
tags.  In fact, if you do that, you can drag and drop data from one grid to the
other.  The problem is that most of these demos are about the visual aspect of
drag and drop.  But in the real world, there is more work to be done.  Most
of the samples that I found did not deal with how to save the data once it was dropped
in the other grid.  I’m using the MVVM pattern so basically what I need to do
is, once the drop is complete, update my ViewModel.
</p>
        <p>
The tricky part was figuring out what information was actually dropped on my target
DataGrid.  It’s not at all obvious how to do that.  The Drop event uses
DragEventArgs which has a Data property.  You’d think this would be the data
from the other DataGrid, but you’d be wrong.  Data has a GetData() method but
you need to provide it a parameter specifying the format of the Data.  Once you
figure out how to do that, you’ll find that GetData() doesn’t actually give you the
data!  It give you ItemDragEventArgs, which also has a Data property.  Is
this Data the data you want?  Not exactly, but you are getting close.  This
Data can be cast as a SelectionCollection – that is because a DataGrid supports multiple
selections at the same time!  And each Selection in the collection is the data
you actually want.  Is it me, or is that pretty confusing?  
</p>
        <p>
I wouldn’t have gotten this far, but searching the web for a while, I stumbled across
a <a href="http://stackoverflow.com/questions/2365768/how-can-i-get-the-dragged-element-in-a-silverlight-app" target="_blank">post
on StackOverflow</a> that was helpful.  The response also references a <a href="http://themechanicalbride.blogspot.com/2009/10/silverlight-drag-drop-support-part-2.html" target="_blank">blog
post found here</a> as well.  
</p>
        <p>
Those samples I found were really helpful but it seemed like it could be made easier. 
Starting with sample code from the blog post mentioned above, I created a very simple,
reusable, Extension Method that uses Generics and does all of the heavy lifting for
you!  This should make things pretty easy now.
</p>
        <p>
Here is some of the sample code and a sample solution is attached <a href="http://www.blog.ingenuitynow.net/content/binary/DataGridDragAndDropSample.zip" target="_blank">here</a>.    
</p>
        <p>
First, here is the extension method that gets the underlying data that was dropped
on the target:
</p>
        <pre class="code">
          <span style="color: blue">public static </span>
          <span style="color: #2b91af">IEnumerable</span>&lt;T&gt;
GetData&lt;T&gt;(<span style="color: blue">this </span><span style="color: #2b91af">DragEventArgs </span>args)
{ <span style="color: #2b91af">IEnumerable</span>&lt;T&gt; results = <span style="color: blue">null</span>; <span style="color: green">//
Get the dropped data from the Data property and cast it to the first format. </span><span style="color: #2b91af">ItemDragEventArgs </span>dragEventArgs
= args.Data.GetData(args.Data.GetFormats()[0]) <span style="color: blue">as </span><span style="color: #2b91af">ItemDragEventArgs</span>; <span style="color: blue">if </span>(dragEventArgs
== <span style="color: blue">null</span>) <span style="color: blue">return </span>results; <span style="color: green">//
Get the collection of items </span><span style="color: #2b91af">SelectionCollection </span>selectionCollection
= dragEventArgs.Data <span style="color: blue">as </span><span style="color: #2b91af">SelectionCollection</span>; <span style="color: blue">if </span>(selectionCollection
!= <span style="color: blue">null</span>) { <span style="color: green">// cast each
item to what is expected </span>results = selectionCollection.Select(selection =&gt;
selection.Item).OfType&lt;T&gt;(); } <span style="color: blue">return </span>results;
}</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
Here is the XAML for the two grids.  Note that in this sample (you can change
this of course) I’m using AllowedSourceEffects.Copy so that the data remains in the
first grid and is copied to the second.
</p>
        <pre class="code">
          <span style="color: blue">&lt;</span>
          <span style="color: #a31515">Toolkit</span>
          <span style="color: blue">:</span>
          <span style="color: #a31515">DataGridDragDropTarget </span>
          <span style="color: red">AllowedSourceEffects</span>
          <span style="color: blue">="Copy" </span>
          <span style="color: red">Grid.Column</span>
          <span style="color: blue">="0" </span>
          <span style="color: red">Grid.Row</span>
          <span style="color: blue">="0"&gt;
&lt;</span>
          <span style="color: #a31515">Controls</span>
          <span style="color: blue">:</span>
          <span style="color: #a31515">DataGrid </span>
          <span style="color: red">x</span>
          <span style="color: blue">:</span>
          <span style="color: red">Name</span>
          <span style="color: blue">="Data" </span>
          <span style="color: red">ItemsSource</span>
          <span style="color: blue">="{</span>
          <span style="color: #a31515">Binding </span>
          <span style="color: red">Data</span>
          <span style="color: blue">}" </span>
          <span style="color: red">AutoGenerateColumns</span>
          <span style="color: blue">="False"&gt;
&lt;</span>
          <span style="color: #a31515">Controls</span>
          <span style="color: blue">:</span>
          <span style="color: #a31515">DataGrid.Columns</span>
          <span style="color: blue">&gt;
&lt;</span>
          <span style="color: #a31515">Controls</span>
          <span style="color: blue">:</span>
          <span style="color: #a31515">DataGridTextColumn </span>
          <span style="color: red">Header</span>
          <span style="color: blue">="Id" </span>
          <span style="color: red">Binding</span>
          <span style="color: blue">="{</span>
          <span style="color: #a31515">Binding </span>
          <span style="color: red">Id</span>
          <span style="color: blue">}"
/&gt; &lt;</span>
          <span style="color: #a31515">Controls</span>
          <span style="color: blue">:</span>
          <span style="color: #a31515">DataGridTextColumn </span>
          <span style="color: red">Header</span>
          <span style="color: blue">="First
Name" </span>
          <span style="color: red">Binding</span>
          <span style="color: blue">="{</span>
          <span style="color: #a31515">Binding </span>
          <span style="color: red">FirstName</span>
          <span style="color: blue">}"
/&gt; &lt;</span>
          <span style="color: #a31515">Controls</span>
          <span style="color: blue">:</span>
          <span style="color: #a31515">DataGridTextColumn </span>
          <span style="color: red">Header</span>
          <span style="color: blue">="Last
Name" </span>
          <span style="color: red">Binding</span>
          <span style="color: blue">="{</span>
          <span style="color: #a31515">Binding </span>
          <span style="color: red">LastName</span>
          <span style="color: blue">}"
/&gt; &lt;/</span>
          <span style="color: #a31515">Controls</span>
          <span style="color: blue">:</span>
          <span style="color: #a31515">DataGrid.Columns</span>
          <span style="color: blue">&gt;
&lt;/</span>
          <span style="color: #a31515">Controls</span>
          <span style="color: blue">:</span>
          <span style="color: #a31515">DataGrid</span>
          <span style="color: blue">&gt;
&lt;/</span>
          <span style="color: #a31515">Toolkit</span>
          <span style="color: blue">:</span>
          <span style="color: #a31515">DataGridDragDropTarget</span>
          <span style="color: blue">&gt;
&lt;</span>
          <span style="color: #a31515">Toolkit</span>
          <span style="color: blue">:</span>
          <span style="color: #a31515">DataGridDragDropTarget </span>
          <span style="color: red">AllowedSourceEffects</span>
          <span style="color: blue">="Copy" </span>
          <span style="color: red">Drop</span>
          <span style="color: blue">="DataGridDragDropTarget_Drop" </span>
          <span style="color: red">Grid.Column</span>
          <span style="color: blue">="1" </span>
          <span style="color: red">Grid.Row</span>
          <span style="color: blue">="0"&gt;
&lt;</span>
          <span style="color: #a31515">Controls</span>
          <span style="color: blue">:</span>
          <span style="color: #a31515">DataGrid </span>
          <span style="color: red">x</span>
          <span style="color: blue">:</span>
          <span style="color: red">Name</span>
          <span style="color: blue">="MoreData" </span>
          <span style="color: red">ItemsSource</span>
          <span style="color: blue">="{</span>
          <span style="color: #a31515">Binding </span>
          <span style="color: red">MoreData</span>
          <span style="color: blue">}" </span>
          <span style="color: red">AutoGenerateColumns</span>
          <span style="color: blue">="False" </span>
          <span style="color: red">AllowDrop</span>
          <span style="color: blue">="True"&gt;
&lt;</span>
          <span style="color: #a31515">Controls</span>
          <span style="color: blue">:</span>
          <span style="color: #a31515">DataGrid.Columns</span>
          <span style="color: blue">&gt;
&lt;</span>
          <span style="color: #a31515">Controls</span>
          <span style="color: blue">:</span>
          <span style="color: #a31515">DataGridTextColumn </span>
          <span style="color: red">Header</span>
          <span style="color: blue">="Id" </span>
          <span style="color: red">Binding</span>
          <span style="color: blue">="{</span>
          <span style="color: #a31515">Binding </span>
          <span style="color: red">Id</span>
          <span style="color: blue">}"
/&gt; &lt;</span>
          <span style="color: #a31515">Controls</span>
          <span style="color: blue">:</span>
          <span style="color: #a31515">DataGridTextColumn </span>
          <span style="color: red">Header</span>
          <span style="color: blue">="First
Name" </span>
          <span style="color: red">Binding</span>
          <span style="color: blue">="{</span>
          <span style="color: #a31515">Binding </span>
          <span style="color: red">FirstName</span>
          <span style="color: blue">}"
/&gt; &lt;</span>
          <span style="color: #a31515">Controls</span>
          <span style="color: blue">:</span>
          <span style="color: #a31515">DataGridTextColumn </span>
          <span style="color: red">Header</span>
          <span style="color: blue">="Last
Name" </span>
          <span style="color: red">Binding</span>
          <span style="color: blue">="{</span>
          <span style="color: #a31515">Binding </span>
          <span style="color: red">LastName</span>
          <span style="color: blue">}"
/&gt; &lt;/</span>
          <span style="color: #a31515">Controls</span>
          <span style="color: blue">:</span>
          <span style="color: #a31515">DataGrid.Columns</span>
          <span style="color: blue">&gt;
&lt;/</span>
          <span style="color: #a31515">Controls</span>
          <span style="color: blue">:</span>
          <span style="color: #a31515">DataGrid</span>
          <span style="color: blue">&gt;
&lt;/</span>
          <span style="color: #a31515">Toolkit</span>
          <span style="color: blue">:</span>
          <span style="color: #a31515">DataGridDragDropTarget</span>
          <span style="color: blue">&gt; </span>
        </pre>
        <p>
Lastly, here is the code behind piece that handles the Drop event, get’s the data
and passes it to my ViewModel.  Note here that I specify e.Handled = true. 
Without that, the DragDropTarget would automatically copy the data again.
</p>
        <pre class="code">
          <span style="color: blue">private void </span>DataGridDragDropTarget_Drop(<span style="color: blue">object </span>sender,
Microsoft.Windows.<span style="color: #2b91af">DragEventArgs </span>e) { <span style="color: green">//use
the extension method to get the data from the DragEventArgs </span><span style="color: blue">var </span>data
= e.GetData&lt;<span style="color: #2b91af">Person</span>&gt;(); <span style="color: blue">foreach </span>(<span style="color: blue">var </span>item <span style="color: blue">in </span>data)
{ _viewModel.AddToMoreData(item); } e.Handled = <span style="color: blue">true</span>;
}</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
Once again, <a href="http://www.blog.ingenuitynow.net/content/binary/DataGridDragAndDropSample.zip" target="_blank">here
is a link to a complete sample project</a></p>
        <img width="0" height="0" src="http://www.blog.ingenuitynow.net/aggbug.ashx?id=d84cf8a7-6bf7-4b21-a206-8e1201cb7b2d" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Visual Studio Tip for working with Code Regions</title>
    <link rel="alternate" type="text/html" href="http://www.blog.ingenuitynow.net/Visual+Studio+Tip+For+Working+With+Code+Regions.aspx" />
    <id>http://www.blog.ingenuitynow.net/PermaLink,guid,b3b292a4-4844-4e99-a0a7-f252f029c133.aspx</id>
    <published>2010-08-23T22:30:05.6870221-04:00</published>
    <updated>2010-08-23T22:30:05.6870221-04:00</updated>
    <category term=".Net" label=".Net" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,.Net.aspx" />
    <category term="C#" label="C#" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,C%23.aspx" />
    <category term="Visual Studio" label="Visual Studio" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,Visual%2BStudio.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
I love working with other developers, whether it be formal “paired programming” or
just one person helping the other with a problem.  I love it so much because
I always learn something new.  Sometimes what I learn is some major, powerful
technique for doing something complicated but usually it is not.  Usually it
is one of those simple, cool, helpful tidbits.  You probably know what I mean. 
You sit with a coworker and without any fanfare they type some keystroke and you say,
“Whoa, I didn’t know you could do that… how did you do that?”  Here’s an example
of something I do that I’ve noticed many developers aren’t aware of. It’s nothing
fancy, but I find it helpful.
</p>
        <p>
Like many developers, I like to surround my code in collapsible regions.  I don’t
get too carried away with this technique, but I usually have a region for Public Properties,
another for Public Methods, maybe one for Class Level variables.  Stuff like
that.  Anyway, most people label the top of the region but I’ve found that many
developers don’t know you can label the bottom.  That extra label on the bottom
is really helpful when you are scrolled down far.  Without the label, you find
an #endregion and you don’t know what region it is the end of.  This is especially
tricky when working with nested tags.  How do you do it?  Just type the
label :).
</p>
        <h5>#endregion with label:
</h5>
        <pre class="code">
          <span style="color: blue">#region </span>Public Methods <span style="color: green">//
put stuff here... </span><span style="color: blue">#endregion </span>Public Methods</pre>
        <p>
I think the reason people don’t do this more is that (I’m pretty sure) you could NOT
do this with some earlier version of Visual Studio.  Those of us that have worked
with VS for a long time just got used to doing #endregions without the label. 
Back then I used to put a comment at the end of my region like this, but it isn’t
necessary anymore:
</p>
        <pre class="code">
          <span style="color: blue">#region </span>Public Methods <span style="color: green">//
put stuff here... </span><span style="color: blue">#endregion </span><span style="color: green">//Public
Methods </span></pre>
        <p>
But at some point, maybe VS2008, we were allowed to just put the label at the end.  
</p>
        <h5>But wait, there’s more…
</h5>
        <p>
Recently I had the idea to update the code snippet for “Surround with Region”. 
You can use this built in Visual Studio snippet by highlighting some code and hitting
“Ctrl + s”, then select #region from the dropdown.  When you do, your code is
automatically surrounded by the #region and #endregion, plus you’ll be prompted for
the text for the label.  I tweaked it so it puts the label at the beginning and
the end.  
</p>
        <ul>
          <li>
You can find your snippets by going to the Code Snippets Manager (Tools &gt; Code
Snippets Manager, or Ctrl + k, Ctrl + b). 
</li>
          <li>
Then select Visual C# (or VB I guess, I don’t use it :) ) 
</li>
          <li>
When you click on the folder or the name of the snippet, the path is shown in the
window.  See the screen shot below… 
</li>
          <li>
In my case, and I think it is pretty standard, I open the snippet in Notepad at the
following path: C:\Program Files\Microsoft Visual Studio 10.0\VC#\Snippets\1033\Visual
C#\pp_region.snippet 
</li>
          <li>
Then change the snippet and use the XML listed below. 
</li>
          <li>
The only thing I changed was the 24th line.  After “#endregion” I put in the
$name$ variable.  The variable was already used in the snippet on line 22 so
it was easy.  
</li>
          <li>
Note: If you can’t save this snippet with notepad, make sure Visual Studio is closed
and run notepad “as administrator”. 
</li>
        </ul>
        <h5>Snippet XML:
</h5>
        <p>
&lt;?xml version="1.0" encoding="utf-8" ?&gt; 
<br />
&lt;CodeSnippets  xmlns="<a href="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet&quot;">http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"</a>&gt; 
<br />
    &lt;CodeSnippet Format="1.0.0"&gt; 
<br />
        &lt;Header&gt; 
<br />
            &lt;Title&gt;#region&lt;/Title&gt; 
<br />
            &lt;Shortcut&gt;#region&lt;/Shortcut&gt; 
<br />
            &lt;Description&gt;Code
snippet for #region&lt;/Description&gt; 
<br />
            &lt;Author&gt;Microsoft
Corporation&lt;/Author&gt; 
<br />
            &lt;SnippetTypes&gt; 
<br />
               
&lt;SnippetType&gt;Expansion&lt;/SnippetType&gt; 
<br />
               
&lt;SnippetType&gt;SurroundsWith&lt;/SnippetType&gt; 
<br />
            &lt;/SnippetTypes&gt; 
<br />
        &lt;/Header&gt; 
<br />
        &lt;Snippet&gt; 
<br />
            &lt;Declarations&gt; 
<br />
               
&lt;Literal&gt; 
<br />
                   
&lt;ID&gt;name&lt;/ID&gt; 
<br />
                   
&lt;ToolTip&gt;Region name&lt;/ToolTip&gt; 
<br />
                   
&lt;Default&gt;MyRegion&lt;/Default&gt; 
<br />
               
&lt;/Literal&gt; 
<br />
            &lt;/Declarations&gt; 
<br />
            &lt;Code Language="csharp"&gt;&lt;![CDATA[#region
$name$ 
<br />
        $selected$ $end$ 
<br />
    #endregion $name$]]&gt; 
<br />
            &lt;/Code&gt; 
<br />
        &lt;/Snippet&gt; 
<br />
    &lt;/CodeSnippet&gt; 
<br />
&lt;/CodeSnippets&gt;
</p>
        <h5>Code Snippets Manager:
</h5>
        <p>
          <a href="http://www.blog.ingenuitynow.net/content/binary/WindowsLiveWriter/VisualStudioTipforworkingwithCodeRegions_13286/image_2.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.blog.ingenuitynow.net/content/binary/WindowsLiveWriter/VisualStudioTipforworkingwithCodeRegions_13286/image_thumb.png" width="494" height="365" />
          </a>
        </p>
        <p>
Code Snippets are really cool and easy to use.  For more information, <a href="http://www.blog.ingenuitynow.net/Using+Custom+Code+Snippets+In+VS2008.aspx" target="_blank">please
see this post</a>.
</p>
        <img width="0" height="0" src="http://www.blog.ingenuitynow.net/aggbug.ashx?id=b3b292a4-4844-4e99-a0a7-f252f029c133" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Silverlight: Creating a WriteableBitmap from a Uri Source</title>
    <link rel="alternate" type="text/html" href="http://www.blog.ingenuitynow.net/Silverlight+Creating+A+WriteableBitmap+From+A+Uri+Source.aspx" />
    <id>http://www.blog.ingenuitynow.net/PermaLink,guid,7e7870a9-c5bf-476c-bd94-fad3f0d3ae2a.aspx</id>
    <published>2010-08-13T22:22:52.1283523-04:00</published>
    <updated>2010-08-13T22:22:52.1283523-04:00</updated>
    <category term=".Net" label=".Net" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,.Net.aspx" />
    <category term="Silverlight" label="Silverlight" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,Silverlight.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
I’ve seen a few posts describing how to use a Uri as a source for a BitmapImage, and
then the BitmapImage as the source for the WriteableBitmap.  But that didn’t
work so well until I figured out the trick.  Here goes…
</p>
        <p>
First, here is the code that I <em>THOUGHT</em> would work, but did not.
</p>
        <pre class="code">
          <span style="color: #2b91af">Uri </span>uri = <span style="color: blue">new </span><span style="color: #2b91af">Uri</span>(<span style="color: #a31515">"http://somedomain.com/someimage.png"</span>); <span style="color: #2b91af">BitmapImage </span>bitmapImage
= <span style="color: blue">new </span><span style="color: #2b91af">BitmapImage</span>(uri); <span style="color: #2b91af">WriteableBitmap </span>writeableBitmap
= <span style="color: blue">new </span><span style="color: #2b91af">WriteableBitmap</span>(bitmapImage);</pre>
        <p>
          <a href="http://11011.net/software/vspaste">
          </a>When you run that, you’ll most likely
get an exception (I did) because the BitmapImage is not set when it is used for the
WriteableBitmap.  My hypothesis was that the BitmapImage doesn’t render itself
until it is needed.  I started looking through the properties of the BitmapImage,
luckily there aren’t too many, when I came across the CreateOptions property – that
sounded interesting!  It is an Enum with 3 values: DelayCreation, IgnoreImageCache,
and None.  Guess which one is set as the default?  DelayCreation! 
Here was my next pass at the code:
</p>
        <pre class="code">
          <span style="color: #2b91af">Uri </span>uri = <span style="color: blue">new </span><span style="color: #2b91af">Uri</span>(<span style="color: #a31515">"http://somedomain.com/someimage.png"</span>); <span style="color: #2b91af">BitmapImage </span>bitmapImage
= <span style="color: blue">new </span><span style="color: #2b91af">BitmapImage</span>();
bitmapImage.CreateOptions = <span style="color: #2b91af">BitmapCreateOptions</span>.None;
bitmapImage.UriSource = uri; <span style="color: #2b91af">WriteableBitmap </span>writeableBitmap
= <span style="color: blue">new </span><span style="color: #2b91af">WriteableBitmap</span>(bitmapImage);</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
That’s it, pretty simple and it works.  But I took it one step further just in
case.  I’m not positive this is necessary but I like to play it safe.  When
inspecting the properties of the BitmapImage, I saw there is an ImageOpened event.
I was thinking that some images may load slowly and I wasn’t sure if that happens
asynchronously or not.  Would my application wait around for them to finish loading? 
I don’t really know but it sure sounds like the ImageOpened event would be a way to
make sure my images were loaded.  Here is my final take:
</p>
        <pre class="code">
          <span style="color: blue">public void </span>SomeMethod() { <span style="color: #2b91af">Uri </span>uri
= <span style="color: blue">new </span><span style="color: #2b91af">Uri</span>(<span style="color: #a31515">"http://somedomain.com/someimage.png"</span>); <span style="color: #2b91af">BitmapImage </span>bitmapImage
= <span style="color: blue">new </span><span style="color: #2b91af">BitmapImage</span>();
bitmapImage.CreateOptions = <span style="color: #2b91af">BitmapCreateOptions</span>.None;
bitmapImage.ImageOpened += ImageOpened; bitmapImage.UriSource = uri; } <span style="color: blue">void </span>ImageOpened(<span style="color: blue">object </span>sender, <span style="color: #2b91af">RoutedEventArgs </span>e)
{ <span style="color: #2b91af">BitmapImage </span>bm = (<span style="color: #2b91af">BitmapImage</span>)sender; <span style="color: #2b91af">WriteableBitmap </span>wbm
= <span style="color: blue">new </span><span style="color: #2b91af">WriteableBitmap</span>(bm); <span style="color: green">//now
I can use wbm for whatever I need... </span>}</pre>
        <a href="http://11011.net/software/vspaste">
        </a>Good luck with your WriteableBitmaps!<img width="0" height="0" src="http://www.blog.ingenuitynow.net/aggbug.ashx?id=7e7870a9-c5bf-476c-bd94-fad3f0d3ae2a" /></div>
    </content>
  </entry>
  <entry>
    <title>Creating Custom Silverlight 4 Controls That Support Command Binding</title>
    <link rel="alternate" type="text/html" href="http://www.blog.ingenuitynow.net/Creating+Custom+Silverlight+4+Controls+That+Support+Command+Binding.aspx" />
    <id>http://www.blog.ingenuitynow.net/PermaLink,guid,e5da246b-41c7-42b7-9b14-ac9cca84f622.aspx</id>
    <published>2010-08-04T22:05:17.5218709-04:00</published>
    <updated>2010-08-04T22:05:17.5218709-04:00</updated>
    <category term=".Net" label=".Net" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,.Net.aspx" />
    <category term="MVVM" label="MVVM" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,MVVM.aspx" />
    <category term="Silverlight" label="Silverlight" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,Silverlight.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
I’m a big fan of the MVVM Pattern for Silverlight development, so naturally I was
excited about Silverlight 4 having support for Commanding.  I recently upgraded
my current project to Silverlight 4 and I found that binding my buttons to my View
Model commands was pretty easy.  However, it wasn’t long before I needed to create
a custom control and I wasn’t sure how the Command Binding would work.  I searched
the web for some samples but found none so I was on my own.  It wasn’t too hard
to figure out the solution, here is how I did it.
</p>
        <p>
The setup… For the sake of this demo, I’ll need a resuable control that combines 2
buttons:  Save and Cancel.  I’ll call it a SaveCancel control :). 
The control is simple enough.  Thanks to Silverlight 4, buttons already support
the command binding – all you have to do is bind the command in your View Model to
the Command property of the button (and optionally the CommandParameter property). 
If you aren’t familiar with the basics of commanding, I suggest you check out this <a href="http://johnpapa.net/silverlight/5-simple-steps-to-commanding-in-silverlight/" target="_blank">blog
post by John Papa</a>.  Anyway, when it is time to use the SaveCancel control
on another page/control, it becomes pretty clear that you can’t access the individual
button’s properties to bind to them!  The individual buttons are not publicly
available since they are “child” controls of the SaveCancel control.  The solution: 
The Command and CommandParameter properties of each button are just DependencyProperties
so all you have to do is create matching properties on the new control that “wrap”
the internal properties.
</p>
        <h5>Here’s how it’s done…
</h5>
        <p>
First, the XAML of the SaveCancel control is pretty straightforward:
</p>
        <pre class="code">
          <span style="color: blue">&lt;</span>
          <span style="color: #a31515">UserControl </span>
          <span style="color: red">x</span>
          <span style="color: blue">:</span>
          <span style="color: red">Class</span>
          <span style="color: blue">="CustomControlWithCommanding.SaveCancel" </span>
          <span style="color: red">xmlns</span>
          <span style="color: blue">="http://schemas.microsoft.com/winfx/2006/xaml/presentation" </span>
          <span style="color: red">xmlns</span>
          <span style="color: blue">:</span>
          <span style="color: red">x</span>
          <span style="color: blue">="http://schemas.microsoft.com/winfx/2006/xaml" </span>
          <span style="color: red">xmlns</span>
          <span style="color: blue">:</span>
          <span style="color: red">d</span>
          <span style="color: blue">="http://schemas.microsoft.com/expression/blend/2008" </span>
          <span style="color: red">xmlns</span>
          <span style="color: blue">:</span>
          <span style="color: red">mc</span>
          <span style="color: blue">="http://schemas.openxmlformats.org/markup-compatibility/2006" </span>
          <span style="color: red">mc</span>
          <span style="color: blue">:</span>
          <span style="color: red">Ignorable</span>
          <span style="color: blue">="d"
&gt; &lt;</span>
          <span style="color: #a31515">StackPanel </span>
          <span style="color: red">x</span>
          <span style="color: blue">:</span>
          <span style="color: red">Name</span>
          <span style="color: blue">="LayoutRoot" </span>
          <span style="color: red">Orientation</span>
          <span style="color: blue">="Horizontal"
&gt; &lt;</span>
          <span style="color: #a31515">Button </span>
          <span style="color: red">x</span>
          <span style="color: blue">:</span>
          <span style="color: red">Name</span>
          <span style="color: blue">="Save" </span>
          <span style="color: red">Width</span>
          <span style="color: blue">="50" </span>
          <span style="color: red">Height</span>
          <span style="color: blue">="25" </span>
          <span style="color: red">Content</span>
          <span style="color: blue">="Save" </span>
          <span style="color: red">Margin</span>
          <span style="color: blue">="0,0,5,0"
/&gt; &lt;</span>
          <span style="color: #a31515">Button </span>
          <span style="color: red">x</span>
          <span style="color: blue">:</span>
          <span style="color: red">Name</span>
          <span style="color: blue">="Cancel" </span>
          <span style="color: red">Width</span>
          <span style="color: blue">="50" </span>
          <span style="color: red">Height</span>
          <span style="color: blue">="25" </span>
          <span style="color: red">Content</span>
          <span style="color: blue">="Cancel"
/&gt; &lt;/</span>
          <span style="color: #a31515">StackPanel</span>
          <span style="color: blue">&gt;
&lt;/</span>
          <span style="color: #a31515">UserControl</span>
          <span style="color: blue">&gt; </span>
        </pre>
        <p>
Next the Code Behind of the SaveCancel control.  Here is where all of the wiring
is done for each button’s commands. There are a few parts.  First is the DependencyProperty
for the Save button’s command.  Like any dependency property, you need a public
property with a Get/Set that sets the value of the Dependency Property as well as
a DependencyProperty:
</p>
        <pre class="code">
          <span style="color: blue">public </span>
          <span style="color: #2b91af">ICommand </span>SaveCommand
{ <span style="color: blue">get </span>{ <span style="color: blue">return </span>(<span style="color: #2b91af">ICommand</span>)GetValue(SaveCommandProperty);
} <span style="color: blue">set </span>{ SetValue(SaveCommandProperty, <span style="color: blue">value</span>);
} } <span style="color: blue">public static readonly </span><span style="color: #2b91af">DependencyProperty </span>SaveCommandProperty
= <span style="color: #2b91af">DependencyProperty</span>.Register(<span style="color: #a31515">"SaveCommand"</span>, <span style="color: blue">typeof</span>(<span style="color: #2b91af">ICommand</span>), <span style="color: blue">typeof</span>(<span style="color: #2b91af">SaveCancel</span>), <span style="color: blue">new </span><span style="color: #2b91af">PropertyMetadata</span>(<span style="color: blue">null</span>,
OnSaveCommandChanged));</pre>
        <p>
          <a href="http://11011.net/software/vspaste">
          </a>Note that in the PropertyMetaData
argument for the SaveCommandProperty we are passing in a callback reference (OnSaveCommandChanged). 
We’ll use that method to set the value of the new DependencyProperty to the actual
Save button.  Here is the code:
</p>
        <pre class="code">
          <span style="color: blue">private static void </span>OnSaveCommandChanged(<span style="color: #2b91af">DependencyObject </span>d, <span style="color: #2b91af">DependencyPropertyChangedEventArgs </span>e)
{ <span style="color: #2b91af">SaveCancel </span>sc = (<span style="color: #2b91af">SaveCancel</span>)d;
sc.Save.Command = (<span style="color: #2b91af">ICommand</span>)e.NewValue; }</pre>
        <p>
To be complete, we’ll do some similar work to make sure that we can wire up the CommandParameter
to the Save button as well, even though we don’t really need it to make the button
work for this sample.   
</p>
        <pre class="code">
          <span style="color: blue">public object </span>SaveCommandParameter
{ <span style="color: blue">get </span>{ <span style="color: blue">return </span>GetValue(SaveCommandParameterProperty);
} <span style="color: blue">set </span>{ SetValue(SaveCommandParameterProperty, <span style="color: blue">value</span>);
} } <span style="color: blue">public static readonly </span><span style="color: #2b91af">DependencyProperty </span>SaveCommandParameterProperty
= <span style="color: #2b91af">DependencyProperty</span>.Register(<span style="color: #a31515">"SaveCommandParameter"</span>, <span style="color: blue">typeof</span>(<span style="color: blue">object</span>), <span style="color: blue">typeof</span>(<span style="color: #2b91af">SaveCancel</span>), <span style="color: blue">new </span><span style="color: #2b91af">PropertyMetadata</span>(<span style="color: blue">null</span>,
OnSaveCommandParameterChanged)); <span style="color: blue">private static void </span>OnSaveCommandParameterChanged(<span style="color: #2b91af">DependencyObject </span>d, <span style="color: #2b91af">DependencyPropertyChangedEventArgs </span>e)
{ <span style="color: #2b91af">SaveCancel </span>sc = (<span style="color: #2b91af">SaveCancel</span>)d;
sc.Save.CommandParameter = e.NewValue; }</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
To use the new SaveCancel control in another Silverlight control, all I need is some
code like the sample below.  It works perfectly with no events wired up and no
code behind on the page where it is used!
</p>
        <pre class="code">
          <span style="color: blue">&lt;</span>
          <span style="color: #a31515">CustomControlWithCommanding</span>
          <span style="color: blue">:</span>
          <span style="color: #a31515">SaveCancel </span>
          <span style="color: red">SaveCommand</span>
          <span style="color: blue">="{</span>
          <span style="color: #a31515">Binding </span>
          <span style="color: red">SaveCommand</span>
          <span style="color: blue">}" </span>
          <span style="color: red">CancelCommand</span>
          <span style="color: blue">="{</span>
          <span style="color: #a31515">Binding </span>
          <span style="color: red">CancelCommand</span>
          <span style="color: blue">}"
/&gt; </span>
        </pre>
        <p>
Of course, my ViewModel must have the commands (SaveCommand and CancelCommand) to
support this as well.  But that is just normal ViewModel commanding.  Again,
if you aren’t familiar with that, check out <a href="http://johnpapa.net/silverlight/5-simple-steps-to-commanding-in-silverlight/" target="_blank">John
Papa’s blog post</a>.
</p>
        <h5>Complete Source Code: 
</h5>
        <p>
To see the complete sample solution, <a href="http://www.blog.ingenuitynow.net/content/binary/CustomControlWithCommanding.zip" target="_blank">download
it here</a>.
</p>
        <img width="0" height="0" src="http://www.blog.ingenuitynow.net/aggbug.ashx?id=e5da246b-41c7-42b7-9b14-ac9cca84f622" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Work-around for VS 2010 &amp;amp; Silverlight Add Reference Hint Path Bug</title>
    <link rel="alternate" type="text/html" href="http://www.blog.ingenuitynow.net/Workaround+For+VS+2010+Amp+Silverlight+Add+Reference+Hint+Path+Bug.aspx" />
    <id>http://www.blog.ingenuitynow.net/PermaLink,guid,59296bb8-87e5-469a-8b28-cc7d05475b31.aspx</id>
    <published>2010-07-25T15:19:34.395137-04:00</published>
    <updated>2010-07-25T15:19:34.395137-04:00</updated>
    <category term="Silverlight" label="Silverlight" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,Silverlight.aspx" />
    <category term="Visual Studio" label="Visual Studio" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,Visual%2BStudio.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
There seems to be a bug in Visual Studio 2010 when adding references to Silverlight
projects.  You can add “regular” .Net references just fine using the add reference
dialog.  However if you use the browse feature to find a reference in an “unusual”
location, Visual Studio is supposed to record that location along with the reference
in the .csproj file.  It includes the location via the HintPath property.
</p>
        <p>
At my company, we use build machines and we don’t like to install a lot of software
on them.  So in source control we have a folder (assets\lib) that includes all
of the necessary .dll files that we need to build or projects.  When we add references
to our project, like for a Silverlight control library, we add it from the assets\lib\Silverlight
folder, even though that library is installed on my local machine.  Anyway, when
I tried doing this in Visual Studio 2010 (I’m upgrading our existing 2008 solution),
VS 2010 isn’t including the HintPath.  If you aren’t careful, you may not realize
this is happening.  That’s because since the library is installed on my machine,
the build works just fine, except it pulls the .dll from the wrong place.  If
I were to try this build from my build server, it would fail though.  
</p>
        <p>
Anyway, here is the workaround.  It’s a pain but it works…
</p>
        <ol>
          <li>
Add your references as you would normally do it via Visual Studio. 
</li>
          <li>
Right click the project file in the Solution Explorer and choose Unload Project. 
</li>
          <li>
Once the project is unloaded, right click again and chose edit.  This will open
the file, and xml document, in Visual Studio. 
</li>
          <li>
Find the incorrect reference in the file and edit it manually.  You’ll need to
make it look something like this: <pre class="code"><span style="color: blue">&lt;</span><span style="color: #a31515">Reference </span><span style="color: red">Include</span><span style="color: blue">=</span>"<span style="color: blue">System.Windows.Controls,
Version=2.0.5.0, Culture=neutral, 
<br />
PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL</span>" <span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">HintPath</span><span style="color: blue">&gt;</span>..\..\..\Assets\lib\Silverlight\System.Windows.Controls.dll<span style="color: blue">&lt;/</span><span style="color: #a31515">HintPath</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">Reference</span><span style="color: blue">&gt; </span></pre><a href="http://11011.net/software/vspaste"></a></li>
          <li>
Save the file and close it. 
</li>
          <li>
Reload the project. 
</li>
        </ol>
        <p>
When you are done you can verify that it works.  Here’s how…
</p>
        <ol>
          <li>
In Visual Studio, choose Tools &gt; Options. 
</li>
          <li>
Find the Projects and Solutions section and select Build and Run. 
</li>
          <li>
In the section “MsBuild Project build output verbosity”, select “Detailed”. 
</li>
          <li>
Now build your project.  When you do so, you’ll see a lot of details of what
goes on in the Output Window.  You’ll need to find the output that relates to
the file that you specified, in my case it is System.Windows.Controls and the important
output looks like this: <blockquote><p><em>Primary reference "System.Windows.Controls, Version=2.0.5.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35". 
<br />
      Resolved file path is "C:\Projects\MC\BackOffice\UI\branches\2010Migration\Assets\lib\Silverlight\System.Windows.Controls.dll". 
<br />
      Reference found at search path location "{HintPathFromItem}".</em></p></blockquote></li>
          <li>
You will note that it tells you specifically which path was used to resolve the reference! 
And if you are so lazy that you don’t want to read the entire path used, you see that
it says the reference was found at the search path location “{HintPathFromItem}”. 
That means it used the path you specified.</li>
        </ol>
        <p>
I hope this helps!  Good luck and let’s hope they fix this bug soon.
</p>
        <img width="0" height="0" src="http://www.blog.ingenuitynow.net/aggbug.ashx?id=59296bb8-87e5-469a-8b28-cc7d05475b31" />
      </div>
    </content>
  </entry>
</feed>
