Monday, September 28, 2009

Lambda expression in Silverlight 3 Databinding

First of all let me thank to M. Orçun Topdağı for pointing out a useful cs file in the samples provided by Microsoft.You may get those samples in the below location.

[Install drive]:\Program Files\Microsoft Visual Studio 9.0\Samples

The file is Dynamic.cs which provides a bunch of functionality to work more with Lambda expressions.That sample was written for .Net 3.5 and I was afraid whether I can use that in my Application since I am working in Silverlight 3. But I was able to use the same classes for Silverlight and a sample which takes Lambda Expression from xaml and executes through converter is attached with this post.

How to specify a Lambda expression in XAML data-binding

I am using the ConverterParameter to get the lambda expression.The no of parameters to Lambda expression is limited because we can pass only one value to the converter.
<UserControl x:Class="SL_LambdaInConverter.MainPage"
        <local:LambdaConverter x:Key="lambda" />
    <Grid x:Name="LayoutRoot">
            Text="{Binding Converter={StaticResource lambda},ConverterParameter='dt=>dt.ToString()'}" />
In the above sample I have set DateTime.Today as DataContext of the UserControl in the constructor.So it will take that value into the converter.Here is the converter code.

Option Strict On
Imports System.Windows.Data
Imports System.Linq.Expressions
Imports System.Linq.Dynamic
Public Class LambdaConverter
    Implements IValueConverter
    Dim _delegate As [Delegate]
    Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
        If Me._delegate = Nothing Then
            ConstructOperation(value, targetType, CType(parameter, String))
        End If
        Return Me._delegate.DynamicInvoke(value)
    End Function
    Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
        Throw New InvalidOperationException("Convert back operation is not supported")
    End Function
    Sub ConstructOperation(ByVal value As Object, ByVal t As Type, ByVal lambdaString As String)
        Dim opi As Integer = lambdaString.IndexOf("=>")
        If (opi < 0) Then Throw New Exception("No lambda operator =>")
        Dim param As String = lambdaString.Substring(0, opi)
        Dim body As String = lambdaString.Substring(opi + 2)
        Dim p As ParameterExpression = Expression.Parameter(value.GetType(), param)
        Dim lambda As LambdaExpression = DynamicExpression.ParseLambda(New ParameterExpression() {p}, t, body, value)
        Me._delegate = lambda.Compile()
    End Sub
End Class

Now you might wonder where is the System.Linq.Dynamic ? Here comes the importance of Dynamic.cs which I had mentioned at the beginning.You may find all those classes there.

Download and enjoy the sample here.

Sunday, September 27, 2009

Silverlight 3 in Windows Mobile 7

Any developer who takes programming as a passion, would always like to see his application running in different plat forms.The code need to be same ,but runs every where.

.Net & Java did this by introducing an intermediate language to which every code compiles to and before running, it convert in to the native code.

When I entered into Silverlight it felt like I am writing code only for Browser.Prism has introduced a solution where we can use same code base for Silverlight as well as WPF.Now we have got much more support in extending our Silverlight code to different plat forms. 

Yes.The support is in Mobiles. Microsoft has announced that their next Mobile operating system Windows Mobile 7 Will support Silverlight 3.They are planning to release Windows Mobile 7 in 2010.So plan your application accordingly,because it has to run in mobiles by next year.

Some links

Tuesday, September 22, 2009

The need for ModuleBase class in Prism

When I started a sample application in Prism, I came to some situations where I need ModuleManager and all.After a small google I came to know that if we specify some arguments in the constructor of Modules we could get lot of useful objects such as ModuleManager,RegionManager etc...We can hold them for future uses.

Instead of having properties in each module to hold these objects, it is always better to have an abstract base class which holds all these objects.Then I did a trial an error method to find out what are all the objects which we can get and it came around 4 objects.

  • IModuleManager
  • IRegionManager
  • IUnityContainer
  • IRegionViewRegistry

And the base class implementation is as follows.
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Practices.Composite.Modularity;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Unity;
namespace Prism.Core
    public abstract class ModuleBase : IModule
        IModuleManager _modulemanager;
        public IModuleManager ModuleManager
            get { return _modulemanager; }
            set { _modulemanager = value; }
        IRegionViewRegistry _regionViewRegistry;
        public IRegionViewRegistry RegionViewRegistry
            get { return _regionViewRegistry; }
            set { _regionViewRegistry = value; }
        IRegionManager _regionManager;
        public IRegionManager RegionManager
            get { return _regionManager; }
            set { _regionManager = value; }
        IUnityContainer _container;
        public IUnityContainer Container
            get { return _container; }
            set { _container = value; }
        public ModuleBase(IModuleManager moduleManager, 
            IRegionManager regionManager,
            IUnityContainer container, 
            IRegionViewRegistry reg)
            ModuleManager = moduleManager;
            RegionManager = regionManager;
            Container = container;
            this.RegionViewRegistry = reg;
        #region IModule Members
        public abstract void Initialize();
The same thing is applicable in the case of Views too.

Saturday, September 19, 2009

Is it DRM which we really need in Silverlight 4?

From some news sites and also from Microsoft's press pass I could know that there will be DRM support in Silverlight 4.DRM is a nice feature which I would like to be in Silverlight.

But I wonder why they are not telling about other features, which as a developer we are waiting for from the Silverlight 1.0 days itself.I came from the WPF environment so I will really look for the below features first instead of DRM.

  1. Full implementation of Commands.Silverlight 3 doesn't have Button.Command property.Do we need to relay on interactions provided by blend 3?
  2. FindAncestor for RelativeSource binding
  3. InputBindings in UIElement.
  4. Attached events.
  5. Triggers or equivalent implementation in DataTemplates
  6. Option to create custom mark-up extensions.
  7. XPS support.
  8. Printing.
  9. More bindings in WCF such as TCP.

A huge wish list is here in Silverlight forum.Hope Microsoft will address our these small issues along with rocking new features.

Wednesday, September 2, 2009

Specify Modules in XAML

When I first started PRISM my thought was how to specify the modules through a xaml file or an xml fille.The existing code is written as like something hard coded in the Bootstraper.cs.

protected override Microsoft.Practices.Composite.Modularity.IModuleCatalog GetModuleCatalog()
    var catalog = new ModuleCatalog();
    return catalog;

We can easily setup an xml file and create module objects using reflection.But I was sure, there will be an implementation of same somewhere in the framework.

After spending some time, I got a new way to implement my scenario.Why don't I have a new xaml file derived from ModuleCatalog and make the entries in xaml ? Yes that did the trick.

I added a new file which derives from ModuleCatelog and added my module entries.The xaml looks as below.

        ModuleType="Students.StudentsListModule, Students, Version=" />
        ModuleType="Students.MarkModule, Students, Version=" />

Make sure that the code behind class should inherit from ModuleCatalog.

Now you can Rewrite the Bootstraper.GetModuleCatalog() as follows

protected override Microsoft.Practices.Composite.Modularity.IModuleCatalog GetModuleCatalog()
    return  new StudentsModuleCatalog();
This makes the code more neat and we can easily add or remove modules.

Tuesday, September 1, 2009

Microsoft SSRS viewer in Silverlight

Some time back we had a scenario of showing reports in Silverlight.Our first though was to create reports in ASP.Net and show that as popup window from Silverlight.But in the research we found out a great tool from Perpetuum softwares which renders the SSRS report inside Silverlight.

Main advantages were it capability to leverage features of Silverlight such as Vector graphics.We can zoom the report without losing it's clarity.

Online demo is available here .You need to install only Silverlight 3 in order view the report.

Basically it works by adding a component at the server side which makes the report compatible with their Silverlight viewer.Currently it supports SQL Server Reporting Services 2005 only.

Good work Perpetuum guys.Keep it up.