Metro Style Window in WPF

Follow the previous post, Metro Style ListBox in WPF, I am going to introduce the Metro Style Window in WPF.

MetroWindowResult

Continue reading

Advertisements

Metro ListBox in WPF

With the latest design strategy of Microsoft, Metro Style is dominating the entire internet. It is not difficult to find many websites adapted some elements of metro style. For Windows 8, you can fully feel the power of Metro Style.

Today, I am going to show you how to turn your ListBox (WPF / Silverlight) into Metro Style.

The result ListBox will be as below.

MetroListBox Continue reading

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)]
[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("<Canvas>");
        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("<TextBlock Canvas.Top='{0}'" + "Foreground = 'White' Text = '{1}' />", nElement, sRead["ScreenName"]);
                nElement += 20;
            }
        }
        catch
        {
            strReturn = new StringBuilder();
            strReturn.Append("<Canvas>");
        }
        finally
        {
            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()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(Page_Loaded);
        }
        void Page_Loaded(object sender, RoutedEventArgs e)
        {
            mydata.Get_StaffInfoAsync("1");
            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;
                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)