Tuesday, March 24, 2009

Silverlight 3 Beta Offline or Out of Browser applications

Now Microsoft added support for Silverlight applications to run out of browser.Earlier it was accomplished by a tool called Desklighter released by Identitymine.

Details on how to make a Silverlight 3 application offline is available here by Pete Brown.

Portability of offline Silverlight applications.

This post describes how can we make the Silverlight applications portable.ie how to copy offline Silverlight application from one system to another.

Normally when we select the Install button on the silverlight context menu the system copies the required files (mainly .xap)into a cache folder locatable at

[Drive]:\Users\[username]\AppData\LocalLow\Microsoft\Silverlight\Offline

The folder name will be localhost.<serial no of offline app in the system> provided the Silverlight application which you saved is running in the localhost.

For eg if you are creating your first offline application the folder name will be localhost.0.

Once you locate this offline folder you can just copy that folder to the new machine.To run the application you need to copy the Shortcut file also which was created in the desktop or in the start menu.In case you don’t have the short cut file you may run by giving the following command in console.

[Install drive]:\Program Files\Microsoft Silverlight\3.0.40307.0\sllauncher.exe" localhost.0

To run another offline application just change the number (here 0) at the end.

NB: This post is written based on Silverlight 3 Beta.Things may change in actual release.

Monday, March 23, 2009

Silverlight 3 Beta Pixel Shader Effects

This is another nice feature included in Silverlight 3.Pixel shaders are programs which operate at pixel level.ie all pixel values(value of A,R,G,B) are passed through the associated pixel shaders before displaying in the screen.Then the pixel shaders will do some calculations and change the pixel value.
For example if we have applied a pixel shader called “Negative” to a StackPanel,then all pixels in the area of that StackPanel will go through Negative pixel shader and it Negate the pixel values by decrementing from 255(255-A,255-R,255-G,255-B).
This is the basic idea of pixel shaders and the Silverlight 3 itself contains 2 Pixel shader effects as of now.They are DropShadow and Blur.

Applying pixel shader Effect

Now the UIElement class in the Silverlight has got a new dependency property called Effect which is the entry point to the pixel shaders.We can directly apply the Effect to this property.Since all the controls and visual elements are inherited from UIElement,they also have the pixel shader support.
Applying DropShadow pixel shader effect through XAML to a Button

<Button HorizontalAlignment="Center" VerticalAlignment="Center" Content="Joy's Button">            <Button.Effect>                <DropShadowEffect Color="Black"                                  ShadowDepth="5" />            </Button.Effect></Button>
C# to Apply DropShadowEffect


private void Button_Click(object sender, RoutedEventArgs e){    btn.Effect = new DropShadowEffect() { Color=Colors.Black,ShadowDepth=5};}



Grouping pixel shader effects

The Silverlight 3 currently doesn’t support grouping of Effects.If we want to apply Blur effect and DropShadow effect to a Button we have to go for a workaround.
  • Wrap the control using a Border.
  • Apply the new effect to the border.

<Border>            <Button HorizontalAlignment="Center"                    VerticalAlignment="Center"                    Content="Joy's Button">                <Button.Effect>                    <DropShadowEffect Color="Black"                                      ShadowDepth="5" />                </Button.Effect>            </Button>            <Border.Effect>                <BlurEffect Radius="5" />            </Border.Effect></Border>



Blur effect has been applied to the Border in order to get more than one effect on the Button.Writing custom pixel shader effects will be discussed later.