Sunday, February 16, 2020

wpf Interview question and answer

1.
How WPF Application (Architecture) Works?

The major components of WPF are
-PresentationFramework,
-PresentationCore, and
-Milcore unmanaged component
Milcore is written in unmanaged code in order to enable tight integration with DirectX. All display in WPF is done through the DirectX engine, allowing for efficient hardware and software rendering. WPF also required fine control over memory and execution. The composition engine in milcore is extremely performance sensitive, and required giving up many advantages of the CLR to gain performance.


2.
what is routed events?

Routed events are events which navigate up or down the visual tree according to their RoutingStrategy. The routing strategy can be bubble, tunnel or direct. You can hook up event handlers on the element that raises the event or also on other elements above or below it by using the attached event syntax: Button.Click="Button_Click". Routed events normally appear as pair. The first is a tunneling event called PreviewMouseDown and the second is the bubbling called MouseDown. They don't stop routing if the reach an event handler. To stop routing then you have to set e.Handled = true;
Tunneling The event is raised on the root element and navigates down to the visual tree until it reaches the source element or until the tunneling is stopped by marking the event as handeld. By naming convention it is called Preview... and appears before corresponding bubbling event.
Bubbling The event is raised on the source element and navigates up to the visual tree until it reaches the root element or until the bubbling is stopped by marking the event as handled. The bubbling event is raised after the tunneling event.
Direct The event is raised on the source element and must be handled on the source element itself. This behavior is the same as normal .NET events.
3.
What are Value Converters?

A value converter converts one type to another. Converters are frequently used in data binding scenarios where the target type and the source type are not the same. Value converters are used heavily in WPF because XAML attributes are strings, but these strings often need to be converted into objects. For instance, the string value of red for a background in XAML needs to be converted to a Color object representing red.
If you want to databind two properties that have incompatible types, you need a piece of code in between, that converts the value from source to target type and back. This piece of code is called ValueConverter. A value converter is a class, that implements the simple interface IValueConverter with the two methods object Convert(object value) andobject ConvertBack(object value).
4.
What is the difference between X:name and x:key?

X:name idetifies a UIElment in XAML Tree
X:Key identifies objects in resources section, can be accessed by resourceDictionary
5.
Explain WPF’s 2-Pass layout engine?

In some cases, an element may know exactly what size it should be (because it’s Width and Height properties have been explicitly set). But very often, the size of an element is determined by its content. To enable this “size to content” feature, the WPF layout engine uses a 2-pass layout cycle to size and position visual elements:
1. First a measure pass is used to determine the desired size of each element.
2. Then an arrange pass is used to explicitly size and position each element.







6.
What is the difference between Logical Tree and Visual Tree?

Logical Tree: XAML is natural for representing a user interface because of its hierarchical nature. In WPF, user interfaces are constructed from a tree of objects known as a logical tree
Visual Tree: A similar concept to the logical tree is the visual tree. A visual tree is basically an expansion of a logical tree, in which nodes are broken down into their core visual components. Rather than leaving each element as a "black box," a visual tree exposes the visual implementation details. Window's default visual tree consists of a Border, AdornerDecorator, two AdornerLayers, a ContentPresenter, and nothing more 

7.
What are the types for data binding modes in WPF?

1.One-way binding:
The data flows from the source to the target,each time a change is made on the source
2.One-time binding:
Sends data from source to the target, however it does this only when the application is started or when the datacontext changes
3.One-way-to-source binding:
sends data from the target to source
4.Two-way binding:(Default)
Sends the source to the target and if there are changes in the target, those values will be sent back to the source.
8.
What are the panels in WPF?

1.CANVAS - Explicitly positioning of controls
2.DOCKPANEL - Similar to windows form docking application
3.GRID - Can arrange child controls with rows and columns
4.STACKPANEL - Used to organize child controls either horizontally or vertically
5.WRAPPANEL - Can arrange child controls from left to right one after the other as long as they fit in to next line









9.
What are the Resources, Styles and Triggers?

Style - element containing setter elements.
Triggers - used to change the look and feel of the controls dynamically.
Resources - Styles and Triggers are stored in the resources
Static Resources - resources are searched at load time
Dynamic Resources - If the resource changes while the program is running
10.
What is CLR Property?

CLR properties are really just safe wrappers around a Private member variable, such that the correct access modifiers can be applied to the property, so we could have a Read or a Read/Write or just a Write property. But basically that's it. This is all CLR properties really do for us.
they are defined like this
private int x;
public int X
{
get { return x; }
set { x = value; }
}
11.
What is dependency property in WPF?

WPF elements are classes with methods, properties and events. Nearly every property of a WPF element is a dependency property. Dependency properties are used with databinding, animation, resources and styles.
Only a class that derive from base class DependencyObject can include dependency properties. For Example

public class MyDependencyObject : DependencyObject
{

public static readonly DependencyProperty SomeStateProperty = DependencyProperty.Register("SomeState", typeof(String), typeof(MyDependencyObject));

public string SomeState
{
get {return (string)this.GetValue(SomeStateProperty);}
set { this.SetValue(SomeStateProperty, value); }
}
}







12.
What is attached property in WPF?

A WPF element can also get features from the parent element. 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.

<Canvas>
<Button Canvas.Top="35" Canvas.Left="45">
Click Me!
</Button>
</Canvas>
13.
What is Property Invalidation?

Its used to update the custom control whenever the property changes.Adding property invalidation callbacks to a custom control is as easy as adding a PropertyChangedCallback object to a PropertyMetadata object and add this PropertyMetadata object to Register() or RegisterAttached() methods.
14.
What are templates?

A template is similar to a style, and two different kinds are available:
Data templates: allow using XAML's DataTemplate element to specify a group of characteristics for how data should be displayed. Colors, alignment, and more can be defined once in a data template, then used elsewhere in an application's user interface.
Control templates: allow using XAML's ControlTemplate element to define the appearance of a control.
15.
What is Resource Dictionary?

The content of the file is just a ResourceDictionary - and because the ResourceDictionary is the root element in the Xml file we need to define the default namespaces, which you would probably be more used to seeing on a new root-level Window or Page when you create a new Xaml file in Visual Studio. The contents of the resource dictionary is a perfectly normal WPF style.
The second step is to merge this resource stored in an external file into one of the resource dictionaries in our application. For this example we'll merge it in to the Application.Resouces collection (located in the App.xaml file). This is done by adding the elements highlighted below which point to the TextStyle.xaml file we created.
16.
what is ObservableCollection?

Represents a dynamic data collection that provides notifications when items get added, removed or when the whole list is refreshed.

17.
what is the diff between Window and Page?

WPF Windows Application --Stand alone application
xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
Title="Window1" Height="300" Width="300">
WPF Browser Application – XBAB
xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
Title="Page1">
18.
what is the diff between user control and Customcontrol in WPF?

--UI which will be used repeatedly in your own project, using UserControl is a much simpler and preferred way.
--a portion of UI and functionality which you believe will be used aross different projects, then using custom control is the right approach.
--A User Control doesn't support ControlTemplates and Themes. But creating a UserControl is much like creating a normal Window with Codebehind. A UserControl exists of a XAML-File and a Codebehind-File. Define Eventhandlers in XAML and implement them in Codebehind.
--A Custom Control supports ControlTemplates and Themes. A Custom Control is a "lookless" Control. The logic is in a Code-File (.cs) and the look is defined in a ControlTemplate that is part of a style. You can define more "theme"-styles for different windows themes.
19.
What are trees in WPF?

Logical Tree and Visual tree.
The logical tree in WPF represents the elements of an application at run time.the logical tree maps very closely to an application's markup definitions.
The visual tree contains all visual elements used in an application's user interface.
20.
what is INotifyPropertyChanged? Morgan(imp)

This interface has only one member - the PropertyChanged event, which you are supposed to call whenever a property changes. This, in turn, will notify WPF to update the controls on the screen.
Example:
public class ConversionOptions : INotifyPropertyChanged
21.
How WPF Application (Architecture) Works?

The major components of WPF are
-PresentationFramework,
-PresentationCore, and
-Milcore unmanaged component
Milcore is written in unmanaged code in order to enable tight integration with DirectX. All display in WPF is done through the DirectX engine, allowing for efficient hardware and software rendering. WPF also required fine control over memory and execution. The composition engine in milcore is extremely performance sensitive, and required giving up many advantages of the CLR to gain performance.



No comments:

Post a Comment