Introduction of code-behind XAML Silverlight
Introduction
Usually
every XAML file has a corresponding code-behind page and code-behind language
could have C# (this is default) or VB. At very first time when we create a new
project, project selection windows has option to choose development language.
Here it is:
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 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.
Both
screenshots has same page code-front (bottom one) and code-behind (just above
the bottom one) shown.
As in
Code-Front
x:Class=”SilverlightApplication1.MainPage”
In above
code, ‘SilverlightApplication1’ is namespace name of this project and
‘MainPage’ is class name of this project. Remember, namespace name should
always be same as project title and class name should always be same as File
name for 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 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 appears at 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 logical grouping rather than physical grouping. ‘using’ alias make it
easier to qualify or identifier to a namespace or class in C#. Look at the
above image that has list of namespaces, all these appears by default. There
are huge list of more namespaces in .Net library that we will use as required.
InitializeComponent()
method plays most important role in Silverlight, we should never delete the
InitializeComponent() call from the constructor.
Comments
Post a Comment