Featured Posts

VS2010+SL3VS2010 beta + Silverlight 3 It keeps showing "Unable to Start Debugging. The Silverlight managed debugging package isn't installed".Finally the problem can be solved by installing Developer Runtime.

Readmore

An image in a post Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Quisque sed felis. Aliquam sit amet felis. Mauris semper, velit semper laoreet dictum, quam diam dictum urna, nec...

Readmore

Cash-Out Refinance For many, their homes are just not dwellings that protect them against rain, sun, and wind. But they are piggy banks, which can be used to raise some urgent money, even if...

Readmore

Wednesday, June 4, 2008

0
Powerful XAML

Today, I answered a question which is about XamlReader. By the way I think of so many technologies which can work with silverlight, not only because Silverlight can run on HTML or Silverlight can contact Webserivce, but because of it is making use of XAML.

Then I think of a Webserivce which return a string which is a string of XAML coding. It is so great to do so. Let say I have a SQL and after getting the data from the DataBase, maybe Information of Staffs. And I want to create a list of cards which includes the information of all staffs of my company. I can simply do that with all work on WebService but not Silverlight.

It has both advantages and disadvantages. For Adv, some class cannot be passed to Silverlight from Webservice (ArrayList, HashTable..) but we can manipulate all these into the form we want to show in XAML and pass that string. Yet, on the other hand, the xaml code will be so long that lengthens the time of downloading from the webserive.

By the way, Let me show an example here for the WebService.

using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using System.Data.SqlClient;
using System.Web.Script.Services;
using System.Text;
using System.Configuration;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class MyService : System.Web.Services.WebServices
{
public MyService()
{
}
[WebMethod]
public string Get_StaffInfo(string id)
{
int nElement = 0;
StringBuilder strReturn = new StringBuilder();
strReturn.Append("");
SqlConnection sqlCon = null;
try
{
string strConnectionString = (string)ConfigurationManager.ConnectionStrings["StaffDataBase"].ConnectionStrings;
sqlCon = new SqlConnection(strConnectionString);
SqlCommand sqlCom = new SqlCommand();
sqlCom.Connection = sqlCon;
sqlCom.CommandType= CommandType.Text;
sqlCom.CommandText = "SELECT ScreenName From dbo.Staffs WHERE (StaffId = @strId)";
sqlCom.Parameters.Add(new SqlParameter("@strId", id));
sqlCon.Open();
SqlDataReader sRead = sqlCom.ExecuteReader();
while (sRead.Read())
{
strReturn.AppendFormat(" nElement += 20;
}
}
catch
{
strReturn = new StringBuilder();
strReturn.Append("");
}
finally
{
strReturn.Append("
" );
if (sqlCon != null) sqlCon.Close();
}
return strReturn.ToString();
}
}


Inside your Silverlight you call this

namespace SL2WebSrv
{
public partial class Page : UserControl
{
WebServiceProxy.ServiceSoapClient mydata = new SL2WebSrv.WebServiceProxy.ServiceSoapClient();
public Page()
{
InitializeComponent();
Loaded += new RoutedEventHandler(Page_Loaded);
}
void Page_Loaded(object sender, RoutedEventArgs e)
{
mydata.Get_StaffInfoAsync("1");
mydata.Get_StaffInfoCompleted += new EventHandler(mydata_Get_StaffInfoCompleted);
}
void mydata_Get_StaffInfoCompleted(object sender, SL2WebSrv.WebServiceProxy.Get_StaffInfoCompletedEventArgs e)
{
if (e.Error == null)
{
Canvas newStaff = XamlReader.Load(e.Result) as Canvas;
LayoutRoot.Children.Add(newStaff);
}
}
}
}


After all this tutorial, I hope more developers can know more about how powerful the Silverlight is and of course the XAML.

It can really work with so much thing.... not only Webservice but also JSP and AJAX and ASP.NET...

Live with Light
SteveWong (Hong Kong)

0 comments:

Post a Comment