Working With Classes: Windows Phone 7.5 Mango Apps Development – Part 12

Hope you are enjoying our Windows Phone 7.5 Mango Apps Development tutorial series. Having learnt about Understanding and Creating Classes in our last tutorial, , in this tutorial, we are going to continue talking about classes and objects but now we will learn how classes are used within .Net framework. As we learnt in last lessons, we can create our own custom classes. We can define its attribute as well as functions. But in this tutorial series, we will not dive much into the custom classes, so for now consider classes as containers for methods.

.Net framework has many pre-written classes, which differ in functionality. For example, we have already seen DateTime class which has various methods enabling users to play around with date and time. So classes act as a bundle of functionalities.

In last lesson, we used “new” keyword to create new instance of the class but there are classes in .NET framework which do not require an instance to be created before using them; such classes are called as “static” classes. Keyword “static” is used to declare such classes. We have used a few static classes, if you can remember! The “string” class which we used to declare as well as manipulate strings is nothing but a static class whose definition is pre-written in .NET framework. E.g. string.Format(“{0}!”,”Hi”);

There are many in-built static classes we’ve used. You would be surprised to know that buttons, text blocks and other elements that we used are a part of the classes and by dragging and dropping we create instance of it. If you want to know more about such .NET framework classes, you can read about them on MSDN.

Let us now see how we can use classes given by .NET framework. Let us start by creating new project and have one button in it. We will code it in C# so that when we click that button a new button will be generated and displayed.

App_Layout

So after placing a button on screen, double-click it and place the cursor in between button_Click event. Paste following code in that event handler.

Button myNewButton = new Button();
            myNewButton.Name = "button2";
            myNewButton.Height = 100;
            myNewButton.Width = 200;
            myNewButton.Content = "Click me";
            myNewButton.Margin = new Thickness(100.0, 100.0, 100.0, 100.0);
            myNewButton.HorizontalAlignment = HorizontalAlignment.Stretch;
            myNewButton.VerticalAlignment = VerticalAlignment.Stretch;

 

ContentGrid.Children.Add(myNewButton);

Upon running this application, you will see that when we click the first button, second button appears. So this shows that there are more than one way to create elements in C#. In first line of code we have declared new instance of button just like any other declaration and later we haves set its properties. The last line of code adds the button to screen.

App_Output

So this is all about Classes. Of course you will have a greater understanding of Classes once you start working on your applications. In our next tutorial, we will talk of NameSpaces.

Posted by on , in Category Phone with Tags
Gadget freak, Apple lover, Windows Phone lover; well that’s me! I have been in this wonderful field of freelance writing for quite some time and looking forward to developing more illuminating content related to gadgets and technology, as I go along.

Leave a Reply

Your email address will not be published. Required fields are marked *


6 + 4 =