Code Simplified – Viral Sarvaiya

Code Simplified – Viral Sarvaiya, Web Developer Friends, dot net Developer, Sql Server Developer

Posts Tagged ‘Silverlight’

Prevent Silverlight XAP file from caching in your browser.

Posted by Viral Sarvaiya on January 19, 2012


Sometime when we deploy silverlight project, that happen often that when you run website it run older deploy project. because that comes from the cache,

so clear the cache of the Browser and some time that problem occur also after clearing cache.

so for that we have to force website to download new version of xap file every time when it run.

normally we have tag for XAP file, as like below

<param name="source" value="/ClientBin/SilverApp.xap" />

but we have to download our XAP file new at every run of site.

so there so many option for that.

1) changing the Assembly and File version numbers

2) GUID value for the project

but, here is the simple and best solution for that and then it worked like a charm everytime.

<param name="source" value="/ClientBin/SilverApp.xap?ignoreme=<%=System.DateTime.Now.ToUniversalTime()%>" />

Enjoy,

Advertisements

Posted in .Net, ASP.NET, Silverlight | Tagged: , , , , , , , , , | 1 Comment »

Get Silverlight XAP and Hosting Page URL

Posted by Viral Sarvaiya on December 5, 2011


Get the URL to the current xap file:
App.Current.Host.Source.AbsoluteUri

Sample:
http://www.WebsiteName.com/ClientBin/SilverlightApplicationName.xap

Get the full URL of the page hosting the xap (with QueryStrings):
HtmlPage.Document.DocumentUri.ToString()

Sample:
http://www.WebsiteName.com/SilverlightApplicationName.aspx?param=1

Posted in .Net, ASP.NET, Silverlight, Visual Studio | Tagged: , , , , , | Leave a Comment »

Bind Multiple Columns in Combobox in Silverlight

Posted by Viral Sarvaiya on October 15, 2011


Silverlight has simple combobox like below.

<Combobox x:Name="combo1"  Width="100" Height="30" />

we use ItemSource property for binding data to combobox.
we can set DisplayMemberPath property as like

<Combobox x:Name="combo1"  Width="100" Height="30"  DisplayMemberPath ="Name"  />

for the multiple property we can edit compbobox itemTemplate.
for that you have to remove DisplayMemberPath property from Combobox tag and use the below tag for binding multiple properties to Combobox.

<ComboBox x:Name="cboTest">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name, Mode=OneWay}" />
<TextBlock Text=" " />
<TextBlock Text="{Binding Surname, Mode=OneWay}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>

if you want to select an item and want Id of selected item then you can use SelectedValue or Selecteditem Property as like below

<ComboBox x:Name="cboTest" ItemSource="{Binding MyList, Mode=TwoWay}" SelectedValue={Binding Path=Name, Mode="TwoWay"}>
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name, Mode=OneWay}" />
<TextBlock Text=" " />
<TextBlock Text="{Binding Surname, Mode=OneWay}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>

hope this will help you.

Posted in .Net, ASP.NET, Silverlight | Tagged: , , , , , | 2 Comments »

Set Source on Image from code in Silverlight

Posted by Viral Sarvaiya on January 11, 2011


Hi

this is the first post of year 2o11

here I demonstrate how to set source of the image from code in silverlight

basically we define source of image from XAML file as below


<Image x:name="imgPhoto" Source="http://www.viralsarvaiya.wordpress.com/imgphoto.jpg" />

so how can we define this source from code file.


string imgurl = " http://www.viralsarvaiya.wordpress.com/imgphoto.jpg ";

ImageSourceConvertor convertor = new ImageSourceConvertor();

imgPhoto.Source = (ImageSource) convertor.ConvertFromString(imgurl);

here we use ImageSourceConvertor but u can also use BitmapImage with an Uri class as below,


Uri uri = new Uri("http://www.viralsarvaiya.wordpress.com/imgphoto.jpg", Urikind.Absolute);

ImageSource imgs = new ImageSource(uri);

imgPhoto.Source = imgs;

with this you can define source of the image from source.

Enjoy…

Posted in Silverlight | Tagged: , , , | Leave a Comment »

File Drag and Drop in Silverlight4

Posted by Viral Sarvaiya on November 2, 2010


Silverlight 4 have drag and drop functionality. here i demostrate that functionality.
first create new project named “DragAndDropImage”.
You have to add references “Microsoft.Expression.Interactions”, “system.windows.interactivity” and “system.windows.browser”

Silverlight is too fun, when coding gets this simple. So the image can now be dragged around. Now lets add the drop feature.

For making everything very simple, I will just now add AllowDrop=”True” and then add a drop handler to handle any file that is being dropped.

Now, lets move to the code behind. For demonstration purpose, I will use images from deviantArt gallery.

In DragAndDropImageTest.xaml file


<UserControl x:Class="SilverLightTest.DragAndDropImageTest"
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 mc:Ignorable="d"
 xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
 xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
 d:DesignHeight="300" d:DesignWidth="400">

 <Grid x:Name="LayoutRoot" AllowDrop="True" Drop="LayoutRoot_Drop" Background="Black">
 <StackPanel x:Name="DropCollection" Height="100" Margin="10,30,10,10" VerticalAlignment="Top" HorizontalAlignment="Center" Orientation="Horizontal">
 </StackPanel>
 <Image x:Name="DropImage" Margin="0,100,0,0" Width="400" Cursor="Hand">
 <i:Interaction.Behaviors>
 <ei:MouseDragElementBehavior ConstrainToParentBounds="True"></ei:MouseDragElementBehavior>
 </i:Interaction.Behaviors>
 </Image>
 </Grid>
</UserControl>

In DragAndDropImageTest.xaml.cs file


using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using System.Windows.Media.Imaging;
using System.Collections.ObjectModel;
using System.Windows.Media;
using System.Windows.Browser;
using System.IO;

namespace SilverLightTest
{
 public partial class DragAndDropImageTest : UserControl
 {
 public DragAndDropImageTest()
 {
 InitializeComponent();
 }

 private void LayoutRoot_Drop(object sender, DragEventArgs e)
 {
 if (e.Data != null)
 {
 //reading the drop file
 FileInfo file = ((FileInfo[])e.Data.GetData(DataFormats.FileDrop)).FirstOrDefault();

 //creating a bitmapImage
 BitmapImage img = new BitmapImage();

 using (Stream s = file.OpenRead())
 {
 img.SetSource(s);
 }

 //setting the image in xaml
 DropImage.Source = img;

 Image newImage = new Image { Source = img };
 newImage.MouseLeftButtonDown += (a, b) =>
 {
 DropImage.Source = newImage.Source;
 };

 DropCollection.Children.Add(newImage);
 }
 }
 }
}

Run the application and check with dropping the image file in browser.

 

Enjoy Coding…..

Posted in ASP.NET, Silverlight | Tagged: , , , , , | 1 Comment »

New Data Binding Features in Silverlight 4

Posted by Viral Sarvaiya on October 11, 2010


Data Binding and String Formatting

In prior versions of Silverlight, in order to perform string formatting while data binding you would need to write a converter that would format the data during binding. In Silverlight 4 you are now able to format the data directly in XAML. This greatly simplifies the process of creating bindings that require some formatting of the data. Adding string formatting is as simple as adding a StringFormat extension in the XAML markup of the data binding. The StringFormat extension supports the same formatting options as the String’s Format method. Consider the following XAML. There are four text boxes displayed, all bound to the same property in code behind. The difference, however, is that each TextBox change displays the data differently based on the binding’s StringFormat extension. The first TextBox shows the raw data, the second formats the data to three decimal places, the third show the value in scientific notation, and the fourth shows the data as currency.


<Grid x:Name="LayoutRoot" Background="White">

<Grid.RowDefinitions>

<RowDefinition Height="*" />

<RowDefinition Height="*" />

<RowDefinition Height="*" />

<RowDefinition Height="*" />

</Grid.RowDefinitions>

<TextBox

Margin="5"

Grid.Row="0"

Text="{Binding DecimalValue}" />

<TextBox

Margin="5"

Grid.Row="1"

Text="{Binding DecimalValue, StringFormat='##.###'}" />

<TextBox

Margin="5"

Grid.Row="2"

Text="{Binding DateValue, StringFormat='MMM dd, yyyy'}" />

<TextBox

Margin="5"

Grid.Row="3"

Text="{Binding DecimalValue, StringFormat='c'}" />

</Grid>

As you can see from this example, you can easily change the format of data directly through the XAML.

TargetNullValue and FallBackValue Extensions

Two other extensions have been added to the base binding class in Silverlight 4: the TargetNullValue and FallBackValue extensions. These extensions allow you to specify data that will be displayed in the case when the data being bound to the control is not as expected. TargetNullValue provides a value for the binding that should be used if the data value being bound is null. An example of using this extension is shown here:


<TextBox Text="{Binding ValueName, TargetNullValue='Value is Null'}" />

FallBackValue provides a value to be used if the bound data is missing or if there was a mismatch that occurred. An example of using the FallBackValue extension is shown here:


<TextBox Text="{Binding ValueName, FallbackValue='Value Not Found'}" />

Thanks you.

Posted in ASP.NET, Silverlight | Tagged: , , , , , , , , | Leave a Comment »

Simple Data Binding in Silverlight

Posted by Viral Sarvaiya on October 1, 2010


Simple Data Binding in Silverlight
To help explain data binding in Silverlight, let’s build a very simple application. The application will include a Book object that contains two properties: Title and ISBN. These properties will be bound to two TextBox controls.

1.  Create a new Silverlight application in Visual Studio 2010. Name the project BasicDataBinding, and allow Visual Studio to create a Web application project to host your application.

2. Edit the MainPage.xaml file to define two columns and six grid rows. Place a TextBlock in each row in column 1 and a TextBox in each row in column 2. Also add some margins and some alignment assignments to improve the layout.

The code for the page follows:


<Grid x:Name="LayoutRoot" Background="White">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Text="Book Title"
VerticalAlignment="Center"
Margin="5" />
<TextBlock Text="ISBN-13"
VerticalAlignment="Center"
Margin="5"
Grid.Row="1" />
<TextBox Text="{Binding Title}"
Height="24"
Margin="5"
Grid.Column="1" />
<TextBox Text="{Binding ISBN}"
Height="24"
Margin="5"
Grid.Column="1" Grid.Row="1" />
<TextBlock Text="Book Title"
VerticalAlignment="Center"
Margin="5"
Grid.Row="2" />
<TextBlock Text="ISBN-13"
VerticalAlignment="Center"
Margin="5"
Grid.Row="3" />

<TextBox Text="{Binding Title}"
Height="24"
Margin="5"
Grid.Column="1" Grid.Row="2" />
<TextBox Text="{Binding ISBN}"
Height="24"
Margin="5"
Grid.Column="1" Grid.Row="3" />

</Grid>

3. Next, edit the code behind, MainPage.xaml.cs. Add a Loaded event handler for the application, which will fire when the application is loaded by the client. This is accomplished with the following source code:


public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Page_Loaded);
}

void Page_Loaded(object sender, RoutedEventArgs e)
{

}
}

Now you need to add a class to define a Book object. Below the MainPage class, add the following class definition:


namespace BasicDataBinding
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Page_Loaded);
}
void Page_Loaded(object sender, RoutedEventArgs e)
{
}
}
public class Book
{
public string Title { get; set; }
public string ISBN { get; set; }
}
}

4. Now that you have Book defined, you need to create an instance of Book and set it to the LayoutRoot’s DataContext, as follows:


void Page_Loaded(object sender, RoutedEventArgs e)
{
Book b = new Book()
{
Title = "Beginning Silverlight 4: From Novice to Professional",
ISBN = "978-1430229889"
};
this.LayoutRoot.DataContext = b;
}

When you set up binding definitions for different controls, the controls do not know where they are going to get their data. The DataContext property sets the data context for a control that is participating in data binding.The  DataContext property can be set directly on the control. If a given control does not have a DataContext property specified, it will look to its parent for its data context. The nice thing about this model is that if you look above in the XAML for the page, you will see little indication of where the controls are getting their data. This provides an extreme level of code separation, allowing designers to design XAML UIs and developers to work alongside the designers, defining the specifics of how the controls are bound to their data sources.

5. At this point, you can go ahead and start debugging the application. If all goes well, you will see the four text boxes populated with the data from the Book’s instance

6.  With the application running, change the book title in the first text box to just “Beginning Silverlight,” by removing the “From Novice to Professional.” You might expect that, since the third text box is bound to the same data, it will automatically update to reflect this change. However, a couple of things need to be done to get this type of two-way binding to work. One problem is that, currently, the Book class does not support notifying bound clients of changes to its properties. In other words, when a property changes in Book, the class will not notify the TextBox instances that are bound to the class of the change. You could take care of this by creating a change event for each property. This is far from ideal; fortunately, there is an interface that a class can implement that handles this for you. This interface is known as INotifyPropertyChanged. Let’s use it.

7.  Modify the Book class definition to inherit from INotifyPropertyChanged. Notice that when you inherit from INotifyPropertyChanged, you need to add using System.ComponentModel. Luckily, Visual Studio will help you with this.

Next, you can let Visual Studio do some more work for you. After adding the using System.ComponentModel statement, right-click INotifyPropertyChanged and select Implement Interface ➤ Implement Interface from the pop-up menu

Now Visual Studio has added a new public event to your class:


public class Book : INotifyPropertyChanged
{
public string Title { get; set; }
public string ISBN { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
}

8. Next, you need to create a convenience method that will fire the PropertyChanged event. Call it FirePropertyChanged, as shown in the following code:


public class Book : INotifyPropertyChanged
{
public string Title { get; set; }
public string ISBN { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
void FirePropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this,
new PropertyChangedEventArgs(property));
}
}
}

9. Now you need to extend the simplified properties by adding private members and full get/set definitions to define the get and set operations, as shown in the following code. The get is just like a normal get operation, where you simply return the internal member value. For the set, you first set the internal member value, and then call the FirePropertyChanged method, passing it the name of the property.


public class Book : INotifyPropertyChanged
{
private string _title;
private string _isbn;
public string Title
{
get
{
return _title;
}
set
{
_title = value;
FirePropertyChanged("Title");
}
}
public string ISBN
{
get
{
return _isbn;
}
set
{
_isbn = value;
FirePropertyChanged("ISBN");
}
}
public event PropertyChangedEventHandler PropertyChanged;
void FirePropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this,
new PropertyChangedEventArgs(property));
}
}
}

With this completed, your class is set up to notify bound clients of changes to the Title and ISBN properties. But you still need to take one more step. By default, when you bind a source to a target, the BindingMode is set to OneWay binding, which means that the source will send the data to the target, but the target will not send data changes back to the source. In order to get the target to update the source, you need to implement two-way (TwoWay) binding.

10. To change to two-way binding, add the Mode=TwoWay parameter when defining the {Binding} on a control, as follows:


<TextBlock Text="Book Title"
VerticalAlignment="Center"
Margin="5" />
<TextBlock Text="ISBN-13"
VerticalAlignment="Center"
Margin="5"
Grid.Row="1" />
<TextBox Text="{Binding Title, Mode=TwoWay}"
Height="24"
Margin="5"
Grid.Column="1" />
<TextBox Text="{Binding ISBN, Mode=TwoWay}"
Height="24"
Margin="5"
Grid.Column="1" Grid.Row="1" />
<TextBlock Text="Book Title"
VerticalAlignment="Center"
Margin="5"
Grid.Row="2" />
<TextBlock Text="ISBN-13"
VerticalAlignment="Center"
Margin="5"
Grid.Row="3" />
<TextBox Text="{Binding Title, Mode=TwoWay}"
Height="24"
Margin="5"
Grid.Column="1" Grid.Row="2" />
<TextBox Text="{Binding ISBN, Mode=TwoWay}"
Height="24"
Margin="5"
Grid.Column="1" Grid.Row="3" />

11.  Rebuild and run your application. Update any of the fields, and leave the focus on the control. You’ll see that the two-way binding is triggered, and the corresponding field is also updated,

Congratulations! You have just created a Silverlight application that allows for two-way data binding.

Posted in ASP.NET, Silverlight | Tagged: , , , , , | Leave a Comment »

Bind Silverlight Grid view with WCF Service

Posted by Viral Sarvaiya on September 17, 2010


Hello friends….

Here I demonstrate how to bind the grid view of the Silverlight with WCF Service.

You already know what is the Silverlight and WCF. My one post that demonstrate the use of the WCF Service with Silverlight, for more info about that check https://viralsarvaiya.wordpress.com/2010/08/26/use-of-wcf-service-in-silverlight/

Setp 1 : Create New Silverlight Project( here I give name of the project is “SilverlightGridTest”)

In the Visual Studio 2008 or 2010, file menu -> New -> project.

Select Silverlight application, give name as SilverlightGridTest and open the project.

Then check the checkbox true of the host the Silverlight application in web site.

Now you have 2 project in the solution.

Step 2: Database information

CREATE TABLE person (

Name VARCHAR(256),

Phone VARCHAR(50),

City VARCHAR(50)

)

make one procedure named “GetAllPersonInfo” and write the following Statement in the procedure

Select *from person order by name;

Step 3: Create WCF Service

Right click to SilverlightGridTest.web project, select add -> new item.

Choose Silverlight in left panel and choose Silverlight-enable WCF Service. Then give name “DataService.svc”

So Service has been added to project.

Here we have interface IDataService.cs and DataService will implements that so in our IDataService.cs code is as below.


using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;

namespace SilverlightGridTest.Web

{

[ServiceContract]

public interface IDataService

{

[OperationContract]

PersonDetails GetPersonDetails();

}

[DataContract]

public class PersonDetails

{

List<Personinfo> _PersonList = new List<Personinfo>();

[DataMember]

public List<Personinfo> PersonIndex

{

get { return _PersonList; }

set { _PersonList = value; }

}

}

[DataContract]

public class Personinfo

{

[DataMember]

public string Name { get; set; }

[DataMember]

public string Phone { get; set; }

[DataMember]

public string City { get; set; }

}

}

In the “DataService.svc.cs” file write the following.


using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;

using System.Data;

namespace SilverlightGridTest.Web

{

public class DataService : IDataService

{

public PersonDetails GetPersonDetails()

{

DatabaseHandler dbObj = new DatabaseHandler();

DataTable dataTablePersonInfo = dbObj.GetPersonInfo();

PersonDetails PersonDataMessage = new PersonDetails();

for (int iTagCounter = 0; iTagCounter < dataTablePersonInfo.Rows.Count; iTagCounter++)

{

Personinfo PersonInfo = new Personinfo();

PersonInfo.Name = dataTablePersonInfo.Rows[iTagCounter]["Name"].ToString();

PersonInfo.Phone = dataTablePersonInfo.Rows[iTagCounter]["Phone"].ToString();

PersonInfo.City = dataTablePersonInfo.Rows[iTagCounter]["City"].ToString();

PersonDataMessage.PersonIndex.Add(PersonInfo);

}

return PersonDataMessage;

}

}

}

Now add the new class name “DatabaseHandler.cs” for the operation on the database. Means this function will get the data from the database.


using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Data;

using System.Data.SqlClient;

using System.Configuration;

using System.Collections.ObjectModel;

namespace SilverlightGridTest.Web

{

public class DatabaseHandler

{

private string _gConnectionString = ConfigurationManager.ConnectionStrings["Database"].ToString();

public DataTable GetPersonInfo()

{

SqlConnection sqlConnect = new SqlConnection(_gConnectionString);

sqlConnect.Open();

DataTable dbTbl = new DataTable();

//Get data

SqlCommand cmd = new SqlCommand();

cmd.CommandText = "GetAllPersonInfo";

cmd.Connection = sqlConnect;

cmd.CommandType = CommandType.StoredProcedure;

SqlDataAdapter oAdapter = new SqlDataAdapter(cmd);

oAdapter.Fill(dbTbl);

sqlConnect.Close();

return dbTbl;

}

}

}

Step 4: Web.Config Settings

Database Connection string


<connectionStrings>

<add connectionString="packet size=4096;server=ServerName;persist security info=False;database=DatabaseName;uid=UserName;pwd=Password;" />

</connectionStrings>

Service Settings


<system.serviceModel>

<behaviors>

<serviceBehaviors>

<behavior>

<serviceMetadata httpGetEnabled="true" />

<serviceDebug includeExceptionDetailInFaults="false" />

</behavior>

</serviceBehaviors>

</behaviors>

<bindings>

<customBinding>

<binding>

<binaryMessageEncoding />

<httpTransport />

</binding>

</customBinding>

</bindings>

<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

<services>

<service behaviorConfiguration="SilverlightGridTest.Web.TestServiceBehavior" name="SilverlightGridTest.Web.DataService">

<endpoint address="" binding="basicHttpBinding" contract="SilverlightGridTest.Web.IDataService" />

<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />

</service>

</services>

</system.serviceModel>

Step 5: Add clientaccesspolicy.xml and crossDomain.xml file.

This will complete the coding in the SilverlightGridTest.Web .

Build the application.

Now move to SilverlightGridTest.

Add the service reference and add the service which we develop above.

Step 6: Mainpage.xaml file.

Now in the Mainpage.xaml file put the grid control.


<UserControl x:Class="SilverlightGridTest.MainPage"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"

xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"

mc:Ignorable="d"

d:DesignHeight="300" d:DesignWidth="400">

<Grid x:Name="LayoutRoot" Background="White" HorizontalAlignment="Left" VerticalAlignment="Top">

<sdk:DataGrid x:Name="AllPerson" GridLinesVisibility="All" AutoGenerateColumns="False" >

<sdk:DataGrid.Columns>

<sdk:DataGridTextColumn Binding="{Binding Name}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Header="Name" />

<sdk:DataGridTextColumn  Binding="{Binding Phone}" CanUserReorder="True" CanUserResize="True" CanUserSort="True"  Header="Phone"/>

<sdk:DataGridTextColumn  Binding="{Binding City}" CanUserReorder="True" CanUserResize="True" CanUserSort="True"  Header="City"/>

</sdk:DataGrid.Columns>

</sdk:DataGrid>

</Grid>

</UserControl>

Step 7 : Mainpage.xaml.cs file

Make the object of the service client, coding as follow.


using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

using SilverlightGridTest.DataService;

namespace SilverlightGridTest

{

public partial class MainPage : UserControl

{

public DataServiceClient objDataService;

public MainPage()

{

InitializeComponent();

objDataService = new DataServiceClient();

objDataService.GetPersonDetailsCompleted += new EventHandler<GetPersonDetailsCompletedEventArgs>(objDataService_GetPersonDetailsCompleted);

objDataService.GetPersonDetailsAsync();

}

void objDataService_GetPersonDetailsCompleted(object sender, GetPersonDetailsCompletedEventArgs e)

{

AllPerson.ItemsSource = e.Result.PersonIndex;

}

}

}

Step 8: Run the application.

You will get the data will display in the grid as below

Enjoy coding…..

Posted in ASP.NET, RIA WCF, Silverlight, WCF Services | Tagged: , , , , , , , , , , , , | 14 Comments »

Benefits of Silverlight

Posted by Viral Sarvaiya on September 10, 2010


Naturally, Silverlight offers all of the same benefits of RIAs, but there are a few features that set it apart from other RIA solutions, including the following:

  • It offers cross-platform/cross-browser support.
  • It provides a cross-platform version of the .NET Framework.
  • XAML is a text-based markup language.
  • Silverlight uses familiar technologies.
  • Silverlight offers an Out of Browser and Full Trust option.
  • Silverlight is the main development platform for Windows Phone 7.
  • It’s easy to deploy the Silverlight runtime to clients.

Let’s take a closer look at each of these benefits.

Cross-Platform/Cross-Browser Support

When ASP.NET was released a number of years ago, one of the benefits touted was cross- browser support. Developers would need to have only one code base, and that code base would work in all modern browsers. For the most part, this is true. No matter which browser you are using, the application will function. However, in order to receive all of the bells and whistles offered by the ASP.NET controls, you must use the latest version of  Internet Explorer. If you are using any other browser, you actually get a downgraded version of the web site, which contains fewer features.

Validation controls are a prime example. If you are using a browser that ASP.NET recognizes as an “upscale” browser, you can take advantage of client-side validation. If you are using any other browser, the validation controls still function, but require a postback to the server to do the validation. So, although ASP.NET is cross-browser, users can get different experiences, depending on which browser they are using.

With Silverlight, this changes. Microsoft is once again pulling out the term cross-browser, and also adding cross-platform, and this time they mean it. As a developer, you can create a Silverlight application and rest assured that it will run exactly the same on all supported platforms and browsers.Currently, two platforms are supported. Naturally, the first is Windows-based platforms, and the second is Mac OS platforms. As for browser support, Internet Explorer, Firefox, Safari and Google Chrome are currently covered.

This leaves one large platform unsupported: Linux. Although Microsoft does not have plans to support Linux, others do. Moonlight is an open source implementation of Silverlight, targeted primarily at Linux based operating systems. Moonlight is part of the Mono project, an open source initiative to develop and run .NET client and server applications on Linux, Solaris, Mac OS X, Windows, and Unix. Although Moonlight brings Silverlight features to Linux, the project lags behind the aggressive Microsoft release cycles. The latest version of Moonlight is version 3 Preview 6, which comes close to compatibility with Silverlight 3.

Cross-Platform Version of the .NET Framework

Silverlight 1.0 was released by Microsoft in the summer of 2007, but this version supported only Ecma languages that are interpreted in the client. Although Silverlight 1.0 works well for developers who are already familiar with client-side scripting, many developers have their eyes on the second release of Silverlight, version 2. Silverlight 1.0 is more or less in direct competition with Flash—some have called it Microsoft’s “Flash killer.” However, things really get exciting with Silverlight 2.

Silverlight 2 and beyond contains its own cross-platform version of the .NET Framework, which means it has its own version of the common language runtime (CLR), the full type system, and a .NET Framework  programming library that you can use in Visual Studio 2010 to build rich user experiences in the browser.

Use of Familiar Technologies

Microsoft is very good at creating tools that make application development easy. The Visual Studio integrated development environment (IDE) has been around for quite some time, and although new features are continually added to the tool, the environment itself has remained remarkably consistent. Silverlight development is no different. At the core of developing Silverlight applications is Visual Studio 2010, the latest version in Visual Studio’s long history. This gives Silverlight a distinct advantage, as developers do not need to learn how to use a new development environment. In addition to Visual Studio, Microsoft has a suite of tools called Expression Studio. Included in this suite is Microsoft Expression Blend, which is used to edit and create XAML for Silverlight applications. While Expression Blend looks completely different, it still has many of the same  elements as Visual Studio. In addition, Expression Blend works off of the same project as Visual Studio. This means that as you make changes in each of the editors—opening a project in Visual Studio, and then opening the  same project in Expression Blend to edit the XAML—the edited files will request to be refreshed when opened again in the other tool.

Small Runtime and Simple Deployment

Since Silverlight requires that a client runtime be installed on the client machine, it is vital that this runtime has a small footprint and downloads quickly. Microsoft worked very hard to get the installation size as small as possible. The developers clearly succeeded with Silverlight 1.0, as the download size is a tiny 1MB. For Silverlight 2, however, they had a harder chore ahead of them, since Silverlight 2 contains its own .NET Framework and object library. Microsoft went to each .NET Framework team and allocated it a size to fit its portion. The end result is astonishing—Silverlight 2 is approximately 4MB in size. In Silverlight 4, even with the large amount of new features that have been added to the Silverlight runtime, the file size is still less than 6MB.

As for pushing the Silverlight runtime out to clients, Microsoft has provided a very easy detection mechanism. If the client does not have the proper Silverlight runtime installed, it will display a logo of the Silverlight.

Posted in ASP.NET, Silverlight | Tagged: , | Leave a Comment »

WCF Duplex client for Silverlight

Posted by Viral Sarvaiya on September 9, 2010


WCF services offer the functionality to send data down to a Silverlight client. The Silverlight client polls the service at a regular interval to check if data is available for transfer.

Here are some great examples of how to implement duplex communication between WCF web services and Silverlight clients:

http://weblogs.asp.net/dwahlin/archive/2008/06/16/pushing-data-to-a-silverlight-client-with-wcf-duplex-service-part-i.aspx

http://petermcg.wordpress.com/2008/09/03/silverlight-polling-duplex-part-1-architecture/

http://petermcg.wordpress.com/2008/09/03/silverlight-polling-duplex-part-2-the-server/

http://petermcg.wordpress.com/2008/09/03/silverlight-polling-duplex-part-3-the-client/

Posted in ASP.NET, Silverlight | Tagged: , , , , , , , , , | 1 Comment »

 
%d bloggers like this: