Archive for September, 2010
Event Handling in WPF
Posted by Manas Patnaik in WPF on September 30, 2010
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
- Declarative Way ( XAML Approach )
- 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
Once you click
, it will add a handler to the app.xaml.cs .And there we will have the application information.
| What is Event Handler ? |
- /// Interaction logic for App.xaml
- /// </summary>
- public partial class App : Application
- {
- private void Application_Startup(object sender, StartupEventArgs e)
- {
- MessageBox.Show(Environment.Version + “\n” + Environment.UserName
- ,“My First WPF Application”);
- }
- }
Through CODE –:
As the OnStartup is application related event we will override in the app.xaml.cs.
- protected override void OnStartup(StartupEventArgs e)
- {
- MessageBox.Show(Environment.Version + “\n” + Environment.UserName
- , “My First WPF Application”);
- }
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 .
- public partial class MyFirstWindow : Window
- {
- public MyFirstWindow()
- {
- InitializeComponent();
- }
- private void ShowApplicationInfo(object sender,RoutedEventArgs e)
- {
- MessageBox.Show(Environment.Version + “\n” + Environment.UserName
- ,“My First WPF Application”);
- }
- }
Now we will attach the event to the button click in through xaml .
So the final Xaml code will look like this.
- <Grid>
- <Label>My First WPF Application</Label>
- <Button Content=”Show About” Height=”50″ HorizontalAlignment=”Left” Margin=”71,78,0,0″
- Name=”btnShowAbout” VerticalAlignment=”Top” Width=”143″
- Click=”ShowApplicationInfo”
- />
- </Grid>
Through CODE–:
Code way is same as previous we need to attach the event to the handler on load.
- private void ShowApplicationInfo(object sender,RoutedEventArgs e)
- {
- MessageBox.Show(Environment.Version + “\n” + Environment.UserName
- ,“My First WPF Application”);
- }
- private void Window_Loaded(object sender, RoutedEventArgs e)
- {
- btnShowAbout.Click += new RoutedEventHandler(ShowApplicationInfo);
- }
That’s it .
Getting Into WPF
Posted by Manas Patnaik in WPF on September 29, 2010
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)
| 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 .
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.
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
- <Application
- xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
- xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
- x:Class=”StartWithWpf.App”
- StartupUri=”MainWindow.xaml”>
- </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
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.
Adding a Form:
Now lets add a Form , here it is Windows (instead of Form as in WinApp).So adding a Window named “MyFirstWindow”.![]()
Now the look at the Window , we have added.
I will change the Title property and add a label to the Grid Layout.(Layouts are very important in WPF ).
- <Window x:Class=”StartWithWpf.MyFirstWindow”
- xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
- xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
- Title=”Home” Height=”300″ Width=”300″
- >
- <Grid>
- <Label> My First WPF Application </Label>
- </Grid>
- </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 :).
Bing With HTML 5
Posted by Manas Patnaik in Latest in the air on September 16, 2010
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 :
Recent Comments