In order to understand the different elements of WPF, we will do a small ‘hello world’ sample and in that process we will understand the different elements of WPF.
Note :- For this sample we have VS 2008 express edition.
So start VS 2008 express and from the templates select the WPF application as show in the below figure below.
Once we have created the WPF application project you will see two file types one the XAML file and the other is the behind code i.e. XAML.cs. XAML files are nothing but XML files which has all the elements needed to display the windows UI. Every of the XAML elements maps to come class. For instance the ‘Window’ element maps to ‘WpfApplication1.Window1’ class , ‘Button’ elements in XAML file maps to ‘System.Windows.Control.Button’ class and ‘Grid’ XAML element is mapped to ‘System.Windows.Control.Grid’.
The ‘App.XAML’ and ‘App.XAML.CS’ are the entry point files. If we see the code for ‘App.XAML.CS’ you will see the reference to XAML file which needs to be loaded. So the first code which runs in the application is ‘void main()’ method from ‘App.XAML.CS’ which in turn loads the ‘Window1.XAML’ file for rendering.
We can now connect the behind code method and function to events in XAML file elements.
You can see from the above code snippet how the button element has the click event linked to the ‘MyButton_Click’ function. ‘MyButton_Click’ is the method which is in the XAML.CS behind code.
So now if you run the code you can see the button and if you click on it you can see the message box for the same.
Asked In: Many Interviews |