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 = "<a href="http://tempuri.org/">http://tempuri.org/</a>")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class MyService : System.Web.Services.WebServices
    public MyService()
    public string Get_StaffInfo(string id)
        int nElement = 0;
        StringBuilder strReturn = new StringBuilder();
        SqlConnection sqlCon = null;
            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));
            SqlDataReader sRead = sqlCom.ExecuteReader();
            while (sRead.Read())
                strReturn.AppendFormat("<TextBlock Canvas.Top='{0}'" + "Foreground = 'White' Text = '{1}' />", nElement, sRead["ScreenName"]);
                nElement += 20;
            strReturn = new StringBuilder();
            strReturn.Append("</Canvas>" );
            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()
            Loaded += new RoutedEventHandler(Page_Loaded);
        void Page_Loaded(object sender, RoutedEventArgs e)
            mydata.Get_StaffInfoCompleted += new EventHandler<SL2WebSrv.WebServiceProxy.Get_StaffInfoCompletedEventArgs>(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;

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)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s