Archive for September, 2010

Event Handling in WPF

Events in WPF works same as in Win Form application except some new concept such as  Routed Event  ( I will blog it in next post).So directly we can jump to the code to add simple events to application as well as Window(Forms in WPF are called as Window ,for XBAP it called as Page).

There are 2 ways of adding events

  1. Declarative Way ( XAML Approach )
  2. Code Way (Conventional as in Win form / Code Approach)

So consider  the scenario bellow and the way to do it in both ways

Scenario 1 –: On Start of our Application we will show the application details.

Through XAML

As we should on load of application the event must be declared in the app.XAML or app.xaml.cs.Write the the event name we want to invoke , the intellisense will prompt as follows

www.manaspatnaik.com/blog Once you click image , it will add a handler to the app.xaml.cs .And there we will have the application information.

Basics What is  Event Handler ?
Code Snippet
  1. /// Interaction logic for App.xaml
  2. /// </summary>
  3. public partial class App : Application
  4. {
  5. private void Application_Startup(object sender, StartupEventArgs e)
  6. {
  7. MessageBox.Show(Environment.Version + “\n” + Environment.UserName
  8. ,“My First WPF Application”);
  9. }
  10. }

Through CODE –:

As the OnStartup is application related event we will override in the app.xaml.cs.

image

Code Snippet
  1. protected override void OnStartup(StartupEventArgs e)
  2. {
  3. MessageBox.Show(Environment.Version + “\n” + Environment.UserName
  4. , “My First WPF Application”);
  5. }

Scenario 2 –: On Button Click display application information (Attaching a event to a method)

Through XAML –:

First we will create a EventHandaler in the the window , as bellow .

Code Snippet
  1. public partial class MyFirstWindow : Window
  2. {
  3. public MyFirstWindow()
  4. {
  5. InitializeComponent();
  6. }
  7. private void ShowApplicationInfo(object sender,RoutedEventArgs e)
  8. {
  9. MessageBox.Show(Environment.Version + “\n” + Environment.UserName
  10. ,“My First WPF Application”);
  11. }
  12. }

Now we will attach the event to the button click in through xaml .

image So the final Xaml code will look like this.

Code Snippet
  1. <Grid>
  2. <Label>My First WPF Application</Label>
  3. <Button Content=”Show About” Height=”50″ HorizontalAlignment=”Left” Margin=”71,78,0,0″
  4. Name=”btnShowAbout” VerticalAlignment=”Top” Width=”143″
  5. Click=”ShowApplicationInfo”
  6. />
  7. </Grid>

Through CODE–:

Code way is same as previous we need to attach the event to the handler on load.

image and the final code will be

Code Snippet
  1. private void ShowApplicationInfo(object sender,RoutedEventArgs e)
  2. {
  3. MessageBox.Show(Environment.Version + “\n” + Environment.UserName
  4. ,“My First WPF Application”);
  5. }
  6. private void Window_Loaded(object sender, RoutedEventArgs e)
  7. {
  8. btnShowAbout.Click += new RoutedEventHandler(ShowApplicationInfo);
  9. }

That’s it .

,

Leave a comment

Getting Into WPF

Learning a new programming platform is never easy until unless somebody is really inspired.WPF (Windows Presentation Foundation ) is  quite convincing if you look at its prospective and the capability it gives to user.This new graphical subsystem from Microsoft relies on Direct X for rendering instead of GDI.So the advantage this  declarative programming model is Resolution Independent provides extensive integration with independent technology (Text to speech , 3d animation ,video etc),Hardware acceleration with Direct 3d and most useful complete customization of controls.

Wpf applications development  can be of two type

  • Windows Based Application
  • Browser Based Application (XBAP)
1285599500_package_edutainment What is Declarative Programming ?

This Blog post is about my experience with first WPF application , a simple application with basic understanding.

Create a WPF Project :

Open Visual Studio and Select WPF Application .Provide a Name “StartWithWpf” and click Ok .

Create a WPF Project

Have a look at the source folder , It contains a default MainWindows.xaml and App.xaml.So the XAML Markup drives WPF application with similar syntax of XML (Read More about XAML and Syntax) .A complied form of XAML is known as BAML.

r.2625741350 App.Xaml
  • App.xaml is a class(Singleton Pattern) which encapsulate the application specific functionality and resource handling such as
    • Application Lifetime
    • Application Windows,property
    • Resource Management
    • Command Line Parameter
    • Navigation
Digging Into APP.Xaml:

The App.Xaml (Application class )is the entry point for the application .We can write as code behind application class or in the mark-up.The declarative Mark-up is must for XBAP.

Code Snippet

  1. <Application
  2. xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation&#8221;
  3. xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml&#8221;
  4. x:Class=”StartWithWpf.App”
  5. StartupUri=”MainWindow.xaml”>
  6. </Application>

So the app.xaml need above lines to run a project.These are the Attributes of Application .

xmlns –:Defines the Namespace for the Application

xmlns : x-:Defines the additional namespace for the application

Basics of XAML Syntax

Above both attributes direct to the specification required to be referenced for xaml.You can find out some additional references as you add your some custom namespaces.

Custom Namespace in WPF

Adding a Form:

Now lets add a Form , here it is Windows (instead of Form as in WinApp).So adding a Window named “MyFirstWindow”.Add new WPF Window

Now the look at the Window , we have added.

Wpf Window

I will change the Title property and add a label to the Grid Layout.(Layouts are very important in WPF ).

Code Snippet
  1. <Window x:Class=”StartWithWpf.MyFirstWindow”
  2. xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation&#8221;
  3. xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml&#8221;
  4. Title=”Home” Height=”300″ Width=”300″
  5. >
  6. <Grid>
  7. <Label> My First WPF Application </Label>
  8. </Grid>
  9. </Window>

Now to Run the application i will go to App.xaml and Change the StartUpUri attribute to “MyFirstWindow.xaml”.Lets run it and you are ready with your first WPF application.A simple but fair start  :).

, ,

1 Comment

Bing With HTML 5

Bing in HTML5 could be Microsoft’s response to Google’s instant search, and we could likely see this being released by the first quarter of next year, right around the Internet Explorer 9 launch.

 

Watch it :

Bing in HTML5

Leave a comment

Design a site like this with WordPress.com
Get started