skip to Main Content

Bag of Links #2

.NET/C# Programming

PowerShell

Apps and Tools

Other Stuff

My Silverlight 2.0 Hello World Application

This is an update to my original article that was based on Silverlight 1.0 alpha. The code for the old article no longer worked with the current Silverlight runtime.

Instead of the traditional and simple display of a string, this Hello World application uses animation in code to say “hello”.

Pre-requisites

You will need to have the following installed:

Create a New Silverlight Project

  • In Visual Studio 2008, choose File/New/Project and select Visual C#/Silverlight/Silverlight Application. The Silverlight project type is added when you install the Silverlight Tools.
  • Give your project a name and choose OK.

    New Silverlight Project

  • On the next dialog window, accept the default “Add a new ASP.NET Web project…” and choose OK.

Silverlight Project Files

Since we will be drawing objects, we need a Canvas control instead of the default Grid. Change your Page.xaml file to read like this:

<UserControl x:Class="SilverlightHelloWorld.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="520" Height="140" Loaded="UserControl_Loaded">
    <Canvas x:Name="MyCanvas"/>
</UserControl>

Press F7 when you have Page.xaml opened to switch to the code-behind file Page.xaml.cs, and replace the code there with this code:

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.Threading;
namespace SilverlightHelloWorld
{
    public partial class Page : UserControl
    {
        // private Storyboard _timer = new Storyboard();
        private DispatcherTimer _timer;
        private List<Cell> cells = new List<Cell>(); // list of all cells
        private int _xSize = 26; // width of the grid, in number of cells
        private int _ySize = 7; // height of the grid
        private int _numCells; // total number of cells
        private int _cellIdx = 0;
        /// <summary>Represents a cell on the grid</summary>
        private class Cell
        {
            public double X; // x-coordinate of cell
            public double Y; // y-coordinate
            public bool On; // True if cell is "on"
            public double Order; // Display order
        }
        public Page()
        {
            InitializeComponent();
        }
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            // Assign a random value to each cell 
            Random rnd = new Random(Environment.TickCount);
            _numCells = (int)(_xSize * _ySize);
            string template = "                          "
                + " x  x xxxx x    x     xx  "
                + " x  x x    x    x    x  x "
                + " xxxx xxxx x    x    x  x "
                + " x  x x    x    x    x  x "
                + " x  x xxxx xxxx xxxx  xx  "
                + "                          ";
            for (int y = 0; y < _ySize; y++)
            {
                for (int x = 0; x < _xSize; x++)
                {
                    Cell cell = new Cell();
                    cell.X = x * (this.Width / _xSize);
                    cell.Y = y * (this.Height / _ySize);
                    cell.Order = rnd.NextDouble();
                    cells.Add(cell);
                }
            }
            for (int i = 0; i < template.Length; i++)
            {
                cells[i].On = template[i] == 'x';
            }
            // Sort the cells by the random values
            cells.Sort(
                delegate(Cell c0, Cell c1)
                {
                    return c0.Order.CompareTo(c1.Order);
                }
            );
            _timer = new System.Windows.Threading.DispatcherTimer();
            _timer.Interval = TimeSpan.FromMilliseconds(1);
            _timer.Tick += new EventHandler(timer_Completed);
            _timer.Start();
        }
        void timer_Completed(object sender, EventArgs e)
        {
            if (_cellIdx < _numCells)
            {
                // Get the next cell
                Cell cell = cells[_cellIdx];
                // Draw the cell
                Rectangle r = new Rectangle();
                r.Stroke = new SolidColorBrush(Colors.DarkGray);
                if (cell.On)
                {
                    r.Fill = new SolidColorBrush(Colors.Red);
                }
                else
                {
                    r.Fill = new SolidColorBrush(Colors.LightGray);
                }
                r.Width = this.Width / _xSize;
                r.Height = this.Height / _ySize;
                r.SetValue(Canvas.LeftProperty, cell.X);
                r.SetValue(Canvas.TopProperty, cell.Y);
                MyCanvas.Children.Add(r);
                _cellIdx++;
            }
            else
            {
                _timer.Stop();
            }
        }
    }
}

Save the file. Right click on SilverlightHellowordTestPage.aspx and choose Set As Start Page, then run the app.

Hosting the Application

To host the application on your web site, just copy the following files:

  • Silverlight.js
  • SilverlightHelloWorldTestPage.html
  • ClientBin/SilverlightHelloWorld.xap

Click here to see the application in action.

Finds of the Week – June 17, 2008

.NET, C#

General Development

Software, Tools, etc.

Gaming

Something Different

Finds of the Week – Nov 28, 2007

I took a break from blogging last week for the U.S. Thanksgiving holiday. Here are the Finds for this week.

.NET

  • Just in case you have not heard, Visual Studio 2008 has been released (ScottGu). I installed the released version earlier this week and everything looks good so far. The first thing I noticed was the very quick load time compared to 2005.
  • Silverlight 1.1 is now 2.0. By Tim Sneath.
  • Dan Fernandez listed the top 15 things to love about Visual Studio 2008 Express.
  • Service Pack 1 for .NET Framework 2.0 is available. By Guy Barrette.
  • Aaronontheweb shared 5 Hot ASP.NET Tips and Tricks for the week of 11/20/2007. By Aaronontheweb.
  • bloggingdeveloper wrote a nice article on how to compress ViewState.

Software and Tools

  • Odiogo can make your blog talk. By Mads Kristensen.
  • I have been a long-time user of MSN Money Porfolio and stock charts but lately I really liked the stuff Yahoo! has been adding to their Finance section. Their new Flash-based stock chart is the best in its class. My favorite feature is the ability to drag left/right to go back/forward in time. Also their quotes are now streaming (AJAX)… meaning anywhere you see a price quote, it will be automatically refreshed. Real time quotes are available for as an optional service as well.

    Yahoo! Finance Stock Chart

  • To track disk space usage, my tool of choice is Windirstats. Windirstats is Open Source.

    Windirstats

Tech, Windows Mobile, Gadgets

  • Google has just released the beta version of My Location, a service that allows GPS like functionality with Google Maps on your mobile device without a GPS. It doesn’t work for me on my Samsung i760 though. I always get this error message: “Your current location is temporarily unavailable”.

    Google Maps My Location

    Looks like many others are getting the same error as well.

And Now, Something Different

Be careful where you put your cellphone.

Finds of the Week – 10/17/2007

As an attempt to make this blog appears more busy:-), I will begin my "Finds of the Week" series. Each week, I will share miscellaneous finds and thoughts I ran into that are related to mostly .NET development.

Here are the finds for this week:

.NET

  • Parallel Performance: Optimize Managed Code For Multi-Core Machines; by Daan Leijen and Judd Hall; via MSDN Magazine. In a few years' time, I predict that most new PCs will be multi-core. This article talks about the new Task Parallel Library (TPL) and provides code samples.
  • document.f.q.focus(); The Billion Dollar Line of JavaScript (by gst, via blogstorm.co.ok) talks about how many people use the Search Box on their browser to navigate to various web sites, instead of typing in a URL, and how this practice translates into revenue for Google. Interesting read.
  • alessandro pointed out that and IMG element with an empty src attribute will call the browser to make a request to the default document (usually default.aspx).
  • Understanding Windows Workflow Foundation (by razi bin rais, via Codeproject.com) is a nice overview of Windows Workflow Foundation.
  • Where are the basic controls in Silverlight 1.1? When I started experimenting with Silverlight recently (see my Silverlight Hello World application), the first question that came to my mind was: where are all the basic controls such as button, checkbox, textbox, etc?

    XAML Intellisense drop-down

    Don't worry! Apparently, they are not there because it's still an Alpha release. According to this post from Tim Sneath, the following controls are planned to be included in the released version of 1.1: Button, TextBox, Scrollbar, Slider, ListBox, CheckBox, RadioButton, ComboBox, and UserControl (no TreeView, RichTextBox, or DataGrid).

 

Tools

  • Probably old news to most CSS pros, but just in case you have not heard, FireBug is a very useful extension for FireFox/CSS development. Highly recommended if you ever work with CSS. I could have saved countless hours debugging HTML/CSS issues with this tool.
  • Notepad++ is a new addition to my toolset. Supports syntax highlighting, regular expression search/replace, Unicode, Macros, and is light-weight.

Windows Mobile / Pocket PC

Blogs

Miscellaneous

  • I am pretty bad with keeping in touch with people, and I have found that LinkedIn makes it fairly easy to keep in touch with past colleages. Last week I had some free time so I decided to look up old colleagues on LinkedIn. It was great to hear from many people I worked with years ago. I found many people by simply searching on the name of the company we worked for.

And Now, Something Different

Check out the Windows Live Maps Bird's Eye view below. See the black car entering the cul-de-sac? That's me and my car.

Microsoft Windows Live Maps - Chinh's Car

How do I know that’s my car? Well, for one thing, my car is black. But the real reason I know it’s me is that a moment later, my garage door opened half-way… as I was about to drive into the garage:

Microsoft Windows Live Maps - Chinh's Car

At first glance, it seems to be an amazing coincidence (like one-in-a-million amazing). What is the chance that Microsoft’s Bird’s Eye low flying plane would fly across my house and snap pictures just as I am about drive into my garage? However, after further calculations, it turns out that the real probability is around 1/8640 (5-second window over 43200 average seconds of day-light). Still a very small chance, but certainly not lottery-winning “amazing”.

Back To Top