Saturday, 21 September 2013

WPF Knowledge - Part2

Hi DotNet Lover’s, It’s my pleasure to serve these question to provide a platform where you can get everything in one umbrella on Microsoft DotNet Technology, In my previous post, have shared a fundamental knowledge on WPF and well explained, what is the purpose to make WPF as a next generation UI. Now, we will learn about various advance concepts in WPF in the coming post. 

Why Microsoft proposed WPF over WinForms? 
There are a lot of differences between WPF and Window Forms. WinForms is past and WPF is the latest technology equipped with advanced features which Window Forms lack .WPF supports UI, media, documents, hardware acceleration, vector graphics, scalability to different form factors, interactive data visualization, and superior content readability 

1. Windows form flickers due to use low level API and overcome by using Directx in WPF 
2. Windows form does not use Directx while WPF does So, In WPF can create very rich UI with visual stunning experience included fixed and flow layout, 2d & 3d vector graphics and media streaming facility 
3. Windows form .NET source code will convert to IL then converted into the source code while In WPF. Xaml will parse during run time and directly converted into BAML file 
4. In WPF Highly advance data binding is possible and WPF is a great platform to develop applications/components utilizing composition code reuse, 
5. The implementation of DockPanel Suite is tightly coupled each other, due to the limitation of Windows Forms 

Note: WPF is a replacement for Winforms. What window forms can do, WPF can, but what WPF can do, Windows forms can’t do all. 

What are the main important concepts in WPF? 
1) XAML 2) Bindings 3) Templating and Styles 

What is XBAP (XAML Browser Application)? 
1. WPF web based Application is called XBAP. In other words, XBAP allows for WPF applications to be used inside a browser, and have a .xbap file extension 2. The .NET framework is required to be installed on the client system. Hosted applications run in a partial trust sandbox environment. They are not given full access to the computer's resources and not all of WPF functionality is available. 

The power of this WPF support is that the exact same programming model is used for a XAML Browser Application as for a standard Windows application. Therefore, creating an XBAP isn’t much different than creating a standard Windows application. 

The main differences are as follows: 
1. Not all features in WPF or the .NET Framework are accessible (by default). 2. Navigation is integrated into the browser using hyperlink (for Internet Explorer 7 or later). 
3. They have restrictive permissions: example: we cannot do database connectivity, file handling or alter registries using XBAP's 
4. Deployment is handled differently. 


What is difference between Loose XAML and Compiled XAML? 
Loose XAML version provides a dynamic way to load, but provides poor performance due to the fact that the XML file is parsed at runtime and change the view while compiled XAML version provides better performance since it is pre-tokenized binary version of the XAML file, hence it is smaller and can be loaded faster, but it is not dynamic. 
1. Loose XAML parsed at runtime while compile XAML marked as a page in Visual Studio Studio ( in MSBuild) 
2. Loose XAML deployed as a simple XML while Complie Xaml deployed as a BAML 
3. Loose XAML remotely or embedded into assembly while Compile XAML embedded as a assembly resource 
4. A loose XAML file can't include the x:Class XAML keyword while A compiled XAML file (BAML) can emit code, by using x:Class, or by registering events 
5. Loose XAML file will be loaded by Calling XamlReader.Load() method, casting the return value into the root element while Compile XAML file will be loaded by calling the Application.LoadComponent() 


What is BAML (Binary Application MarkUp Langauage)? 
1. BAML means XAML has been parsed, tokenized, and converted into binary form. Although any chunk of XAML can be represented by procedural code, the XAML-to-BAML compilation process does not generate procedural source code. So, BAML is not like Microsoft intermediate language (MSIL); it is a compressed declarative format that is faster to load and parse (and smaller in size) than plain XAML. 
2. BAML is just an implementation detail of the XAML compilation process without any direct public exposure, so it could be replaced with something different in the future. Nevertheless, it’s interesting to be aware of its existence. 


What are the Features of BAML? 
1. BAML is compact hence it can be downloaded faster (good for XBAP applications) 
2. BAML is less security threat than code execution (good for XBAP applications) 
3. BAML can be localized (different languages) without requiring recompilation. 


Can BAML be decompiled back into XAML? 
YES because an instance of any public .NET class can be serialized as XAML, regardless of how it was originally declared. 


What is Dependency Properties and its Use in WPF? 
1. Dependency properties are used to enable styling, automatic data binding, templates, animation etc. 
2. All types that want to use DependencyProperties must derive from'DependencyObject' class. 
3. The value of a DependencyProperty is resolved dynamically when read. 
4. The value of a dependency property is stored in a dictionary of keys and values provided by 'DependencyObject' class. 

Advantages of Dependency Property 
1. Property Value Inheritance : Value of a Dependency property can be overridden in the hierarchy 
2. Data Validation: Triggered automatically whenever the property value is modified. 
3. Participate in Animation, Style, Template, Data Binding, Call back, resources and design supports 


What information do you need to define a dependency property? 
1. Property Name 
2. Type 
3. ControlType that it applies to MetaData (which can handle the change events and set a default value) 


Difference between CLR Property, Dependency property and Attached Property? 

CLR Property: 
1. By default, notification features is not available, to achieve this feature need to use INotifyPropertyInterface to notify in the User Interface 
2. Value will get from the private member field 

Dependency property: 
1. Dependency property is associated with that particular element and can help in notification of changes and reacting to that change. 
2. Dependency property works with most of the WPF inbuilt features like built in notification, value inheritance, uses dependencyproperty.register 

Attached property: 
1. 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. 
2. Attached properties allows container to create a property which can be used by any child UI elements 
3. Used dependency property.registerattach 
4. Attached properties are used a lot for layout-related information i.e. Layout, Canvas, DockPanel etc In All property, value will get during runtime when getvalue or setvalue called. 


What is the importance of INotifyPropertyChanged in WPF? 
1. This interface contains a single event called "PropertyChanged" of the delegate type "PropertyChangedEventHandler". 
2. 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? 
1. 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. 
2. "ObservableCollection" is a built-in class which implements the INotifyCollectionChanged interface. 


What kind of properties in the UI can be updated by INotifyPropertyChanged? 
Dependency property 


What is MVVM Pattren? 
Model-View-ViewModel (MVVM) pattern splits the User Interface code into 3 conceptual parts - Model, View and ViewModel. 
Model is a set of classes representing the data coming from the services or the database. 
View is the code corresponding to the visual representation of the data the way it is seen and interacted with by the user. 
ViewModel serves as the glue between the View and the Model. It wraps the data from the Model and makes it friendly for being presented and modified by the view. ViewModel also controls the View's interactions with the rest of the application 


What is the difference between MVVM and MVC patterns? 
MVVM Model-View ViewModel is similar to MVC, Model-View Controller.The controller is replaced with a View Model. The View Model sits below the UI layer. The View Model exposes the data and command objects that the view needs. You could think of this as a container object that view goes to to get its data and actions from. The View Model pulls its data from the model. 


Difference between Events and Commands in WPF MVVM Architecture? Commands are very powerful and useful feature of WPF. Cut, Copy and Paste are basic inbuilt commands in WPF. Commands and Events are related to each other to a large extent but the main concern there is that events doesn't fit well into MVVM paradigm. So we generally prefer to use commands in WPF MVVM architecture. Commands provide a lot of advantages over events. Some of them are listed below: 
1. Commands in WPF allow you to move the implementation of a command handler to the buisness layer. 
2. You can bind WPF command in the view (XAML) and receive the event raised. This way you do not have to use code behind which is a no-no in MVVM. 
3. In events, an action is tightly coupled with its source and can't be reused freely; Using commands you can easily maintain various actions in a single place and reuse them anywhere in application. 
4. Commands are similar to Events except we can associate any number of UI Controls or Input Gestures to a command and bind that command to a handler that is executed when control are activated or gestures are performed. 


What is relay command and ICommand with respect to MVVM in WPF? 
A RelayCommand is a type of command which implements the Interface ICommand. ICommand is an Interface which must be implemented by the entire command event. ICommand can be used as WPF command. You can pass methods as parameters to the RelayCommand constructor. In WPF or silverlight, Relay command requires two basic information: 
1. What code should run when the command is executed (the _execute action) 2. What code should run to determine if this command can be executed (the _canExecute predicate) 


How to host 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. 


I can’t seem to find System.Windows.Forms.Integration, where do I find it?  
The System.Windows.Forms.Integration namespace is defined in WindowsFormsIntegration.dll which currently ships in the WinFX SDK, not in the standard redist. Therefore, the file will be found in “\Program Files\Reference Assemblies\ Microsoft\Avalon\v2.0.50215″. 


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. 


Can I use Windows Forms in a WPF application? 
Absolutely. You can have a WPF application pop a Windows Form much in the same way that you can pop a WPF window from a Windows Forms application. Furthermore, you can place Windows Forms controls side-by-side with WPF controls on a WPF window or page by using the WindowsFormsHost control that will ship as part of the interoperability layer. 


How do I retrieve command-line arguments in my WPF application? Command-line arguments are typically retrieved via a string array parameter passed to Main,but the common way to define WPF applications doesn’t allow you to implement the Main method. You can get around this in two different ways. One way is to forego defining an Application-derived class in XAML, so you can manually define the Main method with a string array parameter. The easier way, however, is to simply call System.Environment.GetCommandLineArgs at any point in your application, which returns the same string array you’d get inside Main. 


What is the Application Lifetime in WPF? 
The main events fired from Application include: 
1. Startup - Application is starting up. 
2. Exit – Fired when an application is shutting down. 
3. Activated – Fired when an application gets focus, i.e. becomes the foreground application 
4. Deactivated – Fired when application loses focus, i.e. is no longer the foreground application 
5. DispatcherUnhandledException – Fired when an exception is thrown, but not yet handled. You can choose to handle the exception or not 
6. 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 rendere 

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 

Tricky Question: 
1. What is a main advantage of WinForms over WPF? 
Generally uses fewer computer resources 

2. Can you create Windows Service Using WPF? 
No, you can’t build the windows Services using WPF. WPF is presentation technolgy, where as Windows service requires certain permission to perform the certain GUI related operations. If the Windows service does not have the appropriate permissions, there may be unexpected results 

Reference 
1. http://blogs.microsoft.co.il/blogs/tomershamam/archive/2007/05/25/Compiled-XAML-_3D00_-BAML-not-IL.aspx 
2. http://www.msdn.microsoft.com/ 
3. http://www.dotnetfunda.com/

No comments:

Post a Comment