Wednesday, 2 June 2010

Silverlight page dragging

Silverlight page need a container to show. As a container we use grid or border basically. But grid has predefined fixed position. So .xaml page is not drag able if we use grid. Border also have problem for dragging. To use .xaml page as like pop up window and to easily drag thought the pages we can use canvas as container.

In .xaml file paste above line. In .cs file paste following line.

canvas.Children.Add(object);

To make position of object we can define a margin for it.

object.Margin = new Thickness(200, 150, 2, 2);

Tuesday, 1 June 2010

Edit Template of silverlight controls

We can make suitable change of any control property by editing its template. Most effecting thing is style change. You can change your control as you wish. Without editing it is hardly possible to change control as we needed. Suppose we want to change button style or child window cross button it is never possible without editing their template. Expression blend gives facility to edit template. Below are the steps required to change or editing template.

1. Open project in expression blend.
2. Select control to edit.
3. Open Object & Timeline window. Right click on control -> Edit Template -> Edit a copy -> Give a name(Optional) -> Click ok.

Sunday, 30 May 2010

Configurable WCF Service url in Silverlight project side

WCF service gets url and binding properties from “ServiceReferences.ClientConfig “ file in silverlight project (client side). When WCF service initialize, it gets all property from this file. There is no problem reading data from that file. But when web site get deployment url are change. So manually you have to change url of WCF services from that file. <endpoint/> tag address property hold WCF url address.

To get rid of this hassle, we can configure it to read the url from web.config file. We can do followings things.

Create a class as an example ServiceFactory. All the initialization related things which is take place in “ServiceReferences.ClientConfig” file, have to be ServiceFactory class. See the sample code.

BasicHttpBinding _BasicHttpBinding;

      EndpointAddress _EndpointAddress;

public ServiceFactory()

        {

    _BasicHttpBinding = new System.ServiceModel.BasicHttpBinding();

            _BasicHttpBinding.MaxReceivedMessageSize = 2147483647;

            _BasicHttpBinding.MaxBufferSize = 2147483647;

            _BasicHttpBinding.OpenTimeout = new TimeSpan(0, 20, 0);

            _BasicHttpBinding.CloseTimeout = new TimeSpan(0, 20, 0);

            _BasicHttpBinding.ReceiveTimeout = new TimeSpan(0, 20, 0);

            _BasicHttpBinding.SendTimeout = new TimeSpan(0, 20, 0);           

Now when we need to make the instance of WCF service we can create a method in ServiceFactory class. So we can bind BasicHttpBinding and EndpointAddress class property within WCF service instance. Look at sample method below.

public PCBuilderServiceClient GetPCBuilderServiceClient()

        {

            _EndpointAddress = new System.ServiceModel.EndpointAddress(ServiceUrlManager.GetValue(ConstantClass.SERVICE_SERVER_URI) + ServiceUrlManager.GetValue(ConstantClass.PC_BUILDER_SERVICE));

                _PCBuilderServiceClient = new PCBuilderServiceClient(_BasicHttpBinding, _EndpointAddress);

            return _PCBuilderServiceClient;

      }

Here PCBuilderServiceClient is the service name. _EndpointAddress object is binding service url. There are two things one is the server url and another is the exact location and name of the WCF service file in server. So both need to cocate to get the exact location of the WCF service. Initially we have kept thos information inside IDictionary object during starting of the web site. Now just fetching the value from IDictionary by key.

Saturday, 29 May 2010

Silverlight project initial configuration

There are several steps to make initial configuration of any silverlight project. We can use RIA service for communication but I will describe here with WCF service. If we use VS 2008 we have to install silverlight toolkit and VS 2008 service pack 3. But for VS 2010 silverlight framework is already exist.

1.       First create an Silverlight type project. There are two types of project will automatically generate. One is web project and another is silverlight project.

2.       Silverlight works in client side so it produce a .xap file which exist in web project ClientBin folder. Being a client side application it needs service to communicate with server.  XAP file reated settings generate when we take silverlight type project. In web project there is ProjectNameTestPage.aspx file. It contains necessary settings.

3.       Now add Silverlight enabled WCF service (.svc file extension) to web project. Service file open automatically. There are few key words or class like [ServiceContract(Namespace = "")] above class name. [OperationContract] is above method name.  

4.       When we will create method in service we have to use the key word [OperationContract] above every method to introduce method publically.

5.       In web.config file there is tag <system.serviceModel/> under which few configuration code will generate. It is basically setup for service communication. In <bindings/> tag there is tag name <customBinding0/> remove the tag and its inside code and paste the following code.

<basicHttpBinding>

            <binding name="PCServicesBinding" closeTimeout="00:10:00" openTimeout="00:10:00"

              receiveTimeout="00:10:00" sendTimeout="00:10:00" maxBufferSize="2147483647"

              maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"

              transferMode="StreamedResponse">

              <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"

                maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />

            binding>

basicHttpBinding>                             

You can change the name and can configure the value of tags.

6.       Now in <endpoint/> tag under <service/> tag change the name of binding = “basicHttpBinding” and bindingConfiguration = “PCServicesBinding”. It is the name of tag.

7.       Run service (.svc) file by right click and “View in browser”. Now go to the Silverlight project add “Add Service Reference” click on discover. All the available service in web project will shown as reference in silverlight project. Try to expand service reference. Click on Advance option, uncheck “Reuse types in referenced assemblies”, click ok.

Now project is ready for coding with WCF service. We can create more configurations in this project. Basically when we deploye a web project service url also need to change along with project url. Service get this url from “ServiceReferences.ClientConfig” file. We can make a configurable file for services from where we can easily change the url of services.    

Friday, 28 May 2010

DeferredLoadingEnabled options in Linq

Linq to SQL has a drawback it load data from all the available relationship of database table. So it crush or become slow that’s why if we make DeferredLoadingEnabled property false it will not load its child or parent data when we request any single row from any table. 

base.DataContext.DeferredLoadingEnabled = false;

We can specify the parent or child table name when we try to fetch any particular table data. Then it will retrieve all the related data automatically from the define table. We can do it when we make the instance of any class. 

DataLoadOptions options = new DataLoadOptions();

options.LoadWith<Builder>(m => m.SalesOffices);

options.LoadWith<SalesOffice>(m => m.ModelHomes);

options.LoadWith<ModelHome>(m => m.ModelHomeDetailImages);

options.LoadWith<SalesOffice>(m => m.SalesOfficeAds);

base.DataContext.LoadOptions = options;

In the above example we create DataLoadOptions object then assign its LoadWidth property then again assigne DataContext LoadOptions. So current DataContext instance only have above define tables relational data only.

In this way we can make faster use of Linq to SQL.

Singletone Pattern

Making one instance of a class through out the project is called singletone. To do a object singletone you could make a class instance as static. Here is the technique to declare a object as singletone.

static BuilderInformation _Instance = null;

public static BuilderInformation GetInstance()

{

if (_Instance == null)

      {

            _Instance = new BuilderInformation();

}

return _Instance;

}     

When you need the object of this class just declare like:

 BuilderInformation builderInformation = BuilderInformation.GetInstance();

We can make collection of object as singletone. Here is an example of that. If SalesOfficeOID is not in the _LiveAgentConversationList list make an object of _ InstanceLA and append it to the collection of list.

static LiveAgentConversation _InstanceLA = null;

static IList<LiveAgentConversation> _LiveAgentConversationList = new List<LiveAgentConversation>();

public static LiveAgentConversation GetInstance(string saleOfficeOID)

{

LiveAgentConversation liveAgentConversation = null;

      if (_LiveAgentConversationList.Count > 0)

      {

            for (int i = 0; i <>

            {

                  if (_LiveAgentConversationList[i]._SalesOfficeOID == saleOfficeOID)

                  {

                        return _LiveAgentConversationList[i];

                  }            

            }

            if (liveAgentConversation == null)

            {

                liveAgentConversation = new LiveAgentConversation();

                liveAgentConversation._SalesOfficeOID = saleOfficeOID;               

                _LiveAgentConversationList.Add(liveAgentConversation);

            }

            return liveAgentConversation;

        }

}

During object invokation write statement like:

LiveAgentConversation liveAgentConversation = LiveAgentConversation. GetInstance(string saleOfficeOID)

Monday, 24 May 2010

appSettings in Web.onfig

It takes key and value combination.

<appSettings>

<add key="ServerURI" value="http://localhost:1643/"/>

appSettings>

To read value from web.config

string configPath = ConfigurationManager.AppSettings[key].ToString();