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

<channel>
	<title>Designer Silverlight &#187; Tutorial</title>
	<atom:link href="http://www.designersilverlight.com/category/tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.designersilverlight.com</link>
	<description>Matthias Shapiro's Silverlight &#38; WPF Blog - Because Developers Get All The Good Blogs</description>
	<lastBuildDate>Thu, 26 Jan 2012 06:45:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Tutorial: Logging Into Facebook with Windows Phone 7 (Silverlight)</title>
		<link>http://www.designersilverlight.com/2012/01/25/tutorial-logging-into-facebook-with-windows-phone-7-silverlight/</link>
		<comments>http://www.designersilverlight.com/2012/01/25/tutorial-logging-into-facebook-with-windows-phone-7-silverlight/#comments</comments>
		<pubDate>Thu, 26 Jan 2012 06:43:12 +0000</pubDate>
		<dc:creator>matthias</dc:creator>
				<category><![CDATA[Facebook]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Windows Phone 7]]></category>
		<category><![CDATA[Blend]]></category>
		<category><![CDATA[Windows Phone 7.1]]></category>

		<guid isPermaLink="false">http://www.designersilverlight.com/?p=1361</guid>
		<description><![CDATA[So, you want to use Facebook to sign into something or in some way integrate Facebook with your Windows Phone 7 app. You are in luck because it is almost hilariously easy. I’ve uploaded this tutorial as a zip file. I tried to github it, but I’m inching up on 1 hour of trying to [...]]]></description>
			<content:encoded><![CDATA[<p>So, you want to use Facebook to sign into something or in some way integrate Facebook with your Windows Phone 7 app. You are in luck because it is almost hilariously easy.</p>
<p>I’ve uploaded this tutorial as a zip file. I tried to github it, but I’m inching up on 1 hour of trying to figure out what the hell I’m doing wrong.</p>
<p><a href='http://www.designersilverlight.com/wp-content/uploads/2012/01/Facebook_Tutorial_App.zip'>Download this Tutorial Project</a></p>
<p>I’ve tried to make this a full featured tutorial, so if you just want to get to the code, head down to the “Let the User Login To Facebook” section.</p>
<h2>Get Your Facebook Key</h2>
<p>Go to <a href="http://developers.facebook.com">http://developers.facebook.com</a> and log in with your Facebook account. Give them the permissions they need and go to the “Apps” tab. Click on “Create new App”</p>
<p>Fill in the App Display Name and the App Namespace and do the verification. Then you’ll get to a page that gives you your App ID and App Secret. </p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2012/01/image.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.designersilverlight.com/wp-content/uploads/2012/01/image_thumb.png" width="758" height="229"></a></p>
<p>Don’t worry about anything else here… you don’t need to select anything in the “how your app integrates with Facebook” section to do this tutorial. If you’re using Facebook integration to do some basic login and simple posting, you could probably just use the Website option.</p>
<p>Also, click the “Graph API Explorer” on the left and keep that open. We’ll come back to that in a moment.</p>
<h2>Add Facebook C# SDK</h2>
<p><a href="http://facebooksdk.codeplex.com/">Download the Facebook C# SDK</a> and extract it. Go to the “sl3-wp” folder and unblock the Facebook.dll file (right-click =&gt; Properties=&gt;click “Unblock”)</p>
<p>Open up your project (for the example, I used the Visual Studio 10 “Windows Phone Databound Application” template) and right-click on “References =&gt; Add References…”. Click “Browse” and navigate to the Facebook.dll and add it to your project.</p>
<h2>Build Facebook Login UI</h2>
<p>We’ll do this quick here (no MVVM, no bindings) but in later versions I’ll integrate this into a more formal project.</p>
<p>Open your project in Blend. For this quick-and-dirty tutorial, we’ll just add another page title and another panel for the UI we want to show when the user is logged in and set the Visibility to Collapsed on those. Our visual tree should look like this.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2012/01/image1.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.designersilverlight.com/wp-content/uploads/2012/01/image_thumb1.png" width="154" height="168"></a></p>
<p>In our loginPanel, we’re going to add a button and a WebBrowser to our panel. Set the button (containing “sign in using facebook” in the content) to the top and the WebBrowser to fill the panel. For a little flare, I’m going to add “ShowFacebookLogin” and “HideFacebookLogin” animations. My login screen now looks like this (the WebBrowser will animate in when we press the button).</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2012/01/image2.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.designersilverlight.com/wp-content/uploads/2012/01/image_thumb2.png" width="244" height="128"></a></p>
<p>When we get our data back, we’re going to tell the user it worked by showing their name and their Facebook avatar. So we’ll add a Grid with an Image and a TextBlock to display the user, along with a friendly “Hello”. (Make sure to name all these things so that we can update them from the code.)</p>
<p>OK… out UI is simple, but ready to roll. Now let’s do something when the user clicks the sign-in button. Go to the “Click” event and add a method like “StartFacebookLogin”.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2012/01/image3.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.designersilverlight.com/wp-content/uploads/2012/01/image_thumb3.png" width="345" height="161"></a></p>
<h2>Let the User Login to Facebook</h2>
<p>Add “using Facebook;” to your references at the top of your MainPage.xaml.cs file. Now, add the following properties:</p>
<pre class="csharpcode"><span class="kwrd">private</span> FacebookClient _asyncFbClient;
<span class="kwrd">private</span> <span class="kwrd">string</span> _appID = <span class="str">"get from your facebook app page"</span>;
<span class="kwrd">private</span> <span class="kwrd">string</span> _appSecret = <span class="str">"get from your facebook app page"</span>;</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<p>Now go to your StartFacebookLogin method and add the following (explained at the end).</p>
<pre class="csharpcode"><span class="kwrd">private</span> <span class="kwrd">void</span> StartFacebookLogin(<span class="kwrd">object</span> sender, RoutedEventArgs e)
{
    <span class="kwrd">string</span>[] extendedPermissions = <span class="kwrd">new</span>[] { <span class="str">"user_about_me"</span>, <span class="str">"publish_stream"</span> };

    var oauth = <span class="kwrd">new</span> FacebookOAuthClient { AppId = _appID, AppSecret = _appSecret };
    var parameters = <span class="kwrd">new</span> Dictionary&lt;<span class="kwrd">string</span>, <span class="kwrd">object</span>&gt;
                    {
                        {<span class="str">"response_type"</span>, <span class="str">"token"</span>},
                        {<span class="str">"display"</span>, <span class="str">"touch"</span>}
                    };
    <span class="kwrd">if</span> (extendedPermissions != <span class="kwrd">null</span> &amp;&amp; extendedPermissions.Length &gt; 0)
    {
        var scope = <span class="kwrd">new</span> StringBuilder();
        scope.Append(<span class="kwrd">string</span>.Join(<span class="str">","</span>, extendedPermissions));
        parameters[<span class="str">"scope"</span>] = scope.ToString();
    }

    var loginUrl = oauth.GetLoginUrl(parameters);</pre>
<pre class="csharpcode">    webBrowser.Navigated += <span class="kwrd">new</span> EventHandler&lt;NavigationEventArgs&gt;(CheckForAuth);
    webBrowser.Navigate(loginUrl);
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<p>Remember how I told you to keep the Graph API open? Here is why. It has a big list of all the extendedPermissions that you can tell Facebook you want from the user. Here, we’ve asked for basic user information and the ability to publish to the user’s stream. Ask for only the permissions you need. The user can see the details of your permission request and may reject it if you ask too much.</p>
<p>Next, we create our OAuth client using out AppID and AppSecret and create a Dictionary of parameters communicating to Facebook the details of our request (for example, setting “display”, “touch” tells Facebook that we want the mobile interface. </p>
<p>We write out permission request into our parameters and then get a login url, which we will direct to our webBrowser. Here is what we will should get.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2012/01/image4.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.designersilverlight.com/wp-content/uploads/2012/01/image_thumb4.png" width="204" height="337"></a></p>
<p>At the point Facebook walks the user through their login and, when they are done, it hands us back the access token we’ll need to get the user info and let the user make posts through our app. </p>
<p>This is why we attached the CheckForAuth event handler to our webBrowser. When we navigate to a new page, we’ll check to see if we got an access token using this code:</p>
<pre class="csharpcode"><span class="kwrd">private</span> <span class="kwrd">void</span> CheckForAuth(<span class="kwrd">object</span> sender, System.Windows.Navigation.NavigationEventArgs e)
{
    FacebookOAuthResult result;
    <span class="kwrd">if</span> (FacebookOAuthResult.TryParse(e.Uri, <span class="kwrd">out</span> result))
    {
        <span class="kwrd">if</span> (result.IsSuccess)
        {
            IsolatedStorageSettings Settings = IsolatedStorageSettings.ApplicationSettings;
            <span class="kwrd">if</span>(Settings.Contains(<span class="str">"MyFacebookAccessToken"</span>))
                Settings[<span class="str">"MyFacebookAccessToken"</span>] = result.AccessToken;
            <span class="kwrd">else</span>
                Settings.Add(<span class="str">"MyFacebookAccessToken"</span>, result.AccessToken);
            Settings.Save();
            _asyncFbClient = <span class="kwrd">new</span> FacebookClient(result.AccessToken);
            _asyncFbClient.GetCompleted += <span class="kwrd">new</span> EventHandler&lt;FacebookApiEventArgs&gt;(_asyncFbClient_GetCompleted);
            _asyncFbClient.GetAsync(<span class="str">"/me"</span>);
        }
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<p>If the Uri does contain a Facebook OAuth result and it is a success, we save the access token to our settings and then immediately use it to get the most basic user information. </p>
<p>The problem we have right now is that we have no class to structure the data that comes back. So get that all squared away.</p>
<p>First, right-click on “References” and select “Add Reference…”. Select “System.Runtime.Serialization”.</p>
<p>Next, add a class to your project (I added mine in a folder called “Models”) and name it FacebookUser.cs.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2012/01/image5.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.designersilverlight.com/wp-content/uploads/2012/01/image_thumb5.png" width="224" height="125"></a></p>
<p>In that class, add the following code</p>
<pre class="csharpcode">[DataContractAttribute]
<span class="kwrd">public</span> <span class="kwrd">class</span> FacebookUser
{
    <span class="kwrd">public</span> FacebookUser() { }

    <span class="kwrd">private</span> <span class="kwrd">string</span> _id;
    [DataMember(Name = <span class="str">"id"</span>)]
    <span class="kwrd">public</span> <span class="kwrd">string</span> ID
    {
        get { <span class="kwrd">return</span> _id; }
        set { _id = <span class="kwrd">value</span>; }
    }

    <span class="kwrd">private</span> <span class="kwrd">string</span> _name;
    [DataMember(Name = <span class="str">"name"</span>)]
    <span class="kwrd">public</span> <span class="kwrd">string</span> Name
    {
        get { <span class="kwrd">return</span> _name; }
        set { _name = <span class="kwrd">value</span>; }
    }
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<p>The sample project has a much larger (although still incomplete) version of this model. But this one will do for our purposes.</p>
<p>We’ll make our “GetCompleted” event handler (remember that?) look like this:</p>
<pre class="csharpcode"><span class="kwrd">void</span> _asyncFbClient_GetCompleted(<span class="kwrd">object</span> sender, FacebookApiEventArgs e)
{
    FacebookUser _fbUser = e.GetResultData&lt;FacebookUser&gt;();

    Deployment.Current.Dispatcher.BeginInvoke(() =&gt;
    {
        fbName.Text = _fbUser.Name;
        BitmapImage bi = <span class="kwrd">new</span> BitmapImage(<span class="kwrd">new</span> Uri(<span class="str">"https://graph.facebook.com/"</span> + _fbUser.ID + <span class="str">"/picture"</span>));
        fbAvatar.Source = bi;
        HideFacebookLogin.Begin();
    });
    _asyncFbClient.GetCompleted -= _asyncFbClient_GetCompleted;
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
<p>What we’ve done here is shove our data into a FacebookUser model. Then we use BeginInvoke to bring ourselves back to the UI thread and set all the properties we want. </p>
<p>Finally, we start the animation that hides the login data and shows that our Facebook login was a success. </p>
<p>Boom!</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2012/01/image6.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.designersilverlight.com/wp-content/uploads/2012/01/image_thumb6.png" width="244" height="168"></a></p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http://www.designersilverlight.com/2012/01/25/tutorial-logging-into-facebook-with-windows-phone-7-silverlight/&amp;layout=standard&amp;show_faces=1&amp;width=450&amp;action=like&amp;colorscheme=light&amp;font=" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:25px"></iframe><div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Tutorial%3A+Logging+Into+Facebook+with+Windows+Phone+7+%28Silverlight%29+http%3A%2F%2Fdesignersilverlight.com%2F%3Fp%3D1361" title="Post to Twitter"><img class="nothumb" src="http://www.designersilverlight.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter.png" alt="Post to Twitter" /></a> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Tutorial%3A+Logging+Into+Facebook+with+Windows+Phone+7+%28Silverlight%29+http%3A%2F%2Fdesignersilverlight.com%2F%3Fp%3D1361" title="Post to Twitter">Tweet This Post</a></p></div>]]></content:encoded>
			<wfw:commentRss>http://www.designersilverlight.com/2012/01/25/tutorial-logging-into-facebook-with-windows-phone-7-silverlight/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Windows Phone 7 Push Notification For Beginners&#8211;The Device</title>
		<link>http://www.designersilverlight.com/2011/06/28/windows-phone-7-push-notification-for-beginnersthe-device/</link>
		<comments>http://www.designersilverlight.com/2011/06/28/windows-phone-7-push-notification-for-beginnersthe-device/#comments</comments>
		<pubDate>Tue, 28 Jun 2011 14:56:14 +0000</pubDate>
		<dc:creator>matthias</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Windows Phone 7]]></category>
		<category><![CDATA[push notifications]]></category>
		<category><![CDATA[WP7]]></category>

		<guid isPermaLink="false">http://www.designersilverlight.com/2011/06/28/windows-phone-7-push-notification-for-beginnersthe-device/</guid>
		<description><![CDATA[OK… it’s been almost 2 months since I tried to start this series and now I’m going to finish it. A brief warning: this is done with pre-Mango bits so it won’t have all the latest, greatest most exciting things like the back-tile functionality or deep linking. (Here’s a good blog post on all that.) [...]]]></description>
			<content:encoded><![CDATA[<p>OK… it’s been almost 2 months since I tried to start this series and now I’m going to finish it. A brief warning: this is done with pre-Mango bits so it won’t have all the latest, greatest most exciting things like the back-tile functionality or deep linking. (<a href="http://blogs.microsoft.co.il/blogs/alex_golesh/archive/2011/05/24/windows-phone-mango-what-s-new-push-notifications-amp-tiles-part-8-of-8.aspx">Here’s a good blog post on all that</a>.) But it remains a good introduction.</p>
<p><a href="https://github.com/matthiasxc/Windows-Phone-7-Push-Notification-Sample-All/">Windows Phone 7 Push Notification Project Files (github)</a></p>
<p>The goal of this post is to provide a sample Push Notification application that is</p>
<ul>
<li>easy to test</li>
<li>good architecture</li>
<li><a href="http://www.designersilverlight.com/WP7PushTester/">provides a sample web app for testing your push code</a> (part of the delay has been because I’ve been trying to be a perfectionist on that web app. I’ve decided I’m never going to get around to making it perfect so this is the best I can do with the time I have)</li>
</ul>
<p>Too many of the sample apps I’ve seen shove the push notification code in the xaml code-behind, leaving it to us to separate out the bits and translate it into a real project. This project uses MVVMLight binding and an event-driven architecture to get the push stuff working.</p>
<p>So… let’s dig into it.</p>
<p>I created a class called PushNotificationService that held a number of helpful little variables and objects. The most important variable here is</p>
<pre class="csharpcode"><span class="kwrd">private</span> <span class="kwrd">static</span> <span class="kwrd">string</span> _pushChannelName = <span class="str">"SampleAppNotification"</span>;</pre>
<p><!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->This name will delineate our push service from other services. If something weird happens and your push service stops working, the first thing you should do is change this string.</p>
<p>Next, we have a method that allows us to control when we get the push notification Uri. As an overview of what we’re doing,</p>
<ol>
<li>See if we already have a valid HttpNotificationChannel (the object that will control the push permissions and handle information from the Microsoft Push server)</li>
<li>If we do have a valid HttpNotificationChannel, make sure we got the channel Uri correctly (the uri we will send out push notifications to). If we did, SUCCESS! Otherwise, go to step 3.</li>
<li>Set the event handlers so we can handle when we get a new channel Uri back from the Microsoft Push server.</li>
<li>Ask the Microsoft Push server for a new channel Uri.</li>
<li>Set the HttpNotificationChannel up to handle the right kind of push notifications.</li>
<li>Handle the returned uri from the Microsoft Push server &amp; send it to the rest of the app.</li>
</ol>
<h2>Step 1</h2>
<pre class="csharpcode">_notificationChannel = HttpNotificationChannel.Find(_pushChannelName);</pre>
<p><!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } --></p>
<h2>Step 2</h2>
<pre class="csharpcode"><span class="kwrd">if</span> (_notificationChannel.ChannelUri != <span class="kwrd">null</span>)
{
      <span class="rem">// Success! We run the event to send that info back to our ViewModel </span>
      RaiseGotPushUri(_notificationChannel.ChannelUri);
}</pre>
<p><!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } --></p>
<h2>Step 3</h2>
<pre class="csharpcode"><span class="kwrd">else</span>
{
    _notificationChannel = <span class="kwrd">new</span> HttpNotificationChannel(_pushChannelName);
    _notificationChannel.ChannelUriUpdated +=
<span class="kwrd">         new</span> EventHandler&lt;NotificationChannelUriEventArgs&gt;(_notificationChannel_ChannelUriUpdated);
    _notificationChannel.HttpNotificationReceived +=
<span class="kwrd">         new</span> EventHandler&lt;HttpNotificationEventArgs&gt;(_notificationChannel_HttpNotificationReceived);
    _notificationChannel.ErrorOccurred +=
<span class="kwrd">         new</span> EventHandler&lt;NotificationChannelErrorEventArgs&gt;(_notificationChannel_ErrorOccurred);</pre>
<pre class="csharpcode">
<pre>}</pre>
</pre>
<h2>Step 4</h2>
<pre class="csharpcode">_notificationChannel.Open();</pre>
<p><!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } --></p>
<h2>Step 5</h2>
<pre class="csharpcode">BindNotifications(_notificationChannel);</pre>
<h2>Binding Details</h2>
<p>In the pre-Mango push notifications, there are 3 kinds of push notifications and we have to announce what kind of notifications we plan on handling.<br />
First, there are toast notifications, which look like this (<a href="http://wildermuth.com/2011/01/02/Architecting_WP7_-_Part_8_of_10_Toast_Push_Notifications">stolen from Shawn Wildermuth</a>):<br />
<!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } --><a href="http://www.designersilverlight.com/wp-content/uploads/2011/06/image.png"><img style="background-image: none; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;" title="image" src="http://www.designersilverlight.com/wp-content/uploads/2011/06/image_thumb.png" border="0" alt="image" width="244" height="58" /></a></p>
<p>Toast notifications consist of</p>
<ul>
<li>a title (“Toast Message” in the example above)</li>
<li>a message (“This is from the server”)</li>
</ul>
<p>To bind them, we simply type:</p>
<pre class="csharpcode">_thisChannel.BindToShellToast();</pre>
<p><!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->Tile notifications will only show up if the user has pinned the application to the front application tile board on the phone. I like to split Tile notifications into two types because we have to handle them differently in the code.</p>
<p>The first kind of tile notification is what I call “simple” Tile notifications. With simple Tile notifications, we can change a number and title on our app tile. The new title will show up where the title always shows up (at the bottom left) and the number will show up in a black circle at the top right.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2011/06/image1.png"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;" title="image" src="http://www.designersilverlight.com/wp-content/uploads/2011/06/image_thumb1.png" border="0" alt="image" width="115" height="117" /></a></p>
<p>Doing this much is simple:</p>
<pre class="csharpcode">_thisChannel.BindToShellTile();</pre>
<p><!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->But then there are Live Tiles, which allow us to send a whole new image to the tile to change it in a more holistic way to represent some push data. (We can still update numbers and titles too.) Live Tiles, however, require that we whitelist all the websites that might want to send an image to our phone. So we need to create a collection of whitelisted domains and add that to our tile binding.</p>
<pre class="csharpcode">System.Collections.ObjectModel.Collection&lt;Uri&gt; permittedImageHosts = <span class="kwrd">new</span> System.Collections.ObjectModel.Collection&lt;Uri&gt;();
permittedImageHosts.Add(<span class="kwrd">new</span> Uri(<span class="str">"http://www.designersilverlight.com"</span>));
_thisChannel.BindToShellTile(permittedImageHosts);</pre>
<p><!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->If you’re going to use my handy-dandy WP7 Push Tester, make sure you whitelist the exact domain above.</p>
<p>And that’s it… that’s all we need to do to get everything bound and ready to go! All we need now is to get the Uri from the Microsoft Push server so we know where to send our Push Notifications to.</p>
<h2>Step 6</h2>
<pre class="csharpcode"><span class="kwrd">void</span> _notificationChannel_ChannelUriUpdated(<span class="kwrd">object</span> sender, NotificationChannelUriEventArgs e)
{
      RaiseGotPushUri(e.ChannelUri);
}</pre>
<p><!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } -->This will run when we get the updated Uri back and we just send it in our event, which the ViewModel is listening to.</p>
<p>I’ve really streamlined the code in this blog post to deal with just the bare essentials. If you want to look at the project in detail, <a href="https://github.com/matthiasxc/Windows-Phone-7-Push-Notification-Sample-All/">you can download it here</a>.</p>
<p>Now, all we have to do is start testing it.</p>
<p>You can test it using my WP7 Push Notification web app. I’m also working on another post that walks through creating a push notification system using PHP, but in the meantime you might want to <a href="http://www.daveamenta.com/2010-11/send-push-notifications-to-windows-phone-7-from-php/">check out this PHP library that I used</a>.</p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http://www.designersilverlight.com/2011/06/28/windows-phone-7-push-notification-for-beginnersthe-device/&amp;layout=standard&amp;show_faces=1&amp;width=450&amp;action=like&amp;colorscheme=light&amp;font=" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:25px"></iframe><div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Windows+Phone+7+Push+Notification+For+Beginners%E2%80%93The+Device+http%3A%2F%2Fdesignersilverlight.com%2F%3Fp%3D1329" title="Post to Twitter"><img class="nothumb" src="http://www.designersilverlight.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter.png" alt="Post to Twitter" /></a> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Windows+Phone+7+Push+Notification+For+Beginners%E2%80%93The+Device+http%3A%2F%2Fdesignersilverlight.com%2F%3Fp%3D1329" title="Post to Twitter">Tweet This Post</a></p></div>]]></content:encoded>
			<wfw:commentRss>http://www.designersilverlight.com/2011/06/28/windows-phone-7-push-notification-for-beginnersthe-device/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Using WrapPanel and DockPanel in Windows Phone 7 With Blend</title>
		<link>http://www.designersilverlight.com/2010/06/28/using-wrappanel-and-dockpanel-in-windows-phone-7-with-blend/</link>
		<comments>http://www.designersilverlight.com/2010/06/28/using-wrappanel-and-dockpanel-in-windows-phone-7-with-blend/#comments</comments>
		<pubDate>Tue, 29 Jun 2010 04:30:59 +0000</pubDate>
		<dc:creator>Matthias Shapiro</dc:creator>
				<category><![CDATA[Blend]]></category>
		<category><![CDATA[Silverlight 3]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Windows Phone 7]]></category>
		<category><![CDATA[XAML]]></category>
		<category><![CDATA[DockPanel]]></category>
		<category><![CDATA[Panels]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[source code]]></category>
		<category><![CDATA[Windows Mobile]]></category>
		<category><![CDATA[WrapPanel]]></category>

		<guid isPermaLink="false">http://www.designersilverlight.com/2010/06/28/using-wrappanel-and-dockpanel-in-windows-phone-7-with-blend/</guid>
		<description><![CDATA[I’m currently working on porting the run-away hit mobile application ShopSavvy to Windows Phone 7 (sign up to be a beta tester) and one of the things I really wished I had was the WrapPanel. (For those who are new to Silverlight/WPF/Windows Phone 7, the WrapPanel stacks UI elements either horizontally or vertically like the [...]]]></description>
			<content:encoded><![CDATA[<p>I’m currently working on porting the run-away hit mobile application ShopSavvy to Windows Phone 7 (<a href="https://spur.wufoo.com/forms/shopsavvy-for-windows-phone-7/">sign up to be a beta tester</a>) and one of the things I really wished I had was the WrapPanel. </p>
<p>(For those who are new to Silverlight/WPF/Windows Phone 7, the WrapPanel stacks UI elements either horizontally or vertically like the StackPanel except that, when it hits the panel limit it… wait for it… wraps and starts stacking in a new column/row. Handy.)</p>
<p>I found a way to <a href="http://www.codebadger.com/blog/post/2010/03/20/Using-the-WrapPanel-from-the-Silverlight-Toolkit-on-Windows-Phone-7.aspx">add it with the Silverlight Toolkit</a>, but doing that added 140 Kb to my application. Maybe I’m just stuck in the world of J2ME, but adding 140 Kb to my mobile app isn’t OK if I can help it. I found <a href="http://www.designersilverlight.com/2010/06/28/using-wrappanel-and-dockpanel-in-windows-phone-7-with-blend/">this post from Jeff Wilcox</a> on adding the WrapPanel and that didn’t seem to work for me. (My comments are the whiny ones at the end of the post.)</p>
<p>But I finally got it working and I’ll tell you how. </p>
<p>Or I could just taunt you. That would be fun too. </p>
<p>Naw, I’m just kidding. Here’s the stuff you need.</p>
<p><a href="http://designersilverlight.com/wp-content/uploads/2010/06/WP7Panels.zip">Windows Phone 7 WrapPanel and DockPanel (Source</a>)</p>
<p><a href="http://designersilverlight.com/wp-content/uploads/2010/06/WP7PanelsDLL.zip">Windows Phone 7 WrapPanel and DockPanel (DLL)</a></p>
<p>All you have to do is download the DLL, unzip and reference in your app like so:</p>
<p>Right click on the “References” section of your project.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2010/06/image.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.designersilverlight.com/wp-content/uploads/2010/06/image_thumb.png" width="232" height="63" /></a>&#160;</p>
<p>Select the WP7Panels DLL from whatever folder you put it in and see it show up in your References</p>
<p><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.designersilverlight.com/wp-content/uploads/2010/06/image_thumb1.png" width="280" height="186" /></p>
<p>Open up the “Assets” section and type “wrap” or “dock” the wrap-or-dock panel should show up. Double click it or drag it onto the canvas to add it to your app. Blend will take care of the rest of the namespaces and everything. </p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2010/06/image1.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.designersilverlight.com/wp-content/uploads/2010/06/image_thumb2.png" width="338" height="121" /></a>&#160;</p>
<p>And start adding items to your panel. It should work the way it’s supposed to work.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2010/06/image2.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.designersilverlight.com/wp-content/uploads/2010/06/image_thumb3.png" width="175" height="217" /></a> <a href="http://www.designersilverlight.com/wp-content/uploads/2010/06/image3.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.designersilverlight.com/wp-content/uploads/2010/06/image_thumb4.png" width="244" height="166" /></a></p>
<p>One note with the DockPanel… items placed in the DockPanel need to have the attached property</p>
<p><font color="#ff0000">[xmlns]:DockPanel.Dock</font>=”<font color="#0000ff">[something]</font>”</p>
<p>if you want them to do something other than align in the center. The [xmlns] name should be the same thing that is before the “DockPanel” in the XAML. It will most likely be “WP7Panels”. Therefore, the following XAML:</p>
<p><font color="#0000ff">&lt;</font><font color="#8b0000">WP7Panels:DockPanel</font><font color="#0000ff">&gt;</font>     <br />&#160;&#160;&#160; <font color="#0000ff">&lt;</font><font color="#800000">Button</font> <font color="#ff0000">Content</font>=&quot;<font color="#0000ff">Button</font>&quot; <font color="#ff0000">WP7Panels:DockPanel.Dock</font>=&quot;<font color="#0000ff">Bottom</font>&quot; <font color="#0000ff">/&gt;      <br /></font>&#160;&#160;&#160; <font color="#0000ff">&lt;</font><font color="#800000">Button</font> <font color="#ff0000">Content</font>=&quot;<font color="#0000ff">Button</font>&quot; <font color="#ff0000">WP7Panels:DockPanel.Dock</font>=&quot;<font color="#0000ff">Top</font>&quot; <font color="#0000ff">/&gt;      <br /></font>&#160;&#160;&#160; <font color="#0000ff">&lt;</font><font color="#800000">Button</font> <font color="#ff0000">Content</font>=&quot;<font color="#0000ff">Button</font>&quot; <font color="#ff0000">WP7Panels:DockPanel.Dock</font>=&quot;<font color="#0000ff">Right</font>&quot; <font color="#0000ff">/&gt;      <br /></font>&#160;&#160;&#160; <font color="#0000ff">&lt;</font><font color="#800000">Button</font> <font color="#ff0000">Content</font>=&quot;<font color="#0000ff">Button</font>&quot; <font color="#ff0000">WP7Panels:DockPanel.Dock</font>=&quot;<font color="#0000ff">Left</font>&quot; <font color="#0000ff">/&gt;      <br /></font>&#160;&#160;&#160; <font color="#0000ff">&lt;</font><font color="#800000">Button</font> <font color="#ff0000">Content</font>=&quot;<font color="#0000ff">Button</font>&quot; <font color="#0000ff">/&gt;      <br /><font color="#0000ff">&lt;/</font><font color="#8b0000">WP7Panels:DockPanel</font><font color="#0000ff">&gt;</font></font></p>
<p>…should look like this:</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2010/06/image4.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.designersilverlight.com/wp-content/uploads/2010/06/image_thumb5.png" width="397" height="321" /></a> </p>
<p align="left">If you want to add the actual files to your Windows Phone 7 app instead of just referencing the DLL (perhaps you are crazy or you have a deep and desperate need to fully understand absolutely everything you touch or you are a C++ developer), you can download the source. I’ve consolidated the DockPanel code into a single file (DockPanel.cs), but you’ll need the following files to get the WrapPanel working (<a href="http://designersilverlight.com/wp-content/uploads/2010/06/WP7Panels.zip">all included in the source</a>).</p>
<ul>
<li>LengthConverter.cs </li>
<li>NumericalExtensions.cs </li>
<li>OrientedSize.cs </li>
<li>TypeConverter.cs </li>
<li>WrapPanel.cs </li>
</ul>
<p>Finally, if you need something else for Windows Phone 7 from the Silverlight Toolkit, you can either add the entire toolkit (<a href="http://www.codebadger.com/blog/post/2010/03/20/Using-the-WrapPanel-from-the-Silverlight-Toolkit-on-Windows-Phone-7.aspx">explained here</a>) or download the <a href="http://silverlight.codeplex.com/SourceControl/changeset/changes/35261">source for the latest Silverlight 3 build</a> and open it in Visual Studio 2010 (for some reason, I couldn’t get it to open in Visual Studio 2008). It should update and you should be all set playing around in there to extract the stuff you need. </p>
<p>Good luck!</p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http://www.designersilverlight.com/2010/06/28/using-wrappanel-and-dockpanel-in-windows-phone-7-with-blend/&amp;layout=standard&amp;show_faces=1&amp;width=450&amp;action=like&amp;colorscheme=light&amp;font=" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:25px"></iframe><div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Using+WrapPanel+and+DockPanel+in+Windows+Phone+7+With+Blend+http%3A%2F%2Fdesignersilverlight.com%2F%3Fp%3D1044" title="Post to Twitter"><img class="nothumb" src="http://www.designersilverlight.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter.png" alt="Post to Twitter" /></a> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Using+WrapPanel+and+DockPanel+in+Windows+Phone+7+With+Blend+http%3A%2F%2Fdesignersilverlight.com%2F%3Fp%3D1044" title="Post to Twitter">Tweet This Post</a></p></div>]]></content:encoded>
			<wfw:commentRss>http://www.designersilverlight.com/2010/06/28/using-wrappanel-and-dockpanel-in-windows-phone-7-with-blend/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>PHP, MySQL, and Silverlight: The Complete Tutorial (Part 3)</title>
		<link>http://www.designersilverlight.com/2010/05/24/php-mysql-and-silverlight-the-complete-tutorial-part-3/</link>
		<comments>http://www.designersilverlight.com/2010/05/24/php-mysql-and-silverlight-the-complete-tutorial-part-3/#comments</comments>
		<pubDate>Mon, 24 May 2010 19:30:58 +0000</pubDate>
		<dc:creator>Matthias Shapiro</dc:creator>
				<category><![CDATA[Binding]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[Data Binding]]></category>
		<category><![CDATA[eventtrigger]]></category>
		<category><![CDATA[How To...]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://www.designersilverlight.com/?p=970</guid>
		<description><![CDATA[This is meant to be the one-stop-shop blog post for creating a very simple web service in PHP that pulls from a MySQL database and displaying the data in Silverlight. Emphasis on the “simple”. Here is an example of the finished product (I reserve the right to clean up the data on a regular basis): [...]]]></description>
			<content:encoded><![CDATA[<p>This is meant to be the one-stop-shop blog post for creating a very simple web service in PHP that pulls from a MySQL database and displaying the data in Silverlight. Emphasis on the “simple”. Here is an example of the finished product (I reserve the right to clean up the data on a regular basis):</p>
<div id="silverlightControlHost">
		<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="500px" height="300px"><param name="source" value="http://designersilverlight.com/Silverlight/ToDo_List_Tutorial.xap"/><param name="onerror" value="onSilverlightError" /><param name="background" value="white" /><param name="minRuntimeVersion" value="3.0.40818.0" /><param name="autoUpgrade" value="true" /><a href="http://go.microsoft.com/fwlink/?LinkID=149156&#038;v=3.0.40818.0" style="text-decoration: none;"><br />
				<img src="http://go.microsoft.com/fwlink/?LinkID=161376" alt="Get Microsoft Silverlight" style="border-style: none"/><br />
			</a><br />
		</object><iframe id='_sl_historyFrame' style='visibility:hidden;height:0;width:0;border:0px'></iframe></div>
<p><a href="http://www.designersilverlight.com/2010/05/22/php-mysql-and-silverlight-the-complete-tutorial-part-1/">Part 1: MySQL</a></p>
<p><a href="http://www.designersilverlight.com/2010/05/23/php-mysql-and-silverlight-the-complete-tutorial-part-2/">Part 2: PHP</a></p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2010/05/AllFiles_PHP_and_Silverlight.zip">Download all files (PHP &#038; Silverlight)</a><br />
<a href="http://www.designersilverlight.com/wp-content/uploads/2010/05/PHP_Tutorial_Files.zip">Download PHP files only</a><br />
<a href="http://www.designersilverlight.com/wp-content/uploads/2010/05/ToDo_List_Tutorial.zip">Download Silverlight project only</a></p>
<p>In Part 1, we created the MySQL tables necessary to store data for a simple to-do list. In Part 2, we wrote a PHP service to deliver the items in our to-do list in JSON format. In Part 3, we’ll create a Silverlight application that can utilize this web service to retrieve, display, and edit the to-do list.</p>
<h2>Part 3: Connecting Silverlight To Our PHP Web Service</h2>
<p>First, let’s start a new Silverlight project. (You can download the full project here.) In the interest of time, I’m not going to go into the details of the “ideal” implementation for this. I’m just going to show the parts necessary to retrieve, display and interact with the to-do data.</p>
<p>Before we start with the UI, let’s build a model so that we can appropriately bind our data. Add a new folder to the Silverlight project and name it “Models”. Add a new class to it and name the class “ToDoItem.cs”. In this instance, we’re just going to make the model look just like our MySQL table.</p>
<p><strong>ToDoItem.cs</strong></p>
<p><span style="color: #0000ff; font-size: small;"> </span></p>
<p><span style="color: #0000ff; font-size: small;"><span style="font-family: Consolas;">p<span style="font-size: small;">ublic </span></span><span style="font-size: small;"><span style="font-family: Consolas;"><span style="color: #0000ff;">class</span><span style="color: #000000;"> </span><span style="color: #2b91af;">ToDoItem</span></span></span><br />
<span style="font-family: Consolas; font-size: small;">{<br />
</span><span style="font-size: small;"><span style="font-family: Consolas;"><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">bool</span><span style="color: #000000;"> isDone { </span><span style="color: #0000ff;">get</span><span style="color: #000000;">; </span><span style="color: #0000ff;">set</span></span></span><span style="color: #000000;"><span style="font-family: Consolas; font-size: small;">; }</span></span><br />
<span style="font-size: small;"><span style="font-family: Consolas;"><span style="color: #0000ff;"> public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">string</span><span style="color: #000000;"> toDoDescription {</span><span style="color: #0000ff;">get</span><span style="color: #000000;">; </span><span style="color: #0000ff;">set</span></span></span><span style="color: #000000;"><span style="font-family: Consolas; font-size: small;">;}</span></span><br />
<span style="font-size: small;"><span style="font-family: Consolas;"><span style="color: #0000ff;"> public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> toDoID {</span><span style="color: #0000ff;">get</span><span style="color: #000000;">; </span><span style="color: #0000ff;">set</span></span></span><span style="color: #000000;"><span style="font-family: Consolas; font-size: small;">;}</span></span><br />
<span style="font-family: Consolas; color: #0000ff; font-size: small;"><br />
public</span><span style="color: #000000;"><span style="font-family: Consolas; font-size: small;"> ToDoItem()</span></span> <span style="font-family: Consolas; font-size: small;">{ </span><span style="font-family: Consolas; font-size: small;">}<br />
</span><span style="font-family: Consolas; font-size: small;">}</span></span></p>
<p><span style="color: #0000ff; font-size: small;"><span style="font-family: Consolas; font-size: small;"> </span></span></p>
<p><span style="color: #0000ff; font-size: small;"> </span></p>
<p>Now, we’ll add to our MainPage.xaml a ListBox named “ToDoList” that will hold the to-do items and a Grid to house the UI to add a new to-do. We’ll work on gathering and displaying our items first.</p>
<p>Right-click on the ListBox and go to “<strong>Edit Additional Templates –&gt; Edit Generated Items (ItemTemplate) –&gt; Create Empty…</strong>”<br />
<a href="http://www.designersilverlight.com/wp-content/uploads/2010/05/clip_image0012.png"><img style="display: inline; border-width: 0px;" title="clip_image001" src="http://www.designersilverlight.com/wp-content/uploads/2010/05/clip_image001_thumb2.png" border="0" alt="clip_image001" width="634" height="94" /></a><br />
You’ll love this part… Just add a checkbox to it. That’s all we’ll need, so that’s all we’ll add. Then, go into the XAML and make the checkbox look like this:</p>
<p><span style="color: #0000ff; font-size: small;"> </span></p>
<p><span style="color: #0000ff; font-size: small;"><span style="font-family: Consolas; font-size: small;">&lt;</span><span style="font-size: small;"><span style="font-family: Consolas;"><span style="color: #a31515;">CheckBox</span><span style="color: #ff0000;"> Content</span><span style="color: #0000ff;">=&#8221;{</span><span style="color: #a31515;">Binding</span><span style="color: #ff0000;"> toDoDescription</span><span style="color: #0000ff;">}&#8221;<br />
</span></span></span><span style="font-size: small;"><span style="font-family: Consolas;"><span style="color: #ff0000;"> IsChecked</span><span style="color: #0000ff;">=&#8221;{</span><span style="color: #a31515;">Binding</span><span style="color: #ff0000;"> isDone</span><span style="color: #0000ff;">}&#8221;</span></span></span><span style="color: #000000;"> </span><br />
<span style="font-size: small;"><span style="font-family: Consolas;"><span style="color: #ff0000;"> Tag</span><span style="color: #0000ff;">=&#8221;{</span><span style="color: #a31515;">Binding</span><span style="color: #ff0000;"> toDoID</span><span style="color: #0000ff;">}&#8221; /&gt;</span></span></span></span></p>
<p><span style="color: #0000ff; font-size: small;"> </span></p>
<p><span style="font-family: Consolas; color: #0000ff; font-size: small;"> </span></p>
<p>This binding is all we’ll need once we gather our to-dos from the web service and attach it to the ListBox. Let’s go ahead and get that data into this ListBox.</p>
<p>Open MainPage.xaml.cs and, at the top of the class, add:</p>
<p><span style="font-size: small;"><span style="font-family: Consolas;"><span style="color: #2b91af;">WebClient</span><span style="color: #000000;"> wc = </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> </span><span style="color: #2b91af;">WebClient</span></span></span><span style="font-size: small;"><span style="font-family: Consolas;"><span style="color: #000000;">();<br />
</span><span style="color: #2b91af;">ObservableCollection</span><span style="color: #000000;">&lt;</span><span style="color: #2b91af;">ToDoItem</span><span style="color: #000000;">&gt; myToDoList = </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> </span><span style="color: #2b91af;">ObservableCollection</span><span style="color: #000000;">&lt;</span><span style="color: #2b91af;">ToDoItem</span></span></span><span style="font-size: small;"><span style="font-family: Consolas;"><span style="color: #000000;">&gt;();<br />
</span><span style="color: #0000ff;">string</span><span style="color: #000000;"> baseURI = </span><span style="color: #a31515;">&#8220;http://&lt;Web_address_holding_your_php_files&gt;&#8221;</span><span style="color: #000000;">;</span></span></span></p>
<p>You may have to add “using System.Net;” and “using System.Collections.ObjectModel;” to the top of the file. While we’re adding references, right-click on the “References” folder and find the “System.Json” component and add it to the project. Then add “using System.Json;” to the references section in the top.  It’ll come in handy in a minute.</p>
<p>In the MainPage() method under InitializeComponent(), type “wc.DownloadStringCompleted += “ and hit TAB twice. This will add an event handler for when the WebClient has finished connecting to the web service we wrote.</p>
<p>The resulting event handler (which should be named “wc_DownloadStringCompleted”), is the code that our application will go to whenever it makes a call to our web service and gets a result. In it, we will do the following things:</p>
<ol>
<li>Check to make sure we got a result without error</li>
<li>Check to see what kind of result we got (did we get all to-do items? or add a new to-do item?)</li>
<li>Walk through the result, extracting the data we need</li>
<li>Apply that data to the UI</li>
</ol>
<p>Let’s add the code to do that for getting all the items. Make your event handler look like this:</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:7934bf62-c08c-435e-980b-d18700e35c2b" class="wlWriterEditableSmartContent" style="margin: 0px; display: inline; float: none; padding: 0px;">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;">
<div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px;">wc_DownloadStringCompleted</div>
<div style="background: #ddd; max-height: 400px; overflow: auto;">
<ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;">
<li><span style="color: #0000ff;">void</span> wc_DownloadStringCompleted(<span style="color: #0000ff;">object</span> sender, <span style="color: #2b91af;">DownloadStringCompletedEventArgs</span> e)</li>
<li style="background: #f3f3f3;"> {</li>
<li> <span style="color: #0000ff;">if</span> (e.Error == <span style="color: #0000ff;">null</span> &amp;&amp; e.Result!= <span style="color: #a31515;">&#8220;&#8221;</span>)</li>
<li style="background: #f3f3f3;"> {</li>
<li> <span style="color: #2b91af;">JsonValue</span> completeResult = <span style="color: #2b91af;">JsonPrimitive</span>.Parse(e.Result);</li>
<li style="background: #f3f3f3;"> <span style="color: #0000ff;">string</span> resultType = completeResult[<span style="color: #a31515;">"returnType"</span>].ToString().Replace(<span style="color: #a31515;">&#8216;&#8221;&#8216;</span>, <span style="color: #a31515;">&#8216; &#8216;</span>).Trim();</li>
<li> <span style="color: #0000ff;">if</span> (resultType == <span style="color: #a31515;">&#8220;todoItems&#8221;</span>)</li>
<li style="background: #f3f3f3;"> {</li>
<li> myToDoList.Clear();</li>
<li style="background: #f3f3f3;"></li>
<li> <span style="color: #2b91af;">JsonArray</span> toDoItemsJson = (<span style="color: #2b91af;">JsonArray</span>)completeResult[<span style="color: #a31515;">"results"</span>];</li>
<li style="background: #f3f3f3;"> <span style="color: #0000ff;">foreach</span> (<span style="color: #2b91af;">JsonValue</span> todoItemJson <span style="color: #0000ff;">in</span> toDoItemsJson)</li>
<li> {</li>
<li style="background: #f3f3f3;"> <span style="color: #2b91af;">ToDoItem</span> thisTodo = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">ToDoItem</span>();</li>
<li></li>
<li style="background: #f3f3f3;"> <span style="color: #0000ff;">if</span> (todoItemJson[<span style="color: #a31515;">"ToDoIndex"</span>] != <span style="color: #0000ff;">null</span>)</li>
<li> thisTodo.toDoID = <span style="color: #2b91af;">Convert</span>.ToInt32(todoItemJson[<span style="color: #a31515;">"ToDoIndex"</span>].ToString().Replace(<span style="color: #a31515;">&#8216;&#8221;&#8216;</span>, <span style="color: #a31515;">&#8216; &#8216;</span>).Trim());</li>
<li style="background: #f3f3f3;"> <span style="color: #0000ff;">if</span> (todoItemJson[<span style="color: #a31515;">"TodoText"</span>] != <span style="color: #0000ff;">null</span>)</li>
<li> thisTodo.toDoDescription = todoItemJson[<span style="color: #a31515;">"TodoText"</span>].ToString().Replace(<span style="color: #a31515;">&#8216;&#8221;&#8216;</span>, <span style="color: #a31515;">&#8216; &#8216;</span>).Trim();</li>
<li style="background: #f3f3f3;"> <span style="color: #0000ff;">if</span> (todoItemJson[<span style="color: #a31515;">"IsDone"</span>] != <span style="color: #0000ff;">null</span>)</li>
<li> {</li>
<li style="background: #f3f3f3;"> <span style="color: #0000ff;">int</span> isDoneInt =  <span style="color: #2b91af;">Convert</span>.ToInt32(todoItemJson[<span style="color: #a31515;">"IsDone"</span>].ToString().Replace(<span style="color: #a31515;">&#8216;&#8221;&#8216;</span>, <span style="color: #a31515;">&#8216; &#8216;</span>).Trim());</li>
<li> <span style="color: #0000ff;">if</span>(isDoneInt == 0){</li>
<li style="background: #f3f3f3;"> thisTodo.isDone = <span style="color: #0000ff;">false</span>;</li>
<li> } <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> (isDoneInt == 1){</li>
<li style="background: #f3f3f3;"> thisTodo.isDone = <span style="color: #0000ff;">true</span>;</li>
<li> }</li>
<li style="background: #f3f3f3;"> }</li>
<li> myToDoList.Add(thisTodo);</li>
<li style="background: #f3f3f3;"> }</li>
<li> ToDoList.ItemsSource = myToDoList;</li>
<li style="background: #f3f3f3;"> }</li>
<li> }</li>
<li style="background: #f3f3f3;"> }</li>
</ol>
</div>
</div>
</div>
<p>With this in place, all we need to do is make a call to the get_todo_items.php, which we can do by adding this code just below the wc.DownloadStringCompleted line in the MainPage() method.</p>
<p><span style="font-size: small;"><span style="font-family: Consolas;"><span style="color: #000000;">wc.DownloadStringAsync(</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> </span><span style="color: #2b91af;">Uri</span><span style="color: #000000;">(baseURI + </span><span style="color: #a31515;">&#8220;get_todo_items.php&#8221;</span><span style="color: #000000;">));</span></span></span></p>
<p>When we run the project, we should get all objects from our database and the results should show up just the way we want in our Silverlight application. We will follow the exact same model to add new to-do items and update the to-do items we already have.</p>
<p>I’m in the process of separating out the final part of this tutorial (adding and updating the items) into a supplemental post so that I can wrap this post up. I encourage anyone walking through this to try to complete those parts by yourself and refer to the last bit only if you get stuck.</p>
<p>Finally, a little bit of warning. I structured this project to get it working in as straightforward a manner as possible. It is not in any way the ideal architectural example for a data-driven Silverlight application. It’s just a good way to get started using Silverlight in an environment that isn’t 100% Microsoft technologies.</p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http://www.designersilverlight.com/2010/05/24/php-mysql-and-silverlight-the-complete-tutorial-part-3/&amp;layout=standard&amp;show_faces=1&amp;width=450&amp;action=like&amp;colorscheme=light&amp;font=" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:25px"></iframe><div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=PHP%2C+MySQL%2C+and+Silverlight%3A+The+Complete+Tutorial+%28Part+3%29+http%3A%2F%2Fdesignersilverlight.com%2F%3Fp%3D970" title="Post to Twitter"><img class="nothumb" src="http://www.designersilverlight.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter.png" alt="Post to Twitter" /></a> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=PHP%2C+MySQL%2C+and+Silverlight%3A+The+Complete+Tutorial+%28Part+3%29+http%3A%2F%2Fdesignersilverlight.com%2F%3Fp%3D970" title="Post to Twitter">Tweet This Post</a></p></div>]]></content:encoded>
			<wfw:commentRss>http://www.designersilverlight.com/2010/05/24/php-mysql-and-silverlight-the-complete-tutorial-part-3/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>PHP, MySQL, and Silverlight: The Complete Tutorial (Part 2)</title>
		<link>http://www.designersilverlight.com/2010/05/23/php-mysql-and-silverlight-the-complete-tutorial-part-2/</link>
		<comments>http://www.designersilverlight.com/2010/05/23/php-mysql-and-silverlight-the-complete-tutorial-part-2/#comments</comments>
		<pubDate>Mon, 24 May 2010 00:07:35 +0000</pubDate>
		<dc:creator>Matthias Shapiro</dc:creator>
				<category><![CDATA[coding]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[web service]]></category>

		<guid isPermaLink="false">http://www.designersilverlight.com/?p=964</guid>
		<description><![CDATA[This is meant to be the one-stop-shop blog post for creating a very simple web service in PHP that pulls from a MySQL database and displaying the data in Silverlight. Emphasis on the “simple”. Here is an example of the finished product (I reserve the right to clean up the data on a regular basis): [...]]]></description>
			<content:encoded><![CDATA[<p>This is meant to be the one-stop-shop blog post for creating a very simple web service in PHP that pulls from a MySQL database and displaying the data in Silverlight. Emphasis on the “simple”.</p>
<p>Here is an example of the finished product (I reserve the right to clean up the data on a regular basis):</p>
<div id="silverlightControlHost">
		<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="500px" height="300px"><param name="source" value="http://designersilverlight.com/Silverlight/ToDo_List_Tutorial.xap"/><param name="onerror" value="onSilverlightError" /><param name="background" value="white" /><param name="minRuntimeVersion" value="3.0.40818.0" /><param name="autoUpgrade" value="true" /><a href="http://go.microsoft.com/fwlink/?LinkID=149156&#038;v=3.0.40818.0" style="text-decoration: none;"><br />
				<img src="http://go.microsoft.com/fwlink/?LinkID=161376" alt="Get Microsoft Silverlight" style="border-style: none"/><br />
			</a><br />
		</object><iframe id='_sl_historyFrame' style='visibility:hidden;height:0;width:0;border:0px'></iframe></div>
<p><a href="http://www.designersilverlight.com/2010/05/22/php-mysql-and-silverlight-the-complete-tutorial-part-1/">Part 1: MySQL</a></p>
<p><a href="http://www.designersilverlight.com/2010/05/24/php-mysql-and-silverlight-the-complete-tutorial-part-3/">Part 3: Silverlight</a></p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2010/05/AllFiles_PHP_and_Silverlight.zip">Download all files (PHP &#038; Silverlight)</a><br />
<a href="http://www.designersilverlight.com/wp-content/uploads/2010/05/PHP_Tutorial_Files.zip">Download PHP files only</a><br />
<a href="http://www.designersilverlight.com/wp-content/uploads/2010/05/ToDo_List_Tutorial.zip">Download Silverlight project only</a></p>
<p>In Part 1, we created the MySQL tables necessary to store data for a simple to-do list. In Part 2, we’ll write some PHP code that will give us the ability to grab the data out of the database and send it, in JSON format, to our Silverlight application.</p>
<h2>Part 2: Writing the PHP Web Service</h2>
<p>We’re going to create 4 PHP files (<a href="http://www.designersilverlight.com/wp-content/uploads/2010/05/PHP_Tutorial_Files.zip">download  php files</a>):</p>
<ul>
<li>mysql_vars.php – holds all the information for connecting to the MySQL database</li>
<li>get_todo_items.php – for getting all the to do items</li>
<li>add_todo_item.php – adds a to do item</li>
<li>change_status.php – changes a to-do item from “not done” to “done” or vice versa</li>
</ul>
<p>Since all these projects will be using the information in mysql_vars.php, we’ll write that first.</p>
<p><strong>mysql_vars.php</strong></p>
<p><span style="color: #ff0000;">&lt;?php</span><br />
$dbUsername = &#8220;<span style="color: #cc0000;">[my_database_username]</span>&#8220;;<br />
$dbPassword = &#8220;<span style="color: #cc0000;">[my_database_password]</span>&#8220;;<br />
$db = &#8220;<span style="color: #cc0000;">[my_database_name]</span>&#8220;;<br />
$server = &#8220;<span style="color: #cc0000;">[my_database_server]</span>&#8220;;</p>
<p><span style="color: #ff9900;"> //To Do Table and Column Names</span><br />
$mysql_todoTable  = &#8220;<span style="color: #cc0000;">to_do_data</span>&#8220;;<br />
$mysql_todoIndexCol = &#8220;<span style="color: #cc0000;">index_key</span>&#8220;;<br />
$mysql_isDoneCol = &#8220;<span style="color: #cc0000;">is_done</span>&#8220;;<br />
$mysql_todoTextCol = &#8220;<span style="color: #cc0000;">to_do_text</span>&#8220;;</p>
<p>$connection = <span style="color: #0000ff;">mysql_connect</span>($server, $dbUsername, $dbPassword);</p>
<p><span style="color: #0000ff;"> function</span> formatInput($rawURLData)<br />
{<br />
$returnString = <span style="color: #0000ff;">urldecode</span>($rawURLData);<br />
$returnString = <span style="color: #0000ff;">mysql_real_escape_string</span>($returnString);<br />
<span style="color: #006600;">return</span> $returnString;<br />
}<br />
<span style="color: #ff0000;">?&gt;</span></p>
<p>We’ve added the to-do table and column names so that, if we decide to change anything later, we can just go to this file and update the table or column once.</p>
<p>Just for good measure, we’ve added a function we’ll want to use across our php files. The function “formatInput” will be used to make sure all our data is decoded from the URL that calls our web service (the urldecode method) and then try to block any SQL injections (the mysql_real_escape_string method).</p>
<p>Now, let’s write the basic “Get the data” file. What we’re going to do is write it so that the we can choose to get:</p>
<ul>
<li>all the to-do items</li>
<li>all the to-do items that are “done”</li>
<li>all the to-do items that are “not done”</li>
</ul>
<p>This range of functionality isn’t even close to ideal. In a perfect world, we would want a wider range of options in gathering items (for example, items that contain a certain word or one item in particular or limit the number of items we call by a date range). However, for our very simple purposes, this will do.</p>
<p>The way our web service will work is that we have a URL that we’ll call from Silverlight when we want to get some data. When we calls this web service, we may want only the &#8220;not done&#8221; items or only the &#8220;done&#8221; items. We&#8217;ll handle that option by adding &#8220;?itemStatus=done&#8221; or &#8220;?itemStatus=notDone&#8221; to the end of the URL.</p>
<p>Example: if our base URL is</p>
<p><a href="http://www.mywebsite.com/">http://www.mywebsite.com/</a></p>
<p>the call to</p>
<p><a href="http://www.mywebsite.com/get_todo_items.php">http://www.mywebsite.com/get_todo_items.php</a></p>
<p>will get all items, regardless of their completed status while</p>
<p><a href="http://www.mywebsite.com/get_todo_items.php?itemStatus=done">http://www.mywebsite.com/get_todo_items.php?itemStatus=done</a></p>
<p>will get all the to do items that are complete. So we need to make sure that our web service responds appropriately to both calls.</p>
<p>There are comments in the code, but I&#8217;ll just explain the basic concept in picture form:</p>
<p>We take in a URL, extract the variables from it, create the MySQL query based on the variables, execute the MySQL query, extract the results, and then send back the php object encoded as a Json object. Each one of our files will follow this same pattern.</p>
<p><strong>get_todo_items.php</strong></p>
<p><span style="color: #ff0000;">&lt;?</span><br />
<span style="color: #008000;"> include</span> <span style="color: #cc0000;">&#8216;mysql_vars.php&#8217;</span>;</p>
<p><span style="color: #ff9900;"> //    set up the &#8220;itemStatus&#8221; URL option and build a query addition<br />
//        to account for the itemStatus variable<br />
</span>$itemStatus = <span style="color: #0066ff;">$_GET</span>[<span style="color: #cc0000;">'itemStatus'</span>];<br />
$itemQueryAddition = &#8220;&#8221;;</p>
<p><span style="color: #008000;"> if</span>($itemStatus <span style="color: #0000ff;">!=</span> <span style="color: #008000;">NULL</span>)<br />
{<br />
<span style="color: #008000;"> if</span>($itemStatus <span style="color: #0000ff;">==</span> <span style="color: #cc0000;">&#8220;done&#8221;</span>)<br />
{<br />
$itemQueryAddition = <span style="color: #cc0000;">&#8220;WHERE `$mysql_isDoneCol` = 1&#8243;</span>;<br />
} <span style="color: #008000;">else if</span> ($itemStatus == <span style="color: #cc0000;">&#8220;notDone&#8221;</span>)<br />
{<br />
$itemQueryAddition = <span style="color: #cc0000;">&#8220;WHERE `$mysql_isDoneCol` = 0&#8243;</span>;<br />
}</p>
<p><span style="color: #ff9900;"> // Construct our MySQL query</span><br />
$todoQuery = <span style="color: #cc0000;">&#8220;SELECT * FROM `$mysql_todoTable` $itemQueryAddition ;&#8221;</span>;</p>
<p><span style="color: #ff9900;"> // execute the query and gather the results&#8230;</span><br />
<span style="color: #0000ff;">mysql_select_db</span>($db, $connection);<br />
$todoResult =<span style="color: #0000ff;"> mysql_query</span>($todoQuery);<br />
$todoArray = <span style="color: #0000ff;">array()</span>;</p>
<p><span style="color: #008000;"> while</span>($itemRow <span style="color: #0000ff;">= mysql_fetch_array</span>($todoResult))<br />
{<br />
$todoArray<span style="color: #0000ff;">[] = array(</span> <span style="color: #cc0000;">&#8220;ToDoIndex&#8221;</span> <span style="color: #0000ff;">=&gt;</span> $itemRow[<span style="color: #cc0000;">'index_key'</span>],<br />
<span style="color: #cc0000;">&#8220;IsDone&#8221;</span> <span style="color: #0000ff;">=&gt;</span> $itemRow[<span style="color: #cc0000;">'is_done'</span>],<br />
<span style="color: #cc0000;">&#8220;TodoText&#8221;</span> <span style="color: #0000ff;">=&gt;</span> $itemRow[<span style="color: #cc0000;">'to_do_text'</span>] );<br />
}<br />
<span style="color: #0000ff;">mysql_close</span>($connection);</p>
<p><span style="color: #ff9900;"> // &#8230; then encode the results as JSON Text&#8230;<br />
//   we&#8217;re using a &#8220;returnType&#8221; field so that our Silverlight application can differentiate between<br />
//   the kind of return values it recieves and parse the Json object appropriately</span></p>
<p>$returnItems = <span style="color: #0000ff;">array(</span> <span style="color: #cc0000;">&#8220;returnType&#8221;</span> =&gt; &#8220;todoItems&#8221;,<br />
<span style="color: #cc0000;">&#8220;results&#8221;</span> =&gt; $returnItems);<br />
$JSONResult = json_encode($todoArray);</p>
<p><span style="color: #ff9900;"> // &#8230; and print the results so that our app can read them<br />
</span> <span style="color: #0000ff;">echo</span> $JSONResult;<br />
<span style="color: #ff0000;">?&gt;</span></p>
<p>The other two files,<strong> add_todo_item.php</strong> and <strong>change_status.php</strong> use exactly the same structure to add a new item and change the status of an existing item (respectively). I won’t put all the code here in this post that is already too long, but you can download all the files here.</p>
<p>Update the mysql_vars.php file to fit your needs and you should be able to just upload these files and have your running to-do web service all ready for Silverlight to call it for data, which is something we’ll deal with in Part 3.</p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http://www.designersilverlight.com/2010/05/23/php-mysql-and-silverlight-the-complete-tutorial-part-2/&amp;layout=standard&amp;show_faces=1&amp;width=450&amp;action=like&amp;colorscheme=light&amp;font=" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:25px"></iframe><div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=PHP%2C+MySQL%2C+and+Silverlight%3A+The+Complete+Tutorial+%28Part+2%29+http%3A%2F%2Fdesignersilverlight.com%2F%3Fp%3D964" title="Post to Twitter"><img class="nothumb" src="http://www.designersilverlight.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter.png" alt="Post to Twitter" /></a> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=PHP%2C+MySQL%2C+and+Silverlight%3A+The+Complete+Tutorial+%28Part+2%29+http%3A%2F%2Fdesignersilverlight.com%2F%3Fp%3D964" title="Post to Twitter">Tweet This Post</a></p></div>]]></content:encoded>
			<wfw:commentRss>http://www.designersilverlight.com/2010/05/23/php-mysql-and-silverlight-the-complete-tutorial-part-2/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>PHP, MySQL and Silverlight: The Complete Tutorial (Part 1)</title>
		<link>http://www.designersilverlight.com/2010/05/23/php-mysql-and-silverlight-the-complete-tutorial-part-1/</link>
		<comments>http://www.designersilverlight.com/2010/05/23/php-mysql-and-silverlight-the-complete-tutorial-part-1/#comments</comments>
		<pubDate>Sun, 23 May 2010 18:56:25 +0000</pubDate>
		<dc:creator>Matthias Shapiro</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[database]]></category>

		<guid isPermaLink="false">http://www.designersilverlight.com/?p=960</guid>
		<description><![CDATA[This is meant to be the one-stop-shop blog post for creating a very simple web service in PHP that pulls from a MySQL database and displaying the data in Silverlight. Emphasis on the “simple”. This tutorial is geared toward someone who has never done databases or web services. Here is an example of the finished [...]]]></description>
			<content:encoded><![CDATA[<p>This is meant to be the one-stop-shop blog post for creating a very simple web service in PHP that pulls from a MySQL database and displaying the data in Silverlight. Emphasis on the “simple”. This tutorial is geared toward someone who has never done databases or web services.</p>
<p>Here is an example of the finished product (I reserve the right to clean up the data on a regular basis):</p>
<div id="silverlightControlHost">
		<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="500px" height="300px"><param name="source" value="http://designersilverlight.com/Silverlight/ToDo_List_Tutorial.xap"/><param name="onerror" value="onSilverlightError" /><param name="background" value="white" /><param name="minRuntimeVersion" value="3.0.40818.0" /><param name="autoUpgrade" value="true" /><a href="http://go.microsoft.com/fwlink/?LinkID=149156&#038;v=3.0.40818.0" style="text-decoration: none;"><br />
				<img src="http://go.microsoft.com/fwlink/?LinkID=161376" alt="Get Microsoft Silverlight" style="border-style: none"/><br />
			</a><br />
		</object><iframe id='_sl_historyFrame' style='visibility:hidden;height:0;width:0;border:0px'></iframe></div>
<p>This tutorial will walk through the steps to create a simple to-do list. Our to-do list will hold text of what it is we need to do and a value indicating if the task has been done. In this tutorial, we will create a MySQL table to hold our data, a PHP service to call the data and a Silverlight application to display and interact with the data.</p>
<p><a href="http://www.designersilverlight.com/2010/05/23/php-mysql-and-silverlight-the-complete-tutorial-part-2/">Part 2: PHP</a></p>
<p><a href="http://www.designersilverlight.com/2010/05/24/php-mysql-and-silverlight-the-complete-tutorial-part-3/">Part 3: Silverlight</a></p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2010/05/AllFiles_PHP_and_Silverlight.zip">Download all files (PHP &#038; Silverlight)</a><br />
<a href="http://www.designersilverlight.com/wp-content/uploads/2010/05/PHP_Tutorial_Files.zip">Download PHP files only</a><br />
<a href="http://www.designersilverlight.com/wp-content/uploads/2010/05/ToDo_List_Tutorial.zip">Download Silverlight project only</a></p>
<h2>Part 1: The MySQL Database</h2>
<p>First, let’s create the table we need for our data.</p>
<p>If you don’t have or don’t like phpMyAdmin, the MySQL query to create the table described below is:</p>
<p>CREATE TABLE `[your_database_name]`.`to_do_data` (<br />
`index_key` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,<br />
`is_done` TINYINT( 4 ) NOT NULL DEFAULT &#8217;0&#8242;,<br />
`to_do_text` TEXT NOT NULL ,<br />
FULLTEXT ( `to_do_text` )<br />
) ENGINE = MYISAM</p>
<p>If you have phpMyAdmin, I highly suggest using it if you’re a MySQL novice. In phpMyAdmin, go to your database and enter the name of the table you want to create in the “Create new table on database [your_db_name]” section.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2010/05/clip_image0015.png"><img style="display: inline; border: 0px;" title="clip_image001[5]" src="http://www.designersilverlight.com/wp-content/uploads/2010/05/clip_image0015_thumb.png" border="0" alt="clip_image001[5]" width="445" height="105" /></a></p>
<p>We’ll call our table “to_do_data” and give it 3 fields.</p>
<ul>
<li>index_key – identifies the to-do item uniquely</li>
<li>is_done  &#8211; true/false value indicates the status of the to-do item</li>
<li>to_do_text &#8211; a short text to describe what needs to be done</li>
</ul>
<p>In phpMyAdmin, it will look like this:</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2010/05/clip_image0011.png"><img style="display: inline; border: 0px;" title="clip_image001" src="http://www.designersilverlight.com/wp-content/uploads/2010/05/clip_image001_thumb1.png" border="0" alt="clip_image001" width="735" height="343" /></a></p>
<p>A couple of notes about the fields:</p>
<p><strong>index_key</strong></p>
<p>This is the primary key of the table and is used to uniquely identify the given row. As such, it cannot be null and it auto-increments as rows are added to the table.</p>
<p><strong>is_done</strong></p>
<p>We use “TINYINT” type for this value because <a href="http://stackoverflow.com/questions/289727/which-mysql-datatype-to-use-for-storing-boolean-values-from-to-php">using BOOLEAN is basically the same thing</a>. It is not null because every item must either be “done” or “not done”. “Done” = true = 1 and “not done” = false = 0. We are going to default to “0” (false) because we’re assuming that users aren’t going to make a to do list of stuff that is already complete.</p>
<p><strong>to_do_text</strong></p>
<p>We have artificially limited the text size to 1024 because we assume that this will be a set of short to-dos to, not a set of journal entries. We’ve also turned on “Fulltext” which lets the MySQL database index our entries for quick searching.</p>
<p>Now we just click the “Save” button at the bottom and we have our table.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2010/05/clip_image0017.png"><img style="display: inline; border: 0px;" title="clip_image001[7]" src="http://www.designersilverlight.com/wp-content/uploads/2010/05/clip_image0017_thumb.png" border="0" alt="clip_image001[7]" width="794" height="105" /></a></p>
<p>Now our database system is in place and we’re ready to write a PHP webservice to implement all the CRUD (create, read, update, delete) capabilities our system will need.</p>
<p>If you want to learn more about MySQL, I highly recommend &#8220;<a href="http://www.amazon.com/gp/product/0471412767?ie=UTF8&#038;tag=irrationalopt-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0471412767">A Visual Introduction to SQL</a><img src="http://www.assoc-amazon.com/e/ir?t=irrationalopt-20&#038;l=as2&#038;o=1&#038;a=0471412767" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />&#8220;. This is the book I have, a left-over of my grad school years and it is an exceptional book for picking your way through various database scenarios. </p>
<p>If you&#8217;re looking for something a bit less costly, &#8220;<a href="http://www.amazon.com/gp/product/0672327120?ie=UTF8&#038;tag=irrationalopt-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0672327120">MySQL Crash Course</a><img src="http://www.assoc-amazon.com/e/ir?t=irrationalopt-20&#038;l=as2&#038;o=1&#038;a=0672327120" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />&#8221; is a fine book on the subject as well.</p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http://www.designersilverlight.com/2010/05/23/php-mysql-and-silverlight-the-complete-tutorial-part-1/&amp;layout=standard&amp;show_faces=1&amp;width=450&amp;action=like&amp;colorscheme=light&amp;font=" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:25px"></iframe><div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=PHP%2C+MySQL+and+Silverlight%3A+The+Complete+Tutorial+%28Part+1%29+http%3A%2F%2Fdesignersilverlight.com%2F%3Fp%3D960" title="Post to Twitter"><img class="nothumb" src="http://www.designersilverlight.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter.png" alt="Post to Twitter" /></a> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=PHP%2C+MySQL+and+Silverlight%3A+The+Complete+Tutorial+%28Part+1%29+http%3A%2F%2Fdesignersilverlight.com%2F%3Fp%3D960" title="Post to Twitter">Tweet This Post</a></p></div>]]></content:encoded>
			<wfw:commentRss>http://www.designersilverlight.com/2010/05/23/php-mysql-and-silverlight-the-complete-tutorial-part-1/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>New Silverlight Posts at Veracity Blogs</title>
		<link>http://www.designersilverlight.com/2009/08/19/new-silverlight-posts-at-veracity-blogs/</link>
		<comments>http://www.designersilverlight.com/2009/08/19/new-silverlight-posts-at-veracity-blogs/#comments</comments>
		<pubDate>Wed, 19 Aug 2009 16:09:27 +0000</pubDate>
		<dc:creator>Matthias Shapiro</dc:creator>
				<category><![CDATA[How To...]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[styles]]></category>
		<category><![CDATA[templates]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://www.designersilverlight.com/?p=732</guid>
		<description><![CDATA[I&#8217;ve started a series of posts on Silverlight and other topics over at the Veracity Solutions Blog. Veracity Solutions is the software consulting firm where I work and we&#8217;ve decided that it would be a good idea to do some sponsored posting there. Blog posts up so far are: Styling a ComboBox in Silverlight 3 [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve started a series of posts on Silverlight and other topics over at the Veracity Solutions Blog.</p>
<p>Veracity Solutions is the software consulting firm where I work and we&#8217;ve decided that it would be a good idea to do some sponsored posting there.</p>
<p>Blog posts up so far are:</p>
<ul>
<li><a href="http://blogs.veracitysolutions.com/styling-a-combobox-in-silverlight-3/">Styling a ComboBox in Silverlight 3</a></li>
<li><a href="http://blogs.veracitysolutions.com/how-to-wrap-text-in-a-silverlight-3-combobox/">How To Wrap Text In a Silverlight 3 ComboBox</a></li>
<li><a href="http://blogs.veracitysolutions.com/how-to-get-a-silverlight-3-autocompletebox-to-show-sample-data-in-blend-3/">How To Get a Silverlight 3 AutoCompleteBox to Show Sample Data In Blend 3</a></li>
<li><a href="http://blogs.veracitysolutions.com/how-to-create-a-listbox-with-checkboxes-or-radiobuttons-in-silverlight-3/">How To Create A ListBox with CheckBoxes (or RadioButtons) In Silverlight 3</a></li>
</ul>
<p>More are coming, including tutorials on behaviors and paths.</p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http://www.designersilverlight.com/2009/08/19/new-silverlight-posts-at-veracity-blogs/&amp;layout=standard&amp;show_faces=1&amp;width=450&amp;action=like&amp;colorscheme=light&amp;font=" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:25px"></iframe><div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=New+Silverlight+Posts+at+Veracity+Blogs+http%3A%2F%2Fdesignersilverlight.com%2F%3Fp%3D732" title="Post to Twitter"><img class="nothumb" src="http://www.designersilverlight.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter.png" alt="Post to Twitter" /></a> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=New+Silverlight+Posts+at+Veracity+Blogs+http%3A%2F%2Fdesignersilverlight.com%2F%3Fp%3D732" title="Post to Twitter">Tweet This Post</a></p></div>]]></content:encoded>
			<wfw:commentRss>http://www.designersilverlight.com/2009/08/19/new-silverlight-posts-at-veracity-blogs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adventures with JSON and Silverlight (and the New York Times)</title>
		<link>http://www.designersilverlight.com/2009/02/25/adventures-with-json-and-silverlight-and-the-new-york-times/</link>
		<comments>http://www.designersilverlight.com/2009/02/25/adventures-with-json-and-silverlight-and-the-new-york-times/#comments</comments>
		<pubDate>Thu, 26 Feb 2009 00:17:56 +0000</pubDate>
		<dc:creator>Matthias Shapiro</dc:creator>
				<category><![CDATA[Advanced Tutorial]]></category>
		<category><![CDATA[Blend]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[How To...]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[New York Times API]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://www.designersilverlight.com/2009/02/25/adventures-with-json-and-silverlight-and-the-new-york-times/</guid>
		<description><![CDATA[Summary: In this post, I walk through the basics of using Silverlight to query the New York Times Article API and display the results of the query. You can see the final result below. You can also download source code for this project here. JSON/Silverlight/New York Times project files Huge thanks to Josh Holmes, whose [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Summary:</strong> In this post, I walk through the basics of using Silverlight to query the New York Times Article API and display the results of the query. You can see the final result below.</p>
<p><iframe src="http://silverlight.services.live.com/invoke/77530/JSON_NYT_Tutorial_Part_1/iframe.html" scrolling="no" frameborder="0" style="width:400px; height:300px"></iframe></p>
<p>You can also download source code for this project here.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/02/jsonnewyorktimestutorial_part_1.zip">JSON/Silverlight/New York Times project files </a></p>
<p>Huge thanks to Josh Holmes, whose <a href="http://www.joshholmes.com/blog/2009/01/20/PlayingWithJSON.aspx">JSON/Silverlight tutorial</a> was the base of much of this project.</p>
<p>This project is somewhat code-intensive (and kind of long, expect 30-60 minutes), so if you just want the utility provided here without any of the work, you can skip over to <a href="http://www.designersilverlight.com/2009/02/25/using-silverlight-to-display-json-data-collected-from-the-new-york-times-api/">my post on displaying the results</a>, which is strictly a Blend tutorial.</p>
<p>However, I recommend walking through this one since it will help get you to a point of pulling real data from an API, which I’ve found to be a wonderful help as I practice putting together data-based designs. One of the things I&#8217;ve been wanting to be able to do as a designer is to explore a data set easily and quickly so that I can have data to play with in my interfaces. I found exactly what I wanted in the New York Times API, but then I found out I had to learn JSON.</p>
<p>&#8220;Oh great,&#8221; I said to myself, &#8220;another technology for me to learn.&#8221; But it turned out that I didn&#8217;t actually have to learn that much, because Visual Studio does nearly all the work for me.</p>
<h3>Super Quick Introduction to JSON</h3>
<p>If you&#8217;re not interested and you want to get to the business of grabbing New York Times data, you can skip it . It is helpful, but not strictly necessary.</p>
<p>JSON stands for JavaScript Object Notation and is basically just a really handy way to pass data along in a web service. It is very simple&#8230; within a set of curly brackets, you will have name/value pairs separated by a colon with each piece of data.<br />
Example:<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;FirstName : &#8220;Matthias&#8221;,<br />
&nbsp;&nbsp;&nbsp;&nbsp;LastName : &#8220;Shapiro&#8221;,<br />
&nbsp;&nbsp;&nbsp;&nbsp;Blog : &#8220;Designer WPF&#8221;<br />
}</p>
<p>This is a JSON object. Arrays are created by using square brackets and JSON obejcts can be placed into a Javascript var. These things are not really related in anyway, but putting them in the same sentence allows me to only write one more example instead of two</p>
<p>Example:<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;FirstName: “Matthias”,<br />
&nbsp;&nbsp;&nbsp;&nbsp;LastName: “Shapiro”,<br />
&nbsp;&nbsp;&nbsp;&nbsp;Siblings : [<br />
&nbsp;&nbsp;&nbsp;&nbsp;{Name : “Abby”},<br />
&nbsp;&nbsp;&nbsp;&nbsp;{Name : “Joel”},<br />
&nbsp;&nbsp;&nbsp;&nbsp;{Name : “Anna”},<br />
&nbsp;&nbsp;&nbsp;&nbsp;{Name : “John”},<br />
&nbsp;&nbsp;&nbsp;&nbsp;{Name : “Nate”}<br />
&nbsp;&nbsp;&nbsp;&nbsp;]<br />
}</p>
<p>And there we have the basics of JSON.</p>
<h3>End of Super Quick Introduction to JSON</h3>
<p>What is so awesome about JSON and Silverlight is that Visual Studio 2008 has a set of JSON-friendly classes that make working with JSON a breeze. Which is really handy because the New York Times, which is a dream come true for the new data-gatherer, delivers JSON results. So let’s walk through making a call to the New York Times Article API, handling the data we get back, and putting it into a Listbox for viewing.</p>
<p>First, <a href="http://developer.nytimes.com/">go to the New York Times Developer site</a>, log in (or register) and get your API key.</p>
<p>Next, start a new Silverlight project in Visual Studio 2008. I named mine “JSONNewYorkTimesTutorial”.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/02/clip-image0019.png"><img style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" title="clip_image001[9]" src="http://www.designersilverlight.com/wp-content/uploads/2009/02/clip-image0019-thumb.png" border="0" alt="clip_image001[9]" width="659" height="424" /></a></p>
<p>Open your new project in Blend, pull up Page.xaml and add a TextBlock, ListBox, a TextBox and a Button. Name the ListBox “ResultsDisplay” and the TextBox “SearchText”.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/02/clip-image00113.png"><img style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" title="clip_image001[13]" src="http://www.designersilverlight.com/wp-content/uploads/2009/02/clip-image00113-thumb.png" border="0" alt="clip_image001[13]" width="299" height="105" /></a></p>
<p>Now, my Page.xaml looks like this.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/02/clip-image00111.png"><img style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" title="clip_image001[11]" src="http://www.designersilverlight.com/wp-content/uploads/2009/02/clip-image00111-thumb.png" border="0" alt="clip_image001[11]" width="457" height="346" /></a></p>
<p>OK… now let’s go to the code-behind for our project go to the event section of the button in Blend and type “PerformQuery” into the “Click” event.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/02/clip-image00115.png"><img style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" title="clip_image001[15]" src="http://www.designersilverlight.com/wp-content/uploads/2009/02/clip-image00115-thumb.png" border="0" alt="clip_image001[15]" width="264" height="104" /></a></p>
<p>This will automatically insert the necessary code into the code-behind, so pull up Visual Studio. Before we implement a call to the NYT API, lets add some useful stuff. If you have not yet gone to <a href="http://developer.nytimes.com/apps/register?authChecked=1">get your Developer key, do so now</a>.</p>
<p><span style="color: blue">private string </span>myApiKey;<br />
<span style="color: blue">private </span><span style="color: #2b91af">WebClient </span>callNYT;</p>
<p><span style="color: blue">public </span>Page()<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;InitializeComponent();<br />
&nbsp;&nbsp;&nbsp;&nbsp;myApiKey = <span style="color: #a31515">&#8220;&amp;api-key=(put your api key here. No, you may not have mine)&#8221;</span>;<br />
&nbsp;&nbsp;&nbsp;&nbsp;callNYT = <span style="color: blue">new </span><span style="color: #2b91af">WebClient</span>();<br />
&nbsp;&nbsp;&nbsp;&nbsp;callNYT.OpenReadCompleted += <span style="color: blue">new </span><span style="color: #2b91af">OpenReadCompletedEventHandler</span>(callNYT_OpenReadCompleted);<br />
}</p>
<p>In the code above, we’ve created a string that we can use to apply our unique NYT API key and we’ve created an instance of WebClient to call and receive information from the NYT API. When an object from the NYT API has been received , it will call the OpenReadCompleted event, which will be handled by our callNYT_OpenReadCompleted method.</p>
<p>(By the way, if you’re not getting the proper intellisense for the “Web Client” part, add “<span style="color: blue">using </span>System.Net;&#8221; to the top of your file.)</p>
<p>Now on to our button method. There are tons of things we can add to our query to find the exact information we want. But in the interest of simplicity, this post will deal only with a simple text search. To that end, let’s go to our PerformQuery method and turn it into this:</p>
<p><span style="color: blue">private void </span>PerformQuery(<span style="color: blue">object </span>sender, <span style="color: #2b91af">RoutedEventArgs </span>e)<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">string </span>NYTQueryBase = <span style="color: #a31515">&#8220;http://api.nytimes.com/svc/search/v1/article&#8221;</span>;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">string </span>SearchTerm = <span style="color: #a31515">&#8220;?query=&#8221;</span>+ SearchText.Text;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af">Uri </span>queryUri = <span style="color: blue">new </span><span style="color: #2b91af">Uri</span>(NYTQueryBase + SearchTerm + myApiKey);</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;callNYT.OpenReadAsync(queryUri);<br />
}</p>
<p>We’ve done two things here. The first is that we built our search query using the query base, the query string and our API key. That was simple enough.</p>
<p>Next, we’ve going to use the WebClient we created to call our new query. When the query has been completed, our program will run the callNYT_OpenReadCompleted method with its result, which will be a JSON object constructed by the NYT servers. We will get back a JSON object with the following:</p>
<ul>
<li>offset – We will get 10 results per page. The offset tells us which page of the results we’re on. The default is 0, which gives us results 0 – 9.</li>
<li>tokens – this is a array of our search terms.</li>
<li>total – this is an integer indicating of how many results there were for our search.</li>
<li>results – this is an array of results with the following format
<ul>
<li>body – a portion of the beginning of the article</li>
<li>byline – the article byline, usually including the author name</li>
<li>date – the date the article appeared, in a “yyyymmdd” format. For example, today would be “20090225”.</li>
<li>title – the article title</li>
<li>url – a url link to the article</li>
</ul>
</li>
</ul>
<p>A quick note: The NYT API is extremely flexible and we can actually define how we want our results to come back. This is just the default result format for the purposes of demonstration.</p>
<p>Before we handle this object, we want to create a class for the results. Right click on your project and go to “Add –&gt; Class…”. Name your new class “NYTResult.cs” and make sure it looks like this:</p>
<p><span style="color: blue">public class </span><span style="color: #2b91af">NYTResult </span>{<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">public string </span>Body { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">public string </span>Byline { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">public </span><span style="color: #2b91af">DateTime </span>Date { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">public string </span>Title { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">public </span><span style="color: #2b91af">Uri </span>Url { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }<br />
}</p>
<p>I added the following method to the class to handle the date conversion from the NYT format to a .NET DateTime object.</p>
<p><span style="color: blue">public </span><span style="color: #2b91af">DateTime </span>formattedDateTime(<span style="color: blue">string </span>NYT_Time)<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue"><span style="color: #444444;"> </span>int </span>year = <span style="color: #2b91af">Convert</span>.ToInt32(NYT_Time.Substring(0, 4));<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">int </span>month = <span style="color: #2b91af">Convert</span>.ToInt32(NYT_Time.Substring(4, 2));<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">int </span>day = <span style="color: #2b91af">Convert</span>.ToInt32(NYT_Time.Substring(6, 2));<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af">DateTime </span>finalDateTime = <span style="color: blue">new </span><span style="color: #2b91af">DateTime</span>(year, month, day);<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">return </span>finalDateTime;<br />
}</p>
<p>OK… now we’re really ready to handle the JSON object. Right click on the references in your project and select “Add Reference…”</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/02/clip-image001.png"><img style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" title="clip_image001" src="http://www.designersilverlight.com/wp-content/uploads/2009/02/clip-image001-thumb.png" border="0" alt="clip_image001" width="287" height="81" /></a></p>
<p>In  your “Add References” box, select “System.Json” and click “OK”.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/02/clip-image0015.png"><img style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" title="clip_image001[5]" src="http://www.designersilverlight.com/wp-content/uploads/2009/02/clip-image0015-thumb.png" border="0" alt="clip_image001[5]" width="481" height="408" /></a></p>
<p>Add “<span style="color: blue">using </span>System.Json;” to the references in your Page.xaml.cs file. And, just for good measure, add “<span style="color: blue">using </span>System.Collections.ObjectModel;” as well.</p>
<p>Go to the callNYT_OpenReadCompleted method and enter the following. I’ve tried to comment the code so that I don’t need to further explain it. Side note: I’m not always the best at understanding what is self-explanatory and what I need to elaborate on. If there are any additional questions, post them in the comments and I’ll answer as quickly as I can.</p>
<p><span style="color: blue">void </span>callNYT_OpenReadCompleted(<span style="color: blue">object </span>sender, <span style="color: #2b91af">OpenReadCompletedEventArgs </span>e)<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: green">//grab our result and make a JSON Object out of it<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span> <span style="color: green">//    then extract the results array from that object<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span> <span style="color: #2b91af">JsonObject </span>completeResult = (<span style="color: #2b91af">JsonObject</span>)<span style="color: #2b91af">JsonObject</span>.Load(e.Result);<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af">JsonArray </span>resultsArray = (<span style="color: #2b91af">JsonArray</span>)completeResult[<span style="color: #a31515">"results"</span>];</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: green">//an observable collection to hold the data and attach it to our ListBox<br />
</span><br />
&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">ObservableCollection</span>&lt;<span style="color: #2b91af">NYTResult</span>&gt; resultCollection = <span style="color: blue">new </span><span style="color: #2b91af">ObservableCollection</span>&lt;<span style="color: #2b91af">NYTResult</span>&gt;();</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: green">//iterate through the results and transfer the data from a<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span> <span style="color: green">//   JSON object into our nice pretty .NET object<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span> <span style="color: blue">foreach </span>(<span style="color: #2b91af">JsonObject </span>NYTRawResult <span style="color: blue">in </span>resultsArray)<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af">NYTResult </span>singleResult = <span style="color: blue">new </span><span style="color: #2b91af">NYTResult</span>();</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: green">//don&#8217;t forget to check your results&#8230; an article might not have a<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span> <span style="color: green">//  byline or a link<br />
</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if </span>(NYTRawResult.Keys.Contains(<span style="color: #a31515">&#8220;body&#8221;</span>))<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;singleResult.Body = NYTRawResult[<span style="color: #a31515">"body"</span>];<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">if</span>(NYTRawResult.Keys.Contains(<span style="color: #a31515">&#8220;byline&#8221;</span>))<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;singleResult.Byline = NYTRawResult[<span style="color: #a31515">"byline"</span>];<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">if </span>(NYTRawResult.Keys.Contains(<span style="color: #a31515">&#8220;date&#8221;</span>))<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;singleResult.Date = singleResult.formattedDateTime(NYTRawResult[<span style="color: #a31515">"date"</span>]);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">if </span>(NYTRawResult.Keys.Contains(<span style="color: #a31515">&#8220;title&#8221;</span>))<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;singleResult.Title = NYTRawResult[<span style="color: #a31515">"title"</span>];<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">if </span>(NYTRawResult.Keys.Contains(<span style="color: #a31515">&#8220;url&#8221;</span>))<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;singleResult.Url = <span style="color: blue">new </span><span style="color: #2b91af">Uri</span>(NYTRawResult[<span style="color: #a31515">"url"</span>]);</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: green">//add our new result to the collection<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span> resultCollection.Add(singleResult);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: green">//assign the result as the source for our ListBox<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span> ResultsDisplay.ItemsSource = resultCollection;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: green">//take the overall article count and display it<br />
</span>&nbsp;&nbsp;&nbsp;&nbsp; resultCount.Text = <span style="color: #a31515">&#8220;Number of articles: &#8221; </span>+ completeResult[<span style="color: #a31515">"total"</span>].ToString();<br />
}</p>
<p>Now, we can run our project. Type something into the TextBox and hit the button and we get this:<br />
<a href="http://www.designersilverlight.com/wp-content/uploads/2009/02/clip-image0017.png"><img style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" title="clip_image001[7]" src="http://www.designersilverlight.com/wp-content/uploads/2009/02/clip-image0017-thumb.png" border="0" alt="clip_image001[7]" width="404" height="303" /></a></p>
<p>Not exactly the most readable thing ever. So let’s add the following to the ListBox XAML:</p>
<p><span style="color: red">DisplayMemberPath</span><span style="color: blue">=&#8221;Title&#8221;</span></p>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Now we get something a little more like this (go ahead and give it a whirl):</p>
<p><iframe src="http://silverlight.services.live.com/invoke/77530/JSON_NYT_Tutorial_Part_1/iframe.html" scrolling="no" frameborder="0" style="width:400px; height:300px"></iframe></p>
<p>Much better. Remember, this is a simple query, so it’s only looking for items that have that word in the article… it might not be in the title.</p>
<p><a href="http://www.designersilverlight.com/2009/02/25/using-silverlight-to-display-json-data-collected-from-the-new-york-times-api/">My next post builds on this one</a> and I walk through building a more useful display for our results. It will be far less code intensive and far more designer centric.</p>
<p>I’ve made the source available for this project. I’ve taken out my NYT API key, so it will not run unless you get your own and put it in.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/02/jsonnewyorktimestutorial_part_1.zip">JSON &#8211; Silverlight &#8211; New York Times Tutorial Part 1 project files</a></p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http://www.designersilverlight.com/2009/02/25/adventures-with-json-and-silverlight-and-the-new-york-times/&amp;layout=standard&amp;show_faces=1&amp;width=450&amp;action=like&amp;colorscheme=light&amp;font=" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:25px"></iframe><div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Adventures+with+JSON+and+Silverlight+%28and+the+New+York+Times%29+http%3A%2F%2Fdesignersilverlight.com%2F%3Fp%3D550" title="Post to Twitter"><img class="nothumb" src="http://www.designersilverlight.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter.png" alt="Post to Twitter" /></a> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Adventures+with+JSON+and+Silverlight+%28and+the+New+York+Times%29+http%3A%2F%2Fdesignersilverlight.com%2F%3Fp%3D550" title="Post to Twitter">Tweet This Post</a></p></div>]]></content:encoded>
			<wfw:commentRss>http://www.designersilverlight.com/2009/02/25/adventures-with-json-and-silverlight-and-the-new-york-times/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Mask Animations (Clipping Path Animations) In Silverlight Without a Line of Code</title>
		<link>http://www.designersilverlight.com/2009/01/15/mask-animations-clipping-path-animations-in-silverlight-without-a-line-of-code/</link>
		<comments>http://www.designersilverlight.com/2009/01/15/mask-animations-clipping-path-animations-in-silverlight-without-a-line-of-code/#comments</comments>
		<pubDate>Thu, 15 Jan 2009 17:14:46 +0000</pubDate>
		<dc:creator>Matthias Shapiro</dc:creator>
				<category><![CDATA[Blend]]></category>
		<category><![CDATA[How To...]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[without a line of code]]></category>

		<guid isPermaLink="false">http://www.designersilverlight.com/?p=472</guid>
		<description><![CDATA[Last night I submitted my MIX 10K Challenge piece, so I’m just killing time while I wait for it to get accepted. Please keep me in mind if you plan on voting. OK… I’ve been excited about this for some time now. I found out (almost completely by accident) that you can animate a mask [...]]]></description>
			<content:encoded><![CDATA[<p>Last night I submitted my MIX 10K Challenge piece, so I’m just killing time while I wait for it to get accepted. Please keep me in mind if you plan on voting.</p>
<p>OK… I’ve been excited about this for some time now. I found out (almost completely by accident) that you can animate a mask (also known as a clipping path) in Silverlight without writing code or even typing (much). This is exciting because I think it’s something that might interest our Flash friends who, late at night and in the privacy of their own homes, take a peek to see if Silverlight is worth looking into. When I worked with Flash, mask animations were my bread and butter and they seemed so hard to do in Silverlight.</p>
<p>But they’re not.</p>
<p>First open up your project in Blend. (I always create my project in Visual Studio and then open it in Blend because visual Studio has so much better project debugging and makes testing a breeze.)</p>
<p>Find the item you want to clip (in my case this is an image) and select the pen tool from the toolbar.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/01/clip-image001.png"><img style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" title="clip_image001" src="http://www.designersilverlight.com/wp-content/uploads/2009/01/clip-image001-thumb.png" border="0" alt="clip_image001" width="44" height="46" /></a></p>
<p>And start drawing.I’m a poor artist, so the star I drew looks pretty bad. If you need to change any of the points, use the direct selection tool (the light arrow, second from the top).</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/01/clip-image0014.png"><img style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" title="clip_image001[4]" src="http://www.designersilverlight.com/wp-content/uploads/2009/01/clip-image0014-thumb.png" border="0" alt="clip_image001[4]" width="42" height="114" /></a></p>
<p>OK… now the fun part. Add a storyboard to your project</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/01/clip-image0016.png"><img style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" title="clip_image001[6]" src="http://www.designersilverlight.com/wp-content/uploads/2009/01/clip-image0016-thumb.png" border="0" alt="clip_image001[6]" width="332" height="141" /></a></p>
<p>and you can animate the clipping mask using all the normal animation tools. Use the direct selection tool above to animate the vertices. You’ll get something like this:</p>
<p><iframe src="http://silverlight.services.live.com/invoke/77530/pathAnimation_Part1/iframe.html" scrolling="no" frameborder="0" style="width:400px; height:300px"></iframe></p>
<p>Now… if you’re not interested in the details, just skip ahead a little bit. Something important happened the moment we started animating the path. Normally, path data is kept in a single string format that looks something like this:</p>
<p><span style="color: red;">Data</span><span style="color: blue;">=&#8221;M159.67175,104.26108 L177.91812,28.328932 L195.51648,104.43327 L255.0802,102.6104 L206.86984,151.82758 L225.8029,226.56477 L179.0616,179.17046 L129.73396,229.29906 L147.97842,150.63879 L98.650803,101.53297 z&#8221;</span></p>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>This is actually a “<a href="http://msdn.microsoft.com/en-us/library/cc189041(VS.95).aspx">powerful and complex mini-language</a> that that you can use to describe geometric paths in XAML.” Microsoft uses it by default to handle path data. But it doesn’t work very well for animating paths, so the second you animate a single vertex in your path, it changes the format you see above to the format you see below:</p>
<p><span style="color: blue;">&lt;</span><span style="color: #a31515;">Path.Data</span><span style="color: blue;">&gt;<br />
</span><span style="color: blue;">&lt;</span><span style="color: #a31515;">PathGeometry</span><span style="color: blue;">&gt;<br />
&lt;</span><span style="color: #a31515;">PathFigure </span><span style="color: red;">IsClosed</span><span style="color: blue;">=&#8221;True&#8221; </span><span style="color: red;">StartPoint</span><span style="color: blue;">=&#8221;91.0527648925781,84.0121078491211&#8243;&gt;<br />
&lt;</span><span style="color: #a31515;">LineSegment </span><span style="color: red;">Point</span><span style="color: blue;">=&#8221;118.057907104492,0.549586236476898&#8243;/&gt;<br />
&lt;</span><span style="color: #a31515;">LineSegment </span><span style="color: red;">Point</span><span style="color: blue;">=&#8221;144.103973388672,84.2013778686523&#8243;/&gt;<br />
&lt;</span><span style="color: #a31515;">LineSegment </span><span style="color: red;">Point</span><span style="color: blue;">=&#8221;232.259979248047,82.1977386474609&#8243;/&gt;<br />
&lt;</span><span style="color: #a31515;">LineSegment </span><span style="color: red;">Point</span><span style="color: blue;">=&#8221;160.907287597656,136.2958984375&#8243;/&gt;<br />
&lt;</span><span style="color: #a31515;">LineSegment </span><span style="color: red;">Point</span><span style="color: blue;">=&#8221;188.928756713867,218.444961547852&#8243;/&gt;<br />
&lt;</span><span style="color: #a31515;">LineSegment </span><span style="color: red;">Point</span><span style="color: blue;">=&#8221;119.750289916992,166.350433349609&#8243;/&gt;<br />
&lt;</span><span style="color: #a31515;">LineSegment </span><span style="color: red;">Point</span><span style="color: blue;">=&#8221;46.7439804077148,221.450408935547&#8243;/&gt;<br />
&lt;</span><span style="color: #a31515;">LineSegment </span><span style="color: red;">Point</span><span style="color: blue;">=&#8221;73.7462997436523,134.989212036133&#8243;/&gt;<br />
&lt;</span><span style="color: #a31515;">LineSegment </span><span style="color: red;">Point</span><span style="color: blue;">=&#8221;0.740016639232636,81.0134506225586&#8243;/&gt;<br />
&lt;/</span><span style="color: #a31515;">PathFigure</span><span style="color: blue;">&gt;<br />
&lt;/</span><span style="color: #a31515;">PathGeometry</span><span style="color: blue;">&gt;<br />
&lt;/</span><span style="color: #a31515;">Path.Data</span><span style="color: blue;">&gt;</span></p>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Same data, but only this latter format is appropriate for animation. Remember this, it will come in handy in a second.</p>
<p>OK… now right click on your path and go to “Path –&gt; Make Clipping Path”</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/01/clip-image00110.png"><img style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" title="clip_image001[10]" src="http://www.designersilverlight.com/wp-content/uploads/2009/01/clip-image00110-thumb.png" border="0" alt="clip_image001[10]" width="367" height="52" /></a></p>
<p>You’ll get a dialog pop-up that will ask you which item in the visual tree you would like to clip. I chose my image, but it will work with anything.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/01/clip-image00112.png"><img style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" title="clip_image001[12]" src="http://www.designersilverlight.com/wp-content/uploads/2009/01/clip-image00112-thumb.png" border="0" alt="clip_image001[12]" width="297" height="158" /></a></p>
<p>And… poof! You have clipping animation.</p>
<p><iframe src="http://silverlight.services.live.com/invoke/77530/PathAnimation_Part2/iframe.html" scrolling="no" frameborder="0" style="width:400px; height:300px"></iframe></p>
<p>LIMITATIONS: Here are the limitations to this method:</p>
<ol>
<li><strong>You need to animate before you make your path into a clipping path. – </strong>Remember that little bit above about the data string format vs. path geometry? If you don’t animate before hand, Blend will convert all your beautiful line segments into the un-animate-able data string. Animating the path tells Blend to leave your formatting alone.</li>
<li><strong>Once you make your path a clipping  path, animation gets <em>a lot</em> harder</strong> – Basically, you can change the time by dragging the key frames around and change the easing. Any other alterations require entering values by hand. So do all your animation first before setting the path. The easiest work around to this is to copy and paste your path out of the Control.Clip section and back into your main XAML and tweak animation from there. But your best option is to just take the time to make your clipping animation perfect before you make it a clipping path.</li>
</ol>
<iframe src="http://www.facebook.com/plugins/like.php?href=http://www.designersilverlight.com/2009/01/15/mask-animations-clipping-path-animations-in-silverlight-without-a-line-of-code/&amp;layout=standard&amp;show_faces=1&amp;width=450&amp;action=like&amp;colorscheme=light&amp;font=" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:25px"></iframe><div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Mask+Animations+%28Clipping+Path+Animations%29+In+Silverlight+Without+a+Line+of+Code+http%3A%2F%2Fdesignersilverlight.com%2F%3Fp%3D472" title="Post to Twitter"><img class="nothumb" src="http://www.designersilverlight.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter.png" alt="Post to Twitter" /></a> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Mask+Animations+%28Clipping+Path+Animations%29+In+Silverlight+Without+a+Line+of+Code+http%3A%2F%2Fdesignersilverlight.com%2F%3Fp%3D472" title="Post to Twitter">Tweet This Post</a></p></div>]]></content:encoded>
			<wfw:commentRss>http://www.designersilverlight.com/2009/01/15/mask-animations-clipping-path-animations-in-silverlight-without-a-line-of-code/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How To Give a Silverlight 2 Gadget a Transparent Background</title>
		<link>http://www.designersilverlight.com/2008/12/03/how-to-give-a-silverlight-2-gadget-a-transparent-background/</link>
		<comments>http://www.designersilverlight.com/2008/12/03/how-to-give-a-silverlight-2-gadget-a-transparent-background/#comments</comments>
		<pubDate>Thu, 04 Dec 2008 06:06:22 +0000</pubDate>
		<dc:creator>Matthias Shapiro</dc:creator>
				<category><![CDATA[gadget]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Silverlight 2]]></category>
		<category><![CDATA[transparent background]]></category>
		<category><![CDATA[vista]]></category>

		<guid isPermaLink="false">http://www.designersilverlight.com/2008/12/03/how-to-give-a-silverlight-2-gadget-a-transparent-background/</guid>
		<description><![CDATA[Warning: This is a poor solution if your gadget has any level of dragging interaction in it. The &#8220;windowless=true&#8221; step will make it so that any dragging interaction will be interrupted by the gadget, which will interpret such a move as an attempt to drag the gadget off the side bar. Giving your Silverlight gadget [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Warning:</strong> This is a poor solution if your gadget has any level of dragging interaction in it. The &#8220;windowless=true&#8221; step will make it so that any dragging interaction will be interrupted by the gadget, which will interpret such a move as an attempt to drag the gadget off the side bar.</p>
<p>Giving your Silverlight gadget a transparent background is actually quite simple. First, follow the directions from <a href="http://blogs.msdn.com/szurgot/archive/2008/11/03/silverlight-gadget-version-of-snowflake-app.aspx">Chris Szurgot&#8217;s post on Silverlight gadgets</a>. No point in duplicating his work.</p>
<p>When you&#8217;re done, create a transparent png file. <a href="http://www.designersilverlight.com/wp-content/uploads/2008/12/transparent.png">Or download this one.</a></p>
<p>Now, head over to your html file. Get rid of all CSS background information. Add the following just below the body tag:</p>
<p><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;"> </span></span></p>
<p><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">&lt;</span><span style="font-size: x-small; color: #a31515;"><span style="font-size: x-small; color: #a31515;">g</span></span><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">:</span></span><span style="font-size: x-small; color: #a31515;"><span style="font-size: x-small; color: #a31515;">background</span></span><span style="font-size: x-small;"> </span><span style="font-size: x-small; color: #ff0000;"><span style="font-size: x-small; color: #ff0000;">id</span></span><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">=&#8221;background&#8221;</span></span><span style="font-size: x-small;"><br />
</span><span style="font-size: x-small; color: #ff0000;"><span style="font-size: x-small; color: #ff0000;">src</span></span><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">=&#8221;transparent.png&#8221;<br />
</span></span><span style="font-size: x-small; color: #ff0000;"><span style="font-size: x-small; color: #ff0000;">style</span></span><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">=&#8221;</span></span><span style="font-size: x-small; color: #ff0000;"><span style="font-size: x-small; color: #ff0000;">position</span></span><span style="font-size: x-small;">:</span><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">absolute</span></span><span style="font-size: x-small;">;</span><span style="font-size: x-small; color: #ff0000;"><span style="font-size: x-small; color: #ff0000;">top</span></span><span style="font-size: x-small;">:</span><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">0</span></span><span style="font-size: x-small;">;</span><span style="font-size: x-small; color: #ff0000;"><span style="font-size: x-small; color: #ff0000;">left</span></span><span style="font-size: x-small;">:</span><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">0</span></span><span style="font-size: x-small;">;</span><span style="font-size: x-small; color: #ff0000;"><span style="font-size: x-small; color: #ff0000;">z-index</span></span><span style="font-size: x-small;">:</span><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">-999</span></span><span style="font-size: x-small;">;</span><span style="font-size: x-small; color: #ff0000;"><span style="font-size: x-small; color: #ff0000;">no</span></span><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">=repeat</span></span><span style="font-size: x-small;">;</span><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">&#8220;</span></span><span style="font-size: x-small;"> </span><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">/&gt;</span></span></span></p>
<p>Finally, change the following params in the Silverlight object host.</p>
<p><span style="color: blue;">&lt;</span><span style="color: #a31515;">param </span><span style="color: red;">name</span><span style="color: blue;">=&#8221;background&#8221; </span><span style="color: red;">value</span><span style="color: blue;">=&#8221;transparent&#8221; /&gt;<br />
&lt;</span><span style="color: #a31515;">param </span><span style="color: red;">name</span><span style="color: blue;">=&#8221;windowless&#8221; </span><span style="color: red;">value</span><span style="color: blue;">=&#8221;true&#8221; /&gt;</span></p>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>And that’s it.</p>
<p><strong>Additional Warning:</strong> It looks like there is no solution yet for the pink halo around any anti-aliased pixels in a Silverlight gadget.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2008/12/clip-image001.png"><img style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" title="clip_image001" src="http://www.designersilverlight.com/wp-content/uploads/2008/12/clip-image001-thumb.png" border="0" alt="clip_image001" width="138" height="213" /></a></p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2008/12/clip-image0014.png"><img style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" title="clip_image001[4]" src="http://www.designersilverlight.com/wp-content/uploads/2008/12/clip-image0014-thumb.png" border="0" alt="clip_image001[4]" width="81" height="83" /></a></p>
<p>This is a problem that transparent Silverlight gadgets have had for some time now. If that doesn’t bother you then you’re all set.</p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http://www.designersilverlight.com/2008/12/03/how-to-give-a-silverlight-2-gadget-a-transparent-background/&amp;layout=standard&amp;show_faces=1&amp;width=450&amp;action=like&amp;colorscheme=light&amp;font=" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:25px"></iframe><div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=How+To+Give+a+Silverlight+2+Gadget+a+Transparent+Background+http%3A%2F%2Fdesignersilverlight.com%2F%3Fp%3D417" title="Post to Twitter"><img class="nothumb" src="http://www.designersilverlight.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter.png" alt="Post to Twitter" /></a> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=How+To+Give+a+Silverlight+2+Gadget+a+Transparent+Background+http%3A%2F%2Fdesignersilverlight.com%2F%3Fp%3D417" title="Post to Twitter">Tweet This Post</a></p></div>]]></content:encoded>
			<wfw:commentRss>http://www.designersilverlight.com/2008/12/03/how-to-give-a-silverlight-2-gadget-a-transparent-background/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

