• +91 9971497814
  • info@interviewmaterial.com

WPF Interview Questions Answers

Question 1 : Are XAML file compiled or built on runtime?

Answer 1 : XAML files are usually compiled rather than parsing on runtime. But it also supports parsing during runtime. When we build a XAML based project, you will see it creates g.cs extension in obi\Debug folder. Therefore, for every XAMl file you will find a g.cs file. For instance, a Shiv.XAML will have Shiv.g.cs file in obi\Debug folder. In short, in runtime you actually do not see the XAML file. But if you want to do runtime, parsing of XAML file it also allows that to be done.

Question 2 : Can explain the different elements involved in WPF application practically?

Answer 2 : 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.  

Question 3 : Can you explain how we can separate code and XAML?

Answer 3 : This is one of the most important features of WPF, separating the XAML from the code to be handled. So designers can independently work on the presentation of the application and developers can actually write the code logic independent of how the presentation is.    Figure .1:- XAML and behind code in action Above is the code snippet, which shows a XAML file and the code completely detached from the XAML presentation. In order to associate a class with XAML file you need to specify the x: Class attribute. Any event specified on the XAML object can be connected by defining a method with sender and event values. You can see from the above code snippet we have linked the MyClickEvent to an event in the behind code.

Question 4 : Can you explain the overall architecture of WPF?

Answer 4 : Above figure shows the overall architecture of WPF. It has three major sections presentation core, presentation framework and milcore. In the same diagram we have shown how other section like direct and operating system interact with the system. So let’s go section by section to understand how every section works. User32:- It decides which goes where on the screen. DirectX: - As said previously WPF uses directX internally. DirectX talks with drivers and renders the content. Milcore: - Mil stands for media integration library. This section is a unmanaged code because it acts like a bridge between WPF managed and DirectX / User32 unmanaged API. Presentation core ;- This is a low level API exposed by WPF providing features for 2D , 3D , geometry etc. Presentation framework:- This section has high level features like application controls , layouts . Content etc which helps you to build up your application.

Question 5 : Does that mean WPF has replaced DirectX?

Answer 5 : No, WPF does not replace DirectX. DirectX will still be still needed to make cutting edge games. The video performance of directX is still many times higher than WPF API. So when it comes to game development the preference will be always DirectX and not WPF. WPF is not a optimum solution to make games, oh yes you can make a TIC TAC TOE game but not high action animation games. One point to remember WPF is a replacement for windows form and not directX.

Question 6 : How can we access XAML objects in behind code?

Answer 6 : To access XAML objects in behind code you just need to define them with the same name as given in the XAML document. For instance in the below code snippet we named the object as objtext and the object is defined with the same name in the behind code.    Figure .2 Accessing XAML object

Question 7 : How does hardware acceleration work with WPF?

Answer 7 : Hardware acceleration is a process in which we use hardware to perform some functions rather than performing those functions using the software which is running in the CPU. WPF exploits hardware acceleration in a two tier manner. WPF API first detects the level of hardware acceleration using parameters like RAM of video card , per pixel value etc. Depending on that it either uses Tier 0, Tier 1 or Tier 2 rendering mode. Tier 0:- If the video card does not support hardware acceleration then WPF uses Tier 0 rendering mode. In other words it uses software acceleration. This corresponds to working of DirectX version less than 7.0. Tier 1:- If the video card supports partial hardware acceleration then WPF uses Tier 1 rendering mode. This corresponds to working of DirectX version between 7.0 and 9.0. Tier 2:- If the video card supports hardware acceleration then WPF uses Tier 2 rendering mode. This corresponds to working of DirectX version equal or greater than 9.0.

Question 8 : So can we define WPF in a precise way?

Answer 8 : Windows Presentation Framework is the new presentation API. WPF is a two and three dimensional graphics engine. It has the following capabilities:- • Has all equivalent common user controls like buttons, check boxes sliders etc. • Fixed and flow format documents  • Has all of the capabilities of HTML and Flash  • 2D and 3D vector graphics  • Animation  • Multimedia  • Data binding

Question 9 : So is XAML meant only for WPF?

Answer 9 : No,XAML is not meant only for WPF.XAML is a XML-based language and it had various variants. WPF XAML is used to describe WPF content, such as WPF objects, controls and documents. In WPF XAML we also have XPS XAML which defines an XML representation of electronic documents. Silverlight XAML is a subset of WPF XAML meant for Silverlight applications. Silverlight is a cross-platform browser plug-in which helps us to create rich web content with 2-dimensional graphics, animation, and audio and video. WWF XAML helps us to describe Windows Workflow Foundation content. WWF engine then uses this XAML and invokes workflow accordingly.

Question 10 : What are dependency properties?

Answer 10 : These dependency properties belong to one class but can be used in another. Consider the below code snippet:- Height and Width are regular properties of the Rectangle. But Canvas. Top and Canvas. Left is dependency property as it belongs the canvas class. It is used by the Rectangle to specify its position within Canvas.

WPF Contributors

krishan

Share your email for latest updates

Name:
Email:

Our partners