<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Brett Robinson's Blog : Silverlight</title><link>http://blogs.msdn.com/brettrobinson/archive/tags/Silverlight/default.aspx</link><description>Tags: Silverlight</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Be a Silverlight DJ - with my Silverlight Alpha 1.1 sample</title><link>http://blogs.msdn.com/brettrobinson/archive/2007/05/23/be-a-silverlight-dj-with-my-silverlight-alpha-1-1-sample.aspx</link><pubDate>Thu, 24 May 2007 01:27:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2824907</guid><dc:creator>BrettRobinson</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/brettrobinson/comments/2824907.aspx</comments><wfw:commentRss>http://blogs.msdn.com/brettrobinson/commentrss.aspx?PostID=2824907</wfw:commentRss><wfw:comment>http://blogs.msdn.com/brettrobinson/rsscomments.aspx?PostID=2824907</wfw:comment><description>&lt;P&gt;I wanted to get my hands dirty with Silverlight Alpha 1.1 and so I decided to make a DJ table with two turntables where you can match two beats in different songs.&amp;nbsp; You can click the button on the bottom right of&amp;nbsp;each turntable to get the music started.&amp;nbsp; You can pause it or rewind it by clicking and dragging backwards.&amp;nbsp; There's also a fader so that you can cue up the music.&amp;nbsp; There isn't a scratch feature nor is there a fast forward feature.&amp;nbsp; &lt;/P&gt;
&lt;P align=center&gt;&lt;A class="" title="Silverlight DJ" href="http://brob.members.winisp.net/SilverlightDJ/TestPage.html" target=_blank mce_href="http://brob.members.winisp.net/SilverlightDJ/TestPage.html"&gt;&lt;IMG title="Silverlight DJ" style="WIDTH: 429px; HEIGHT: 271px" height=271 alt="Silverlight DJ" src="http://brob.members.winisp.net/SilverlightDJ.jpg" width=429 align=middle border=0 mce_src="http://brob.members.winisp.net/SilverlightDJ.jpg"&gt;&lt;/A&gt;&lt;/P&gt;I didn't write one line of javascript in this application and the entire thing is controlled by managed C# code.&amp;nbsp; Have fun!&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=2824907" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/brettrobinson/archive/tags/Silverlight/default.aspx">Silverlight</category></item><item><title>Componentizing Silverlight - what I consider best practices</title><link>http://blogs.msdn.com/brettrobinson/archive/2007/04/18/componentizing-silverlight-what-i-consider-best-practices.aspx</link><pubDate>Wed, 18 Apr 2007 20:02:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2177876</guid><dc:creator>BrettRobinson</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/brettrobinson/comments/2177876.aspx</comments><wfw:commentRss>http://blogs.msdn.com/brettrobinson/commentrss.aspx?PostID=2177876</wfw:commentRss><wfw:comment>http://blogs.msdn.com/brettrobinson/rsscomments.aspx?PostID=2177876</wfw:comment><description>&lt;P&gt;I was looking at a &lt;A class="" href="http://blogs.msdn.com/mharsh/archive/2007/04/06/gavin-gear-s-xaml-javascript-string-converter.aspx" target=_blank mce_href="http://blogs.msdn.com/mharsh/archive/2007/04/06/gavin-gear-s-xaml-javascript-string-converter.aspx"&gt;post&lt;/A&gt;&amp;nbsp;on Mike Harsh's blog about a utility that turns XAML into javascript and&amp;nbsp;I didn't entirely agree with&amp;nbsp;what this utility accomplished.&amp;nbsp; A while back I &lt;A class="" href="http://blogs.msdn.com/brettrobinson/archive/2007/01/25/wpf-e-asp-net-ajax-and-webservices-hooking-it-all-together.aspx" target=_blank mce_href="http://blogs.msdn.com/brettrobinson/archive/2007/01/25/wpf-e-asp-net-ajax-and-webservices-hooking-it-all-together.aspx"&gt;posted an entry&lt;/A&gt; about hooking in web services, Silverlight (WPF/E) and ASP.NET Ajax.&amp;nbsp;&amp;nbsp;Although I did use string concatenation in the sample, I was posting a simple example just to demonstrate how to piece those three technologies together.&amp;nbsp; After creating large solutions with multiple Silverlight components and really digging deep into the technology, I recommend a different way of building out applications.&amp;nbsp; Here's what I found to work best and a way to really manage all the pieces easily.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;Lets say you are building a list in XAML and what gets populated in that list comes from some data&amp;nbsp;source (text file, web service, etc.).&amp;nbsp; Of course you could do this in javascript and create xaml in the javascript and add objects to the canvas.&amp;nbsp; But, from what I've found, this gets hard to manage and your js files become enormous.&amp;nbsp; What I did was create a XAML template and give your dynamic&amp;nbsp;values based on&amp;nbsp;format items, like so:&lt;/P&gt;&lt;FONT color=#0000ff size=2&gt;
&lt;P&gt;&amp;lt;&lt;/FONT&gt;&lt;FONT color=#800000 size=2&gt;Image&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt; &lt;/FONT&gt;&lt;FONT color=#ff0000 size=2&gt;Height&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;=&lt;/FONT&gt;&lt;FONT size=2&gt;"&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;26&lt;/FONT&gt;&lt;FONT size=2&gt;"&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt; &lt;/FONT&gt;&lt;FONT color=#ff0000 size=2&gt;Width&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;=&lt;/FONT&gt;&lt;FONT size=2&gt;"&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;32&lt;/FONT&gt;&lt;FONT size=2&gt;"&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt; &lt;/FONT&gt;&lt;FONT color=#ff0000 size=2&gt;Source&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;=&lt;/FONT&gt;&lt;FONT size=2&gt;"&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;{2}&lt;/FONT&gt;&lt;FONT size=2&gt;"&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt; &lt;/FONT&gt;&lt;FONT color=#ff0000 size=2&gt;Canvas.Top&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;=&lt;/FONT&gt;&lt;FONT size=2&gt;"&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;{3}&lt;/FONT&gt;&lt;FONT size=2&gt;"&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt; &lt;/FONT&gt;&lt;FONT color=#ff0000 size=2&gt;Canvas.Left&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;=&lt;/FONT&gt;&lt;FONT size=2&gt;"&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;6&lt;/FONT&gt;&lt;FONT size=2&gt;"&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;/&amp;gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;Now, the file that&amp;nbsp;has the above&amp;nbsp;line of code in has a lot of different attributes that are set based on code.&amp;nbsp; So, lets see how to&amp;nbsp;hook it up.&amp;nbsp;&amp;nbsp;You have created elements that have the values being defined as a format item and you need to add those values.&amp;nbsp; How I did this was to create an object that has a property in it that represents the fragment, like this:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;struct&lt;/SPAN&gt; &lt;SPAN style="COLOR: teal"&gt;Item&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;{&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; XamlFragment;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;}&lt;/P&gt;&lt;/DIV&gt;&lt;!--EndFragment--&gt;
&lt;P&gt;Then set the property to the correct data item by&amp;nbsp;streaming in the XAML template and replacing the format item with the correct value, like so:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; itemTemplate = &lt;SPAN style="COLOR: teal"&gt;HttpContext&lt;/SPAN&gt;.Current.Server.MapPath(&lt;SPAN style="COLOR: maroon"&gt;"~/Xaml/Item.xaml"&lt;/SPAN&gt;);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; xamlText = &lt;SPAN style="COLOR: teal"&gt;String&lt;/SPAN&gt;.Format(&lt;SPAN style="COLOR: teal"&gt;File&lt;/SPAN&gt;.ReadAllText(itemTemplate)&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; , myfirstvalue&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; , textTop&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; , item.TypeIcon);&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;
&lt;P style="MARGIN: 0px" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;
&lt;DIV style="FONT-FAMILY: Arial"&gt;I built an ArrayList of Item structs&amp;nbsp;in C#&amp;nbsp;and then&amp;nbsp;have a method in javascript to call a C# web service method that returns the arraylist.&amp;nbsp; In javascript, have your function that calls the web service method point to a function that takes in the results as a parameter, like so:&lt;/DIV&gt;
&lt;DIV style="FONT-FAMILY: Arial"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV style="FONT-FAMILY: Arial"&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;
&lt;P style="MARGIN: 0px"&gt;MyService.GetItems(OnSucceeded); &lt;SPAN style="COLOR: green"&gt;//call web service&lt;/SPAN&gt;&lt;/P&gt;&lt;/DIV&gt;&lt;!--EndFragment--&gt;&lt;/DIV&gt;
&lt;DIV style="FONT-FAMILY: Arial"&gt;&lt;!--EndFragment--&gt;&lt;/DIV&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;function&lt;/SPAN&gt; OnSucceeded(result)&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;{&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; _results = result;&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Array.forEach(_results,AddXamlResultElement);&lt;/P&gt;&lt;/DIV&gt;&lt;!--EndFragment--&gt;
&lt;P style="MARGIN: 0px"&gt;}&lt;/P&gt;
&lt;P style="MARGIN: 0px" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;
&lt;DIV style="FONT-FAMILY: Arial"&gt;Then use the Array.foreach method, and point it to the function to add the xaml fragment to the canvas, like so:&lt;/DIV&gt;
&lt;P style="MARGIN: 0px" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;function&lt;/SPAN&gt; AddXamlResultElement(element,index)&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;{&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; properCanvas.children.Add(canvas.CreateFromXaml(element.XamlFragment));&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;}&lt;/P&gt;&lt;/DIV&gt;&lt;!--EndFragment--&gt;&lt;/DIV&gt;&lt;!--EndFragment--&gt;
&lt;DIV&gt;&lt;/DIV&gt;&lt;!--EndFragment--&gt;
&lt;DIV&gt;&lt;/DIV&gt;&lt;!--EndFragment--&gt;
&lt;P&gt;Hopefully this will help you build projects and manage them that could potentially be very large.&amp;nbsp; As you can see, it cuts down on the amount of javascript you have to use to keep concatenating the strings and adding them.&amp;nbsp; Plus its a lot easier to maintain, speaking from experience.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=2177876" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/brettrobinson/archive/tags/XAML/default.aspx">XAML</category><category domain="http://blogs.msdn.com/brettrobinson/archive/tags/WPF_2F00_E/default.aspx">WPF/E</category><category domain="http://blogs.msdn.com/brettrobinson/archive/tags/Web+Services/default.aspx">Web Services</category><category domain="http://blogs.msdn.com/brettrobinson/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://blogs.msdn.com/brettrobinson/archive/tags/Silverlight/default.aspx">Silverlight</category></item></channel></rss>