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.

No comments:

Post a Comment

Thank you for comment