Tuesday, December 22, 2009

Silverlight 4 Beta slides & code from Web-Fu! event

Yesterday I had a great time at Microsoft's Web-Fu! event in Belgrade, Serbia, and at the same time it was an opportunity to talk about what's new in Silverlight 4 Beta. During the talk, I demoed a couple of interesting things:
I also showed a little demo application which demonstrates some of the new features coming in Silverlight 4:
  • New RichTextArea control
  • DataGrid improvements
  • New databinding improvements
  • Out of browser improvements: notification (toast), HtmlBrowser control
  • Making Silverlight speech using COM Interop
  • etc.
Here are Silverlight 4 Beta slides, and the code for demo application. Hope you'll find it useful.

Wednesday, October 28, 2009

Silverlight slides & demo application from MsForge CodeCamp

I had a great pleasure to attend MsForge Code Camp in Belgrade (Serbia) and meet lots of cool IT folks. At the same time, I had an opportunity to talk about Silverlight & .NET RIA Services and demonstrate 2 simple Silverlight poll applications: one for managing polls and one for voting.
RIA Poll application for managing polls is available here (username: codecamp, pasword: codecamp).

Example of RIA Poll voting is available here.

Code can be found here - .NET Ria Service July Preview is required and web.config connection string needs to point to database which is in RiaPoll.Web/Data folder.
Presentation slides are available online here.

Thursday, September 24, 2009

Multi-threading options in Rich Internet Applications

A non-responsive user interface due to heavy computational work can lead to very poor user experience. In order to avoid freezing user interface, use of multi-threading is a must in any complex Rich Internet Application. Here are some pointers on multi-threading options in existing technologies and those that will come in the (near) future:


Silverlight provides a wide range of threading features and it's all done in a familiar .NET way. MSDN documentation about threading in Silverlight is extensive and you can read it here, and there are lots of examples out there about using threads in Silverlight application. You can use Thread, ThreadPool, BackgroundWorker, Timers, Monitors, Wait Handles, and more . Sure, lots of options here.


To my surprise, I've found that JavaFX script as of version 1.2 (released June 2009) is single-threaded, and all background work must be done in Java code as stated here by key members of JavaFx team at Sun Microsystems. More in their blog post:

There is one major caveat that I need to make clear. As of JavaFX 1.2, JavaFX Script is single threaded. All background work must be done in Java code. The JavaTaskBase abstract class is the base class intended to be used by all background threading implementations in JavaFX 1.2. Unfortunately, the hookup between JavaTaskBase and RunnableFuture didn’t come out right in 1.2 and you’re left with very little guidance as to how to do this appropriately.

The key thing is to understand that nothing should touch JavaFX variables from a background thread. So the idiom to use is to have the JavaTaskBase subclass to have a Java language peer which does the background work. The JavaTaskBase subclass would register a listener on the peer, and the peer would communicate back to the JavaTaskBase through that listener interface. The JavaTaskBase subclass would then pump events from the peer back onto the JavaFX thread (i.e. event thread) using FX.deferAction.
More information about background tasks in JavaFX using Java code is available here.


There isn't much you can do in Flash, since Flash is single-threaded application.
Alternatives include voting for missing threading feature here (if you are a part of Adobe community), or you can use Pixel Bender for calculating purposes, which is not a part of Flash and is programmed using GLSL syntax. There are more Flash alternatives for threading here, but that just doesn't look viable to me (and surely not just me!).

Html 5 and JavaScript

Html and JavaScript are not categorized as RIA technologies, but that could be changed in the near future. There are some sources here that mention things like multi-threaded JavaScript in Html 5. Draft recommendation for "Web Workers" is here and it's strongly influenced by a work of Google Gears. So we will have to wait until Html 5 is implemented in all major browsers. In the meantime you can try some nasty tricks for "simulating" multi-threading in JavaScript by James Edwards here.


Microsoft Silverlight provides great multi-threading features in today's RIA with lots of options.
Multi-threading in JavaFx 1.2 (released in June 2009) has changed so that JavaFx Script is single-threaded and therefore all threading operations are handled by Java code since JavaFx is fully integrated with Java Runtime Environment (JRE). Some multi-threading improvements (a hookup between JavaTaskBase and FutureRunnable) are coming in next JavaFx releases.
Communicating using only primitive types with Pixel Bender (which is not a part of Flash) doing background work written in GLSL syntax represents a very poor multi-threading choice. Moreover, missing multi-threading feature unfortunately doesn't have highest priority in Flash community.
Web Worker as a part of Html 5 which is currently in draft recommendation form have a long way to go. A lot of existing Ajax and Flash application could also take advantage of Html 5 Web Worker once it comes out, so it is also an option to watch for.

Monday, September 14, 2009

Silverlight SaveFileDialog doesn't work in Opera

Generally, most Silverlight 3 applications work ok in Opera. There were some problems with windowless mode in Opera using Silverlight 2, but it appears that it's been fixed in Silverlight 3.
Nevertheless, Opera is still not on the list of Microsoft Silverlight 3 officially supported browsers, and today I've found one more reason why. There isn't much documentation about this stuff, but I've got an exception when Silverlight 3 SaveFileDialog is used in Opera 10 (build 1750). The thrown exception is:
"Dialogs must be user-initiated at System.Windows.Controls.SaveFileDialog.ShowDialog()..."
which appears in other browsers, too, but only if you try to invoke save file dialog avoiding button click event. I haven't found any workaround for this, so my code looks like this. I would like to know if there is a better way to solve this problem in order to use Silverlight applications in Opera. Any thoughts?

Monday, August 24, 2009

Prime Timetable in Summer Silverlight Coding Competition

I've decided to submit Prime Timetable to the Summer Silverlight Coding Competition hosted by ComponentArt. If you find Prime Timetable interesting, you can vote for it here. In order to vote you will need ComponentArt account (just click on "Log in to cast your vote" > Create account, if you don't have it).
It's my pleasure to be able to take part in this competition. The motivation for entering the coding competition came from these:
  • Not many other Silverlight line of business (LOB) application have been submitted yet, and I wonder why? They don't like to compete or what?
  • I am ambitious by nature and like to compete :).
So, go ahead and vote and may the best Silverlight app win!

Monday, August 17, 2009

Snapshot of scrollable content using Silverlight 3 WriteableBitmap

Recently, I've been working on exporting part of XAML tree to Jpeg image using Silverlight 3 WriteableBitmap and it all went pretty well until I put lots of stuff into exporting content container. Here are some of the problems I've encountered:
  • Scrollbar appears in the snapshot.
  • Non-visible content doesn't appear in the snapshot even when its size increased.
Here is my approach for solving these problems:

1. Prior to creating WriteableBitmap adjust user interface:
  • Adjust the size (width and height) of scrolling content so that the scrollbar doesn't appear.
  • Adjust the size of content containers including plugin size (!), so that you don't have cut offs in exported image.
  • If there are some UIElements you don't want exported, set them as invisible.
2. Create WriteableBitmap.
3. Revert user interface changes.

The part of code that solves scrolling problems in my scenario is published here. Feel free to adjust it to your needs and let me know what you think. I wonder how this could be accomplished in Out Of Browser (OOP) scenario. How can the platform window be resized to get all the scrollable content without cut offs, and does the cut-off problem even exist? I might even try that one day ;).
Example of generating image from (scrollable) content is available online here, just choose Timetable > Export to Jpeg. That's it.

Tuesday, August 11, 2009

JavaScript location.href instead of Silverlight HtmlPage.Window.Navigate

MSDN documentation for Silverlight HtmlPage.Window.Navigate method is pretty basic, here is the link. Summary for the method is: "Opens the specified page in the specified browser instance."
But, behind the scene, if you look at the code in Reflector, HtmlPage.Window.Navigate method invokes JavaScript "open" method. That's all right if you want to call popups, but what if you want to navigate to html page in the same window without calling the popup?
I've encountered following problems with using HtmlPage.Window.Navigate:
  • It looks like there are some problems with the browser's cache when Navigate method is used, as described by Kevin Hazzard here. Furthermore, in case you use Asp.Net postbacks, the popup page can lead to corrupted viewstate in some scenarios.
  • HtmlPage.Window.Navigate method's just stopped working in recent builds of Google Chrome browser, probably because of some internal pop-up blocker code.
Therefore, I've decided to use JavaScript location.href instead of HtmlPage.Window.Navigate when target navigation page is set to "_self". Code snippet which looks like a possible solution is published on Rick Strahl's great CodePaste tool here.

I would like to hear your thoughts and experiences on this.