Listbox

I’ve remembered one day, I am doing my Imagine Cup coding part in Silverlight.

Suddenly, List of check box, List of Radio Button , List of Pic with Description come across in my mind.

Then, I think of a good way to do so. After a moment, I have these ideas.

 

Let me briefly explain. I will use a stackpanel to store the items for each item in list. Then add the stackpanel into the list.

Let say we have a list of checkbox and a list of redio button.

It is easy for us to create the list like this.

ListBox lb = new ListBox();
for (int i = 0; i <= 7; i++)
{
    CheckBox cb = new CheckBox(); 
    cb.Content = "Choice" + i.ToString();
    lb.Items.Add(cb);
    // Of course you can add some Event here for
} 

 

Case of Radio button  is the similar, so I dont explain so much here.

But for the case of Pic with Desc…

I will do like this.

ListBox lb = new ListBox();
for (int i = 0; i <= 7; i++)
{
    Image Img = new Image();
    BitmapImage bi = new BitmapImage(); 
    string k = "pic" + i.ToString();
    TextBlock tb = new TextBlock();
    tb.Text = k;
    k += ".jpg";
    bi.UriSource = new Uri(k, UriKind.Relative);
    Img.Source = bi;
    StackPanel sp = new StackPanel();
    sp.Add(Img);
    sp.Add(tb);
    lb.Items.Add(sp);
}  

Hope this can help more people to know more about Silverlight.

Live with Light!

Steve Wong (Hong Kong)

Advertisements

Xml and Linq in Silverlight

While answering a post in silverlight.net froum, I am surprised that the Silverlight work so well with XML WebService. 

First, in Xml WebService, you usually pass the Xml String to the Silverlight Application or using Http.Request to get the string by the Application itself. Silverlight can also parse it however you get it.

Now, I am going to introduce a good way to use the Linq with Silverlight.

Assume, you get a string by Xml Service after the Async Completed. That is,

<Records>
    <UserRecords>
        <Name>Steve</Name>
        <Age>17</Age>
        <Occupation>Student</Occupation>
        <Birthday>18-11-1990</Birthday>
    </UserRecords>
    <UserRecords>
        <Name>Hazel</Name>
        <Age>17</Age>
        <Occupation>Student</Occupation>
        <Birthday>14-12-1990</Birthday>
    </UserRecords>
    ...
    ...
    ...
</Records>

 

And Suppose there is so many records here. It is very complicated for you to use XmlReader
Now, XDocument is introduced.

string[,] mydata = new string[100,4]

if (e.Error == null)
{
    int i = -1;
    XDocument xd = XDocument.Parse(e.Result);
    var v = from g in xd.Descendants("UserRecords" )
    select new 
            {
                uname = g.Element("Name").Value,
                uage = g.Element("Age").Value,
                uocc = g.Element("Occupation").Value,
                udob = g.Element("Birthday").Value,
            }; 
    foreach (var itm in v)
    {
        i++;
        mydata[i,0] = itm.uname.Trim();
        mydata[i,1] = itm.uage.Trim();
        mydata[i,2] = itm.uocc.Trim();
        mydata[i,3] = itm.udob.Trim();
    }
} 

Furthermore, if the data is not present in this way, is as follow,

<Ranks>
  <Rank place="1">
    <FIELD NAME="name">Steve</FIELD>
    <FIELD NAME="age">17</FIELD>
  </Rank>
  <Rank place="2">
    <FIELD NAME="name">Hazel</FIELD>
    <FIELD NAME="age">17</FIELD>
  </Rank>
<Ranks>

The way in parsing this in similar.

public class RankingList
{
public string name {get ; set; }
public string age {get ; set; }
}
if (e.Error == null)
{
            List list = new List();
            XDocument xd = XDocument.Parse(e.Result);
            var v = from row in xd.Descendants("Rank")
                    select row.Elements("FIELD").ToDictionary(r=>r.Attribute("name").Value, r=>r.Value);
            foreach (var itm in v)
            {
                    RankingList t = new RankingList();
                    t.name = itm["Name"];
                    t.age = itm["Age"];
                    list.Add(t);
            }
}

 

I hope everyone can familiar with this Linq and Xml in Silverlight.

Live with Light.

Steve Wong (Hong Kong)

 

Navigation Bar

It seems that most people are very confused with designing a navigation for their own website
Let me introduce my own one.

First of all, we should have those thumbnail-sized  icons inside the bar. I suggest people download them from gallery in Microsoft.com. There is a tip on searching those icon. Please enter ‘png” for the searching keyword.

After downloaded all of them, we should add them into a stackpanel in xaml file.
Let see here.

Then, we should add four storyboard for three case

  1. Mouse enter the region of one of the icons
  2. Mouse leave the region of one of the icons
  3. Mouse click on one of the region

       
       
       


       
       
       


       
       
       


       
       
       

After that, we should try to finish the work with C#
We should have three case now as mentioned before.

string CurrPage = null;
Image CurrImg = null;
int Curr = 0;
TextBlock e_msg = new TextBlock();
#region Navigation Animation Starts Here
void Navigation_MouseLeave(object sender, MouseEventArgs e)
{
    Image pic = sender as Image;
    if (pic != null)
    {
        if (CurrPage != pic.Name)
        {
            pic.SetValue(Canvas.ZIndexProperty, 1);
            foreach (Timeline tl in LeaveButtonSB.Children)
            {
                Storyboard.SetTarget(tl, pic);
            }
            LeaveButtonSB.Begin();
        }
        else
        {
            e_msg.Text = "It is clicked!";
        }
    }
}
void Navigation_MouseEnter(object sender, MouseEventArgs e)
{
    Image pic = sender as Image;
    if (pic != null)
    {
        if (CurrPage != pic.Name)
        {
            pic.SetValue(Canvas.ZIndexProperty, 10);
            foreach (Timeline tl in EnterButtonSB.Children)
            {
                Storyboard.SetTarget(tl, pic);
            }
            EnterButtonSB.Begin();
        }
        else
        {
            e_msg.Text = "It is clicked!";
        }
    }
        }
#endregion Navigation Animation Ends Here
#region PageShowing Starts Here
public void ShowPage(object sender, MouseButtonEventArgs e)
{
    Image pic = sender as Image;
    if (pic != null)
    {
        if (CurrPage != pic.Name && CurrImg != pic)
        {
            if (CurrImg != null)
            {
                GlowButtonSB.Stop();
                foreach (Timeline tl in ResetButtonSB.Children)
                {
                    Storyboard.SetTarget(tl, CurrImg);
                }
                ResetButtonSB.Begin();
            }
            foreach (Timeline tl in GlowButtonSB.Children)
            {
                Storyboard.SetTarget(tl, (sender as Image));
            }
            GlowButtonSB.Begin();
            CurrPage = pic.Name;
            CurrImg = pic;
            //Show Page doing here
        }
        else
        {
            e_msg.Text = "Pic is needed for the sender in ShowPage";
        }
    }
    else
    {
        e_msg.Text = "It is clicked!";
    }
}

This time, I really control all the stuff in C# and Blend, never in ASP.NET.
Ofcoure, there is a way round in doing this trick by adding only a navigation bar in the MasterPage.
Just Like http://www.silverlight.net/
I hope everyone will love this navigation bar.

Let’s say “I LOVE YOU” to my dear computer

After the release of Siverlight 2.0, I do start reading the tutorials online. Of course, it is a very hard time for me becuase I am just a secondary school student. I spend most of the time at shcool on school day, leaving about 3 hours for me to read the posts at silverlight.net and sure I will answer them.

As I heard before, it is newly updated for Visual Studio to have a code behind on the Silverlight, I don’t know if it is true but it is so powerful that the xaml and xaml.cs can work so well with each other.

I had my own computer when I was Primary four. Starting from that day, it should be a summer holiday, I had started to borrow books from the public library, all books are so call Deep Look in ASP, CGI and PHP, some maybe HTML and Flash. When I was Primary five, my primary school had a workshop for me to learn Flash. I remember that is Flash 4.0. Learning for a few days, I just fell in love with computer and I told myself that I must become a programmer.

Nowadays, I do not need to borrow books because online learning materials are available wherever in MSDN, silverlight.net or MVP’s Blog.

I just want to be member of MVPs. It is a great honour to become a MVP. I do not only share my own experience of programming but also my experience of how do I work with computer. I know many programmers complain about the Async and Sync in cooperate webservice and silverlight. I do support the elimination of Sync. It is really protecting the platform …
Starting from today, I will start writing my own blog here … of course only about programming here.