Introduction of code-behind XAML Silverlight



Introduction

Usually every XAML file has a corresponding code-behind page and the code-behind language can be C# (it is the default) or VB. Initially when we create a new project, the project selection windows have the option to choose the development language. Here it is:

XSil1.gif

Well, look at the screenshot given below, recall the Visual Studio ASP.Net development, we create new project and project explorer lists the files and a file named Default.aspx is the default for our entire web project (in general), in Blend MainPage.xaml has the same role here (in general but both are very different stocks, will talk later). Also recall web.config file, app.xaml same role here (in general but both are very different stocks, will talk later). We start our editing or development in Blend from MainPage.xaml page and it is very important to understand the logics of both pages.

XSil2.gif

XSil3.gif

Both screenshots has the same page code-front (bottom one) and code-behind (just above the bottom one) shown.

As in Code-Front

x:Class="SilverlightApplication1.MainPage"

In the above code, 'SilverlightApplication1' is the namespace name of this project and 'MainPage' is the class name of this project. Remember, the namespace name should always be the same as the project title and the class name should always be same as the file name in which we are writing code-behind (no use of extension name here).


Why we use additional namespaces like 'using System; etc…'?

In almost every code-behind file we see an already declared list of namespaces, what's these? We see at first glance, namespaces represented in C++ as "include <iostream.h>" or in VB "import System" or in C# "using System" these all appear in the very top lines. A namespace is just a grouping of related classes. It's a method of putting or using classes inside code. In java we use it as "package". Namespaces are a logical grouping rather than a physical grouping. A "using" alias makes it easier to qualify or identify a namespace or class in C#. Look at the above image that has the list of namespaces; all those appear by default. There is a huge list of additional namespaces in the .Net library that we will use as required.

XSil4.gif

The InitializeComponent() method plays a very important role in Silverlight; we should never delete the InitializeComponent() call from the constructor.

Be tuned for the next article.

HAVE A HAPPY CODING...

Up Next
    Ebook Download
    View all
    Learn
    View all