Showing posts with label WPF. Show all posts
Showing posts with label WPF. Show all posts

Wednesday, 15 March 2017

WPF interview question Part 3

1. Ques: What are the difference Between WPF and Silverlight ?
Answer:
  • Silverlight is simply a subset of WPF.
  • Silverlight is meant to be used online, while WPF is for local use.
  • You can use Silverlight applications regardless of the operating system you use, while WPF applications are restricted to later versions of the Windows operating system.
  • Silverlight lacks access to local resources, while WPF can utilize local resources.
  • Silverlight only has perspective 3D support, while WPF is capable of full 3D images.
  • WPF you can create Windows App, Navigation app and XBAP (IE based) application With Silverlight you can create only XAP (Browser based application.).
  • WPF supports 3 types of routed events (direct, bubbling, and tunneling). Silverlight supports direct and bubbling only.
  • Silveright does not support MultiBinding.
  • Silverlight supports the XmlDataProvider but not the ObjectDataProvider. WPF supports both.


2. Ques: What is use of resources in WPF  ? How many types of resources in WPF ?
Answer:
Resources in WPF allow you to set the properties of multiple controls at a time. For example you can set the background property on several elements in a WPF application using a single resource.

There are two types of resource in wpf 
Static Resource
Dynamic Resource

Static Resource:The value of StaticResource is determined at the time of loading.

Ex: in Window1.xaml file inside the Grid

<Grid.Resources>
            <SolidColorBrush x:Key="lblbgcolor" Color="Blue"/>
</Grid.Resources>
       <Label Name="lbl" Margin="71,44,77,0" Background="{StaticResource lblbgcolor}" Height="49" />


Dynamic Resource: Dynamic Resource we use in a situation where we want to change the value of property at run time.
EX:  Window1.xaml file inside 
<Window.Resources>
        <SolidColorBrush x:Key="brush" Color="Red" />
</Window.Resources>
    <Button x:Name="btn" Content="Click Me" Click="Button_Click" Background="{DynamicResource brush}" Height="100" Width="100" />

In Code behind
private void Button_Click(object sender, RoutedEventArgs e)
{
    this.btn.SetResourceReference(BackgroundProperty, "brush");
}


3. Ques: What is the INotifyPropertyChanged Interface?
Answer:
The INotifyPropertyChanged interface is used to notify clients, typically binding clients, that a property value has changed.

For example, consider a Person object with a property called FirstName. To provide generic property-change notification, the Person type implements the INotifyPropertyChanged interface and raises a PropertyChanged event when FirstName is changed.


4. Ques: What are the Advantages of dependency properties in WPF ?
Answer:
Advantages of a Dependency Property given bellow

Less memory consumption
The Dependency Property stores the property only when it is altered or modified. Hence a huge amount of memory for fields are free.

Property value inheritance 
It means that if no value is set for the property then it will return to the inheritance tree up to where it gets the value.

Change notification and Data Bindings
Whenever a property changes its value it provides notification in the Dependency Property using INotifyPropertyChange and also helps in data binding.

Participation in animation, styles and templates 
A Dependency Property can animate, set styles using style setters and even provide templates for the control.

CallBacks 
Whenever a property is changed you can have a callback invoked.

Resources 
You can define a Resource for the definition of a Dependency Property in XAML.

Overriding Metadata 
You can define certain behaviors of a Dependency Property using Property Metadata. Thus, overriding a metadata from a derived property will not require you to redefine or re-implement the entire property definition.


5. Ques:  What are the core WPF assemblies?
Answer:
The core WPF assemblies are
  • WindowsBase.dll: This is the core types constituting the infrastructure of WPF API.
  • PresentationCore.dll: It defines numerous types constituting foundation of WPF GUI layer.
  • PresentationFoundation.dll: It defines WPF control types, animation & multimedia support, databinding suport and other WPF services.
  • Besides these three libraries WPF also uses an unmanaged binary called milcore.dll which acts as abridge between WPF assemblies and DirectX runtime laye

6. Ques: What are the types of binding in WPF?
Answer:
There are four types of data binding modes in WPF.
One-Way
Two-Way
OneWayToSource
OneTime

OneWay: The target property will listen to the source property being changed and will update itself. If you programmatically change the ViewwModel's UserName property, it will reflect in the text box. This is of intermediate cost as the binding system watches only Source for changes.

TwoWay: The target property will listen to the source property being changed and will update itself. AND The source property will listen to the target property being changed and will update itself. Both the TextProperty and the UserName property will remain in sync and will update each other if one changes. This is most costly as the binding system has to watch both sides for change.

OneWayToSource: The Source property will change if the target property is changed. If the user changes the TextProperty, the UserName property will take up the changed value. This again is of intermediate cost as the binding system watches only Target for changes.

OneTime: This happens only once during the lifetime of Binding, the Target property will be updated with the Source property when the Binding happens. This is least costly and is advisable for scenarios where you have static data to be shown e.g. Label, TextBlock etc.


7. Ques: What are Triggers and its type in WPF?
Answer:
The WPF styling and templating model enables you to specify Triggers within your Style.
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.

There are three types of trigger in wpf
  • Property triggers 
  • Data triggers 
  • Event triggers
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.


8. Ques:  How to Creating Windows Forms Controls Dynamically in WPF ?
Answer:
Step to Creating Windows Forms Controls Dynamically

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.

Sunday, 26 February 2017

WPF interview question Part 2

1. Ques: What are the common assemblies used in WPF?
Answer:
 WPF has three common assemblies. These are as below,
  • WindowsBase.dll:- This assembly defines the core types that constitute the infrastructure WPF API.
  • PresentationCore.dll:- As the name specifies this assembly specifies different types for WPF GUI layer.
  • PresentationFoundation.dll:- It defines the WPF control types, animation, multimedia and data binding support. It also specifies some other functionality. 

2. Ques: What are Freezable objects in WPF ?
Answer:
.NET 3.5 introduces a new type of objects called freezable objects. An object, which has its state locked down, so that it becomes unchangeable, is known as a freezable object. These objects are usually related to graphics sub-system. A freezable object has two states – frozen and unfrozen. When an object is frozen, it cannot be modified and it cannot even fires events but the unfrozen state, it works same as a normal object. Such objects perform better. It is also safer if they are required to be shared between threads.
Some freezable objects are – Brush, Pen, Geometry, Transform, and AnimationTimeline.


3. Ques: What are important Namespace in WPF ?
Answer:
The major subsystems are:
  • System.Windows:- It is the core namespace of WPF. You will find all the base classes such as, Application, DependencyObject , DependencyProperty and FrameworkElement here.
  • System.Threading.DispatcherObject: Most of the WPF classes derive from the DispatcherObject object to provides the Thread-Safe environment.
  • Windows.FrameworkElement: It is a base class that is inherited by all of the layout controls and other WPF Controls. FrameworkElement provides functionality related to the logical tree, styling, data binding and lifetime events.
  • System.Windows.Controls:- This namespaces contains all the controls of WPF. You will also find classes to work with complex controls such as PopUp, ScrollBar, StatusBar, TabPanel, etc. as well as all the basic controls.
  • System.Windows.Data:- This namespace is used by WPF for Data Binding. 
  • System.Windows.Input:- This namespace provides several classes for command handling, keyboard input, etc.
  • System.Windows.Markup:- This namespace helper classes for XAML markup code.
  • System.Windows.Media:- This namespace is responsible to work with images, audio, and multimedia.
  • System.Windows.Shapes:- This namespace provides core classes for UI such as Line, Rectangle, Ellipse, etc. 
  • System.Windows.Threading:- This namespace provides classes to work with multiple threads. 
  • System.Windows.Navigation:- This namespace provides classes for navigation between WPF pages particularly when working with Web Applications.
  • System.Windows.UIElements:- UIElement is a base class for most of the objects that have visual appearance and can process basic input.

4. Ques: What are Attached Properties in WPF ?
Answer:
Attached properties are a special kind of DependencyProperties. They allow you to attach a value to an object that does not know anything about this value.
For example, if the button element is located inside a Canvas element. the button has Top and Left properties that are prefixed with the parent element's name. Such a property is known as attached property.

5. Ques: What is ObservableCollection?  What are the uses of ObservableCollection.
Answer:
An ObservableCollection is a dynamic collection of objects of a given type. Objects can be added, removed or be updated with an automatic notification of actions. When an object is added to or removed from an observable collection, the UI is automatically updated. This happens because, when binding to an observable collection, WPF automatically adds a CollectionChanged event handler to the ObservableCollecion's events.

The ObservableCollection class exists in the System.Collections.ObjectModel namespace.

One of the biggest uses is that you can bind UI components to one, and they'll respond appropriately if the collection's contents change. For example, if you bind a ListView's ItemsSource to one, the ListView's contents will automatically update if you modify the collection.

6. Ques: How many types of Documents are supported by WPF ?     
Answer:   
WPF supports two types of documents
Fixed format Document -It is just like PDF Format. they display content regardless of screen size and resolution.
Flow format Document. -It adjust depending on screen size and resolution.


7. Ques: What are the types of windows in WPF?
Answer:   
WPF has three types of windows:
  • Normal Window
  • Page Window
  • Navigate Window

8. Ques: What are the advantage of using XAML?
Answer:   
The advantage of using XAML are given bellow
  • XAML code is clear to read, and they are short
  • Separation of designer code and logic
  • Tools like expression blend used for graphical design require XAML as source
  • It clearly separates the roles of designer and developer

9. Ques : What are the different types of brushes that WPF offers?
Answer:   
There are six different types of brushes

  • SolidColorBrush
  • LinearGradientBrush
  • RadialGradientBrush
  • DrawingBrush
  • VisualBrush
  • ImageBrush

10. Ques : Question:What are Templates and its type in WPF?
Answer:   
A Template is used to change how a control looks.
Types of templates:
Control template: How a control is rendered and behaves by specifying the visual structure and behavioral aspects.
Data template: To specify the visualization of data objects.
Hierarchical data template: Used over hierarchical structure like Tree View and Menu.

Saturday, 18 February 2017

WPF interview question Part 1

1. Ques: Explain the architecture of WPF ?
Answer:


WPF architecture 3 layered architecture which have Managed layered, Unmanaged layered, Core API layered 

Managed layered : This layer has tow part Presentation Framework and Presentation Core.
  • Presentation Framework: This section has features like application controls , layouts . Content etc which helps you to build up your application. PresentationFamework DLL is responsible for this layer. 
  • Presentation Core: This is a low level API exposed by WPF providing features for 2D , 3D , geometry etc.
Unmanaged layered: this layer also called as MiliCore API Layer. Mili 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.

Core API layered : This layer has OS core components like Kernel, User32, GDI, Device Drivers, Graphic cards etc. These components are used by the application to access low level APIs. User32 manages memory and process separation.


2. Ques:What is dependency property? What are the Advantages of a Dependency Property and why we use it?
Answer:
  • Dependency property is a specific type of property which extends the CLR property. 
  • It is a property whose value depends on the external sources, such as animation, data binding, styles, or visual tree inheritance.
  • Dependency property also has the builtin feature of providing notification when the property has changed, data binding and styling.
Advantages of a Dependency Property:-
  • Less memory Uses:The Dependency Property stores the property only when it is altered or modified. Hence a huge amount of memory for fields are free.
  • Property value inheritance:It means that if no value is set for the property then it will return to the inheritance tree up to where it gets the value.
  • Change notification and Data Bindings:Whenever a property changes its value it provides notification in the Dependency Property using INotifyPropertyChange and also helps in data binding.
  • Participation in animation, styles and templates:A Dependency Property can animate, set styles using style setters and even provide templates for the control.
  • CallBacks: Whenever a property is changed you can have a callback invoked.
  • Resources:You can define a Resource for the definition of a Dependency Property in XAML.
Dependency Property can used for -
  • If you want to set the style
  • If you want data binding
  • If you want to set with a resource (a static or a dynamic resource)
  • If you want to support animation

3. Ques: What is routed event ?
Answer:
A routed event is event that can invoke handlers on multiple listeners in an element tree rather than just the object that raised the event. RoutedEvents has three type
  • Direct Event
  • Bubbling Event
  • Tunnel Event
Direct event is similar to events in Windows forums which are raised by the element in which the event is originated.

Bubbling Event
A bubbling event begins with the element where the event is originated. Then it travels up the visual tree to the topmost element in the visual tree. So, in WPF, the topmost element is most likely a window.

Tunnel Event
Event handlers on the element tree root are invoked and then the event travels down the visual tree to all the children nodes until it reaches the element in which the event originated.


4. Ques: Explain the WPF object hierarchy?
Answer:-

Object
: - Object is the main class for all .Net classes and  it is the base class for all wpf classes.

Dispatcher: -It is the base class for any object which wants to accessed only by its own thread. It means the object that derives from DispatcherObject is going to be accessed by only its own thread and if any other thread tries to access that object gives error. In this way DispatcherObject object provides Thread-Safe environment. Most of the WPF classes derive from the DispatcherObject object to provides the Thread-Safe environment.

DependencyObject: If any object supports dependency properties in WPF means it derives from the base class DependencyObject. DependencyObject provides GetValue() & SetValue() methods that are central to dependency properties.

Visual: - Visual is the base class for all objects which has their own visual representation.

UI Element: - It is the base class for all Visual objects with supported events, command binding, layout, focus...etc.

Framework element: - This class supports for templating , styles , binding , resources and common mechanisms for Windows-based controls such as tooltips and context menus.

5. Ques: Explain the Framework element hierarchy?
Answer:-





























6. Ques: what is prism in WPF ?
Answer:
Prism is the Microsoft Patterns and Practices Team official guidance for building "composite applications" in WPF and Silverlight. Its intended to provide guidance on the best practices for building large scale applications which are flexible in terms of development and maintainability.


7. Ques: What is the Application Lifetime in WPF?
Answer:
 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 canchoose 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 yourApplication-derived class, e.g. OnStartup