Blue Theme Orange Theme Green Theme Red Theme
 
Team Foundation Server Hosting
Home | Forums | Videos | Photos | Blogs | Beginners | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Nevron Gauge for SharePoint
Search :       Advanced Search »
Home » WPF » Writing Your First Procedural application in Windows Vista

Writing Your First Procedural application in Windows Vista

This article gives a background to build procedural application for Windows Vista. We build a simple application that gives you a good understanding of how to go about writing a procedural application.

Page Views : 8854
Downloads : 79
Rating :
 Rate it
Level : Intermediate
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
MyFirstApplication.zip
 
 
Nevron Gauge for SharePoint
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

Description

This article gives a background to build procedural application for Windows Vista (codename known as Longhorn). The simple application that would be built as we go will give a developer a good understanding of how to go about writing a procedural application.

Introduction

The sample code with this article is written using the Longhorn Build 4074 (Released at WINHEC).

There are many articles telling how to write applications using XAML (ZAMAL) for longhorn and a very few giving details on how to write an winform application in the way we always have been writing. The procedural way of writing a longhorn application is not new to a .NET developer. BY procedural way i mean, writing your application totally in c#, which is the way we have been writing applications in managed code. Hence, i write this article to demystify this way of writing applications.

XAML applications are not always the way to build applications on longhorn. In many cases a developer does not want to use a declarative method (XAML application) of writing applications {Usually when you want to decide the look and feel of your application in runtime.}. For such cases we can write applciations totally in c#. One might wonder that, every developer would now need to learn two different methodologies to build applications. But the good part is that a procedural application uses the same model as an XAML application. In a procedural application a developer initializes the same classes for which he may use tags in the XAML way. For example, if you use a <Dockpanel></DockPanel> tag in your XAML application, you would inturn use System.Windows.Controls.DockPanel class while writing a procedural application.

Let's look at the sample code of a procedural application.

Now lets tear apart the sample code and look at it piece by piece.

You would find that among the various namespaces that are imported in this sample, the System.Windows.Navigation is a new one. This name space contains the various classes that are used for navigation in applications, which includes navigating between windows or pages.

Notice that the class MyFirstApplication is inherited from the Application class. Application class is a light-weight class compared to the NavigationApplication class. The Application class is usually the class that is used the least, as this kind of applications lack navigation. In other words, these applications cannot create windows and pages in their applications. For Applications where you will need navigation support (which in most cases you will) you will have to inherit the NavigationApplication class.

Also, you would notice a sealed class in this code called MyMain with a main method called Main. This calss is used to launch the application. The application object has several events. I have overridden a event from application class to implement a custom OnStartingUp procedure. The other events that are available with the application object are Activate, Deactivate, SessionEnding, and ShuttingDown.

using System;
using System.Windows;
using System.Threading;
using System.Windows.Navigation;

namespace MyFirstProceduralApplication
{

public class MyFirstApplication : Application
{

System.Windows.Controls.DockPanel dockPanel;
System.Windows.Controls.DockPanel topPanel;
System.Windows.Controls.TextBox textBox;
System.Windows.Controls.Button button;
System.Windows.Controls.Button closeButton;
System.Windows.Controls.ListBox listBox;
System.Windows.Controls.Image image;
System.Windows.Window window;

// Override the OnStartingUp method to have custom start up routine.

protected override void OnStartingUp(StartingUpCancelEventArgs e)
{

// Get an instance of the Window object where the UI will be hosted.
window = new NavigationWindow();
// dockPanel will be used as the dock panel where all the application UI
// will be contained. Also it shall be the root to the UI Tree.
dockPanel = new System.Windows.Controls.DockPanel();
dockPanel.Height = new Length(1500);
// Initilizing all the controls needed in the application.
topPanel = new System.Windows.Controls.DockPanel();
textBox = new System.Windows.Controls.TextBox();
button = new System.Windows.Controls.Button();
closeButton = new System.Windows.Controls.Button();
listBox = new System.Windows.Controls.ListBox();
image = new System.Windows.Controls.Image();
// Set Values for the different controls.

textBox.Text = "Here goes some text";
textBox.Height = new Length(30);
textBox.Width = new Length(300);
button.Content = "Click Me";
button.Click += (this.Button_Clicked);

// Add the controls in to the top panel.
topPanel.Children.Add(textBox);
topPanel.Children.Add(button);
System.Windows.Controls.DockPanel.SetDock(textBox, System.Windows.Controls.Dock.Left);
System.Windows.Controls.DockPanel.SetDock(button, System.Windows.Controls.Dock.Right);
// Add the topPanel to the root DockPanel
dockPanel.Children.Add(topPanel);

System.Windows.Controls.DockPanel.SetDock(topPanel, System.Windows.Controls.Dock.Top);
listBox.Items.Add("Item1");
listBox.Items.Add("Item2");
listBox.Items.Add("Item3");
listBox.Items.Add("Item4");

listBox.Height = new Length(1000);
dockPanel.Children.Add(listBox);
System.Windows.Controls.DockPanel.SetDock(listBox, System.Windows.Controls.Dock.Left);
closeButton.Content = "Exit";

closeButton.Height = new Length(1000);
closeButton.Click += (this.End_App);
dockPanel.Children.Add(closeButton);
System.Windows.Controls.DockPanel.SetDock(closeButton, System.Windows.Controls.Dock.Right);

// Insert the image.

image.Height = new Length(1000);
image.Width = new Length(800);
image.Source = new System.Windows.Media.ImageData(new System.IO.FileStream(@"image.jpg",System.IO.FileMode.Open));
dockPanel.Children.Add(image);

System.Windows.Controls.DockPanel.SetDock(image, System.Windows.Controls.Dock.Fill);
window.Content = dockPanel;
window.Show();

}

 

private void Button_Clicked(object obj, System.Windows.Controls.ClickEventArgs e)
{

MessageBox.Show("Hey i was Just Clicked !!!");

}

private void End_App(object obj, System.Windows.Controls.ClickEventArgs e)
{

MessageBox.Show("Hey i am going DOWN !!!");
System.Windows.Application myApp = System.Windows.Application.Current;
myApp.Shutdown();
}

}

internal sealed class MyMain
{

[System.STAThread()]
public static void Main()
{

Thread.CurrentThread.ApartmentState = System.Threading.ApartmentState.STA;
MyFirstApplication myFirstApplication = new MyFirstApplication();
myFirstApplication.Run();
}
}
}


Below is the screen shot of how the application will look after it is compiled.

<Project DefaultTargets="Build">
<PropertyGroup>
<Property Language="C#" />
<Property DefaultClrNameSpace="MyFirstProceduralApplication" />
<Property TargetName="MyFirstApplication" />
<Property HostInBrowser="False" />
<Property ProductVersion="8.0.30703" />
<Property SchemaVersion="2.0" />

<Property ProjectGuid="{F93695FC-EAE2-471B-AD1E-3F9515FD42BD}" />
</PropertyGroup>
<Import Project="$(LAPI)\WindowsApplication.target" />
<ItemGroup>
<Item Type="Compile" Include="MyFirstApplication.cs" SubType="Code" />
</ItemGroup>
<ProjectExtensions>
<VisualStudio>
<CommonProperties>
<FL_FAE04EC0_301F_11D3_BF4B_00C04F79EFBC />
</CommonProperties>
<UserProperties />
</VisualStudio>
</ProjectExtensions>
</Project>

Below is the .proj file to build the above sample.

To build the application from the command line you will have to do the following.
(I am assuming that you have longhorn sdk's Bin folder part of the PATH environment variable)

c:\>SetEnv.cmd
c:\>msbuild MyFirstApplication.proj

(Tip: Use msbuild MyFirstApplication.proj /v:diag to build in verbose mode with all the diagnostic messages, to know what happens behind the scenes during the build process.)

Summary

This article gives a background to build procedural application for longhorn. The simple application that would be built as we go will give a developer a good understanding of how to go about writing a procedural application.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Suhil Srinivas
Suhil Srinivas is a seasoned developer, developing applications using various microsoft technologies. Suhil is a MCP and has been actively involved in mentoring and helping new-comers. He has a rich experience of building web, wireless and backend systems and has been doing consulting for various enterprises. Suhil in particular has been working a lot with the credit card and payment processing industry. EKS as a company has several products for the Educational institutes of any size. One of the chief products is the School-ERP system, which completely revolutionizes the day-to-day operation of an educational institute. Among other products EKS also has products for the small and medium healthcare providers. EKS is in to consulting and does offshore development for its customers from its office at Bangalore, India.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Team Foundation Server Hosting
Become a Sponsor
 Comments
Team Foundation Server Hosting
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.