Friday 15 February 2013

Posted by Prasad KM | 01:05 Categories:

WPF Interview Questions and Answers





How to define a button USING XAML?

To define a button in WPD using XAML use the following syntax,
<Button Name="btnName">btnCaption</Button>


Example:-
<Button Name="btnClick">Click Me</Button>

Here the <Button> element specifies the use of the Button class.
What is the use of "System.Windows.Markup" namespace in WPF?

The System.Windows.Markup namespace provides some helper classes for XAML code.
Which namespace provide classes for integration with WPF and Win32?

The "System.Windows.Interop" namespace provides classes for integration of WPF with Win32.
Which class of the WPF is the base class of all the user-interactive elements?

NOTE: This is objective type question, Please click question title for correct answer.
Which class is the base class of all the visual elements of WPF?
NOTE: This is objective type question, Please click question title for correct answer.
Control class of WPF is derived from which class?

NOTE: This is objective type question, Please click question title for correct answer
The core classes for User Interface is located in which namespace?

NOTE: This is objective type question, Please click question title for correct answer.
Which namespace provide classes to work with images, sound, video, etc?

The System.Windows.Media namespace provide classes to work with images, sound, video, etc in WPF.
What is x:Code?
x:Code is a directive element defined in XAML.
An x:Code directive element can contain inline programming code.
The code that is defined inline can interact with the XAML on the same page.

example:
<Window  x:Class="WpfApplication14jan.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">

    <Grid Name="dd">

        <Button Name="button1" Click="Clicked">In Line code!</Button>



        <x:Code>

            <![CDATA[

 void Clicked(object sender, RoutedEventArgs e)

 {

 button1.Content = "WPF Inline Code";

 }

 ]]>

       </x:Code>





    </Grid>

    </Window>
How many type of templates are available in WPF..?
Data Templates: Data Template visual representation of data in control with style. like List box.

Control Templates: Control Template suppliers a visual representation of a UI Control like Button or List View.

Items Panel Templates:Item Panel Templates uses when we want to show data in Hierarchical way like child object use under parent object.
What is the WPF Unit?
The WPF Unit is measured in DPI.

First we need to understand about DPI and PX (i.e. Device Independent Pixels & Physical Pixels)

1 DPI = 1/96 inch
1 PX = 1/DPI - Depends on screen type
The default system setting is 96 DPI

Note: If the DPI increases, the number of physical pixels which compose one device independent pixel also increases. Hence, objects that have their sizes specified in device independent pixels get larger as the system DPI increases
What are the core WPF Assemblies that one should refer in a WPF application?

1) PresentationCore.dll: Visual rendering of low-level types UIElement.
2) PresentationFramework.dll: Most of the common controls, and also support for data binding.
3) WindowsBase.dll: Lower-level types like DependencyObject and DependencyProperty.
What is the Class Hierarchy in Windows Presentation Foundation?

* System.Threading.DispatcherObject class represents on object associated with a "Dispatcher".
* System.Windows.DependencyObject class is the base class for classes that support dependency properties.
* System.Windows.Media.Visual class is the base class for objects that can be rendered to a WPF window or page.
* System.Windows.UIElement class defines core subsystems like Layout, Input, Focus and Events(LIFE).
* System.Windows.FrameworkElement introduces data binding and data templates, styles and animation.
* System.Windows.Controls.Control class adds a feature called templating. This is the base class for all the controls that the user interacts with.
* System.Windows.Controls.ContentControl can hold a single nested element.
What are the different Data Binding Modes in Windows Presentation Foundation?

The different Data Binding Modes are:
1) OneWay: The target is updated when the source changes.
2) TwoWay: The target is updated when the source changes, and similarly, the source is updated when the target changes.
3) OneWayToSource: Only the source is updated when the target changes.
4) OneTime: The target is updated only the first time the source changes.
What is the importance of INotifyPropertyChanged in WPF?

* This interface contains a single event called "PropertyChanged" of the delegate type "PropertyChangedEventHandler".
* To notify the WPF/Silverlight framework whenever the value of a property changes in an object, fire the event in the "set" accessor of the property.

What is the importance of INotifyCollectionChanged in WPF

* This interface is similar to INotifyPropertyChanged, which is to be implemented to notify the WPF/Silverlight framework whenever any changes occur in a collection, like adding an object or deleting an object.
* "ObservableCollection" is a built-in class which implements the INotifyCollectionChanged interface.
What is Logical Tree in WPF?

* Logical tree describes the relations between elements of the user interface.
* Every aspect of WPF (properties, events, resources, and so on) has behavior tied to the logical tree.
* Logical tree is responsible for:
1) Inherit Dependency Property values
2) Resolving Dynamic Resources references
3) Looking up element names for bindings
4) Forwarding Routed Events
* System.Windows.LogicalTreeHelper class is used to traverse the logical tree.
What is Visual Tree in WPF?

* Visual tree is an expansion of a logical tree, in which nodes are broken down into their visual components.
* Visual tree contails all logical elements including all visual elements.
* The elements that appear in a visual tree derive from 'System.Windows.Media.Visual' and 'System.Windows.Media.Visual3D'.
* Visual tree is responsible for:
1) Rendering visual elements
2) Propagate element opacity
3) Propagate layout and render transforms
4) Propagate the 'IsEnabled' property
5) Do hit-testing
NOTE:
* Visual tree is dependent on Windows theme.
* Visual tree is empty until the window undergoes layout at least once. So it must be navigated in 'OnContentRendered' event handler.
* System.Windows.VisualTreeHelper class is used to traverse the logical tree.
What is Dependency Properties and its Use in WPF?

* Dependency properties are used to enable styling, automatic data binding, templates, animation etc.
* All types that want to use DependencyProperties must derive from 'DependencyObject' class.
* The value of a DependencyProperty is resolved dynamically when read.
* The value of a dependency property is stored in a dictionary of keys and values provided by 'DependencyObject' class.
* Advantages:
1) Reduced memory footprint
2) Value Inheritance
3) Change Notification
What are the Types of Resources in WPF?

1) Binary Resources
Binary resources could be logo/image files, AV files etc.

2) Logical Resources are of tow types:
Static and Dynamic Resources

* StaticResource finds the key defined within the ResourceDictionary under its scope during the Loading of the application.
* Hence the Compiler will throw error during compilation if not found in resources.
--
* DynamicResource Markup Extension defers the resource assignment to the actual runtime of the application.
* So the expression remains unevaluated until the object being created.
What are Binary Resources in WPF?

* Binary resources could be logo/image files, AV files etc.
* Resource files which are added to project, can have the “Build Action” defined on it from the file properties windows:
- Resource : Embeds resource into the assembly (or culture specific satellite assembly)
- Content : this leaves resource as loose file and upon compilation this resource information is not embedded to assembly. Instead, it adds custom attribute to the assembly (AssemblyAssociatedContentFile) which records the existence and relative location of file.
What are Static Resources in WPF?

* StaticResource finds the key defined within the ResourceDictionary under its scope during the Loading of the application.
* Hence the Compiler will throw error during compilation if not found in resources.
What are Dynamic Resources in WPF?

* DynamicResource Markup Extension defers the resource assignment to the actual runtime of the application.
* So the expression remains unevaluated until the object being created.
What is the Choice between StaticResource and DynamicResource in WPF?
 * StaticResource requires less CPU during runtime, so it is faster.
* StaticResource are created when the application loads. So making everything as StaticResource means slowing down the Application Load process.
* When the resources are unknown during compilation, you can use DynamicResource.
* DynamicResource are used when user interaction changes the look and feel of an object.
What are Triggers and its type in WPF?

* The WPF styling and templating model enables you to specify Triggers within your Style.
* Essentially, Triggers are objects that enable you to apply changes when certain conditions (such as when a certain property value becomes true, or when an event occurs) are satisfied.
* Types of triggers:
1) Property triggers get active when a property gets a specified value.
2) Data triggers get active when a specified event is fired.
3) Event triggers get active when a binding expression reaches a specified value.
What are Templates and its type in WPF?

* A Template is used to change how a control looks.
* Types of templates:
1) Control template: How a control is rendered and behaves by specifying the visual structure and behavioral aspects.
2) Data template: To specify the visualization of data objects.
3) Hierarchical data template: Used over hierarchical structure like TreeView and Menu.
What is application object and its reponsiblity?
* Application is a class that represents a WPF application running as a standalone client application in Windows.
* Each running application contains at most a single instance of Application.
* The Application object is defined in the App.xaml file and is responsible for:
- Managing application lifetime (e.g. responding to startup/shutdown events)
- Window, property and resource management
- Command-line processing
- Navigation
What is the Application Lifetime in WPF?

The main events fired from Application include:
* Startup - Application is starting up.
* Exit – Fired when an application is shutting down.
* Activated – Fired when an application gets focus, i.e. becomes the foreground application
* Deactivated – Fired when application loses focus, i.e. is no longer the foreground application
* DispatcherUnhandledException – Fired when an exception is thrown, but not yet handled. You can choose to handle the exception or not
* SessionEnding – Fired when Windows is being shut down–due to either logoff or Windows shutdown. You can cancel the shutdown sequence.
* You can add custom code for any of these events by just overriding the OnEventName method in your Application-derived class, e.g. OnStartup.
What is the series of Window Events at Startup in WPF?

* At application startup, the Window events that are fired (in order) for the main window are:
1) Initialized - Main window is being created
2) IsVisibleChanged - IsVisible property set to true
3) SizeChanged - Size property set to size of window
4) LayoutUpdated - Window layout changes
5) SourceInitialized - Window is attached to Win32 window handle
6) Activated - Window becomes foreground window
7) PreviewGotKeyboardFocus - Window getting focus
8) IsKeyboardFocusWithinChanged - IsKeyboardFocusWithin property set to true
9) IsKeyboardFocusedChanged - IsKeyboardFocused property set to true
10) GotKeyboardFocus - Window now has keyboard focus
11) LayoutUpdated - Window layout changes
12) Loaded - Window is now laid out, fully rendered
13) ContentRendered - All window content has been rendered
What is the series of event fired for application shutdown in WPF?

1) Closing - Window is going to close
2) IsVisibleChanged - IsVisible property set to false
3) Deactivated - Window becomes background window
4) IsKeyboardFocusWithinChanged - IsKeyboardFocusWithin property set to false
5) IsKeyboardFocusedChanged - IsKeyboardFocused property set to false
6) LostKeyboardFocus - Window no longer has keyboard focus
7) Closed - Window is closing

How to Creating Windows Forms Controls Dynamically in WPF?

1) Import the following namespaces:
using System.Windows.Forms;
using System.Windows.Forms.Integration;
2) Create the windows forms control and set its properties and event handlers.
3) Add the control to the 'Child' property of 'WindowsFormsHost' object.
4) Add the host object to the 'Children' collection of the panel.
How to host WPF Controls in Windows Forms?

1) Add reference to the following:
a) Presentation Core
b) Presentation Framework
c) WindowsBase
d) WindowsFormsIntegration
e) System.Xaml
2) Also add reference to the WPF control DLL.
3) Use the 'ElementHost' control in 'WPF Interoperability' tab in Toolbox to host WPF controls.




0 comments:

  • RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
  • Youtube