Archive for March, 2008
Silverlight how to: RSS feed stored as JSON in Isolated Storage
This week I was reading two of my favorite Microsoft Evangelist blogs; Kirk Allen Evans’ and Tim Heuer and was inspired to build upon two of their posts. Kirk wrote a great entry on Creating a JSON Service with WebGet and WCF 3.5 and Tim on
Calling web services with Silverlight 2. I thought it would be interesting to take the concepts covered in both of these posts and put them together into my own how to. In my sample I read an RSS feed into a JSON string, stores it in Isolated storage, and displays it in a ListBox. Once displayed, I will check every 30 seconds to see if the RSS feed has changed. Out-of-the-box my solution is not practical, but illustrates a flexible technique for caching a serialized collection of data in the event the service is unavailable.
In the first part of my sample I check to see if I have a cache of RSS in Isolated Storage. If so, I use the “DataContractJsonSerializer” class to de-serialize the JSON array. Once de-serialized, I can bind it to my ListBox control in “Page.xaml”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | void Page_Loaded(object sender, RoutedEventArgs e)
{
RssService.RssItem[] items |
After we update our presentation of RSS, I will insert a animating UserControl to be use when we go to retrieve additional RSS items. This retrieval from our WCF 3.5 service will be executed every 10 seconds when our “DispatcherTimer” fires.
1 2 3 4 5 6 7 8 9 10 11 12 | //insert a UserControl that animates a spinner when we are retrieving
//an update of RSS data
_indicator.HorizontalAlignment = HorizontalAlignment.Center |
Upon successful retrieval from our WCF service, we will update our isolated storage by serializing our array of RssItems[] into JSON, and writing that text to a file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | void wcf_GetFeedsCompleted(object sender,
IsolatedStorage.RssService.GetFeedsCompletedEventArgs e)
{
try
{
RssService.RssItem[] items = e.Result as RssService.RssItem[] |
Thanks again to Tim and Kirk for the majority of my example.
Code: IsolatedStorage.zip
Security lapse exposes Facebook photos
Check out this article on Cnn that describes the recent privacy breach that caught Facebook with there pants down. A security lapse allowed strangers to access photos of Facebook member and their friends, even if the privacy settings were set to restrict who could see the photos.
With over 67 million active users, Facebook will always be suspect to hackers looking for a way to circumvent its user privacy. For reasons beyond my comprehension, our generation of Internet users have little trepidation about what information we place in the hands of others. When Facebook made the decision to open ups its platform they put upon them self a huge risk that someday the breach will be very serious. Privacy is not a setting, but a caution we should all take before we choose to upload a private photo or other personal information to the 5th most-trafficked website in the world .
Mobile PC adds Adobe’s Flash Lite
In a move to make the Windows Mobile PC more usable, Microsoft announced that mobile phones running there operating system will include both Adobe’s Flash Lite and Adobe Reader LE. Microsoft has over 11 million phones running Mobile PC and hopes to grow that to over 20 million by next year. This move will certainly put pressure on Apple to support Flash and hopefuly Silverlight on the increasingly popular iPhone. Even with this move I believe that the Mobile PC operating system is virtually unusable and needs a complete overhaul if it hopes to gain or even maintain its existing market share.
Silverlight how to: On-demand assembly deployment
One of the cool new features of Silverlight 2 is the concept of on-demand deployment of assemblies. In most interactive development there is a desire to minimize the amount of data a user has to download to get your RIA up and running. The application package (XAP) size can grow quite quickly if one tries to include all of resources required to run the entire application. In Silverlight 2 we have the concept of a XAP file or compressed package (zip file). By default, the XAP contains an application assembly and any Silverlight assemblies that your application is dependent on (System.Xml.Linq, etc). In Silverlight 2 we have the ability to generate more than one XAP file giving us the opportunity to deploy additional assemblies on demand, asynchronously to the initial payload.
To leverage this technology one has to understand the concept of “AssemblyParts” and how reflection is used to extract an assembly from a downloaded XAP. The following example will show how one can code this functionality in Silverlight 2 Beta 1.
The first step in downloading an additional assembly is to go and get the XAP package. Note the use of a BaseAddress. In my example the base address is http://localhost/MultipleXap_Web/.
1 2 3 4 5 | Uri addressUri = new Uri(_baseUri, "ClientBin/MultipleXap1.xap") |
Once downloaded, the “OpenReadCompleted” event will fire and one can extract the assembly from the XAP. I have chosen to factor into a method the logic used extract the Assembly from the compressed zip file. In the event that we had multiple assemblies we could use Linq to navigate the xml of AppManifest and programmatically load each assembly.
1 2 3 4 5 6 7 8 9 10 | public Assembly LoadAssemblyFromXap(string relativeUriString, Stream xapPackageStream)
{
Uri uri = new Uri(relativeUriString, UriKind.Relative) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | void wcXap1_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if ((e.Error == null) && (e.Cancelled == false))
{
// Convert the downloaded stream into an assembly
Assembly a = LoadAssemblyFromXap("MultipleXap1.dll", e.Result) |
Once the assembly has been extracted, we can create an instance of the selected UserControl by using the “CreateInstance” method on the Assembly object. Based on Microsoft’s documentation, we should have been able to add a project reference to the on-demand XAP, within our default Silverlight Application. This would give us access to that second assemblies types. We can make the reference and the project compiles, but throws an exception at runtime. CreateInstance is not a preferred approach since it drastically limits how we can leverage the strong typing of objects instantiated from this reflected assembly.
Code: MultipleXap.zip
Silverlight Rendering Defect
During the process of writing our Silverlight 2 game “Tunnel Trouble” we stumbled upon a fairly significant rendering defect in the Beta 1 version of the Silverlight 2 plugin. This defect appears to affect any UIElement (Rectangles, Paths, Images) that has been transformed on its X or Y coordinate. If the object breaks the bounds of the plugin, Silverlight chooses not to render the object that has been flipped or rotated.
Note: There appears to be an inconsistent workaround. If you set the width and height of the <object> tag to a fixed size (i.e. 250×205) and not 100%x100% the defect appears to resolve itself.
Code: RenderDefect.zip
Silverlight how to: Deep Zoom
At Mix08 one of the components of Silverlight 2 I was most intrigued by was the multi-scale image technology called “Deep Zoom” (formerly seadragon). The demos we saw were quite compelling. Immediately I thought of areas where our clients could improve their users interactive experiences through this new technology.
Below you will find my first attempt at using the technology. It took about 10 min to create and showcases an interesting example of composition using deep zoom.
(Hint: Look for the plane in the right part of the sky)
Attached you will find my sample project. It is pretty big in size with the full collection of image tiles. All of the code you see in the page.xaml.cs was taken from Scott Hanselmans blog on Deep Zoom where he leveraged the Mousewheel class written by
Pete Blois
The image of the flags I took last summer and in the original there is a small speck in the sky where I believe a plane was. I found a image of an F16 on the internet and used Photoshop to remove its original background. I replaced it with a sample of the color of the flag photos sky. Using Deep Zoom Composer I placed the new F16 photo over the sky speck and exported the collection.
Code: DeepZoomFlag.zip
(Note: to get the sample to work you will need to edit the absolute path to the collection in the page.xaml file)
Silverlight how to: Procedural Animation
Whenever possible developers should encourage designers to maintain all of their Silverlight storyboards within XAML. This allows the designers the optimum environment to use a tool like Blend to tweak the animation timing, easing and any relevant Key Splines. That said, there are situations where a developer may create a UserControl that requires full control over the format of the animation. Defining this storyboard procedurally, allows the developer a more consistent level of control and allows an easier approach to manipulating properties within this storyboard.
The following will show a simple example of how one could include a “DoubleAnimationUsingKeyFrames” that allows the TranslateTransform’s “X” property to be manipulated each time the animation is played.
The first step in defining our animating UserControl is to attach a “Loaded” event handler within our constructor. This allows us to have the XAML parsed by the UserControls base class, yet inject a procedural storyboard into the UserControls resources.
1 2 3 4 5 | public Box()
{
InitializeComponent() |
Our storyboard will include a “DoubleAnimationUsingKeyFrames”, “SplineDoubleKeyFrame” with a corresponding “KeySpline”. I have chosen to refactor this logic into a method. In the event that we wanted to manipulate both the “X” and the “Y” TranslateTransform property, this method could be called twice passing both “X” and “Y” as the last argument.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | private static SplineDoubleKeyFrame CreateDoubleAnimation(ref Storyboard sb,
TranslateTransform translation, string property)
{
DoubleAnimationUsingKeyFrames da = new DoubleAnimationUsingKeyFrames() |
Within our Page_Loaded handler we define the transformation group for this control, call our helper function and insert the storyboard into the UserControls grid (LayoutRoot) resource collection. In this example, we are only manipulating the 2D, X/Y coordinate system so we only need to include the “TranslateTransform” within the transform group. If Scale was being translated as well, we would include a “ScaleTransform” object within the group.
1 2 3 4 5 6 7 8 9 10 11 12 | void Page_Loaded(object sender, RoutedEventArgs e)
{
//define the Render Transformation for this control
TranslateTransform translation = new TranslateTransform() |
Lastly, I created a public method in the UserControl that sets the “X” and starts the animation. This method can be called repeatedly from the location that is instantiating this UserControl.
1 2 3 4 5 6 | public bool Animate(int x)
{
_sdkfX.SetValue(SplineDoubleKeyFrame.ValueProperty, x) |
Code: Animation.zip
My Mix08 Experience
Even though Mix08 is now only a distant memory, the inspiration I leave with will last for quite a while. There is something quite powerful about being around thousands of professionals that are as passionate and enthusiastic about the future of interactive development. For months now I have had the special opportunity to be working close with Microsoft and the there development of the Silverlight 2 Beta. We have seen first hand the power and potential of the platform. It was great to share that excitement with the rest of the development community.
In the weeks to come, I look forward to sharing our discoveries as we learned how to leverage the Silverlight 2 framework to build our most recent casual game. Look for posts on the techniques and methodologies we found most effective.
Interactive Design Firm Powers Casual games
A month ago Microsoft selected Terralever to conduct a in depth Case Study on our use of Silverlight 2 for creating a new Miniclip.com game Tunnel Trouble. This process was really exciting for Terralever. I feel Microsoft did a great job of capturing the value we find in Silverlight and how this powerful framework allowed us to create a compelling casual game in less then 5 weeks.
![]()


