WPF Interview Questions and Answers
1. What is a WPF Animation?
--Windows Presentation Foundation (WPF) is a next-generation application development tool that ties together many different technologies: document data, audio, video, 2D/3D graphic rendering, etc. It has the ability to perform animation on the objects available, and this article will show some of the various animations that can be performed.
--Prior to WPF, Microsoft Windows developers had to create and manage their own timing systems or use special custom libraries. WPF includes an efficient timing system that is exposed through managed code and Extensible Application Markup Language (XAML) and that is deeply integrated into the WPF framework. WPF animation makes it easy to animate controls and other graphical objects.
--WPF handles all the behind-the-scenes work of managing a timing system and redrawing the screen efficiently. It provides timing classes that enable you to focus on the effects you want to create, instead of the mechanics of achieving those effects. WPF also makes it easy to create your own animations by exposing animation base classes from which your classes can inherit, to produce customized animations. These custom animations gain many of the performance benefits of the standard animation classes.
--Creating animations in WPF is easier than it sounds. No explicit use of timers is required, in fact you can create many useful animations using pure markup (XAML). Perhaps the best use of animations is to spice up UI. Most properties of objects can be animated (e.g. width, height, color, position, etc). Want your button to fade to a new color when a user mouses over it? Want your text to quickly slide into view rather than just abruptly appear? Of course once you see how easy WPF animations are you might be in danger of overdoing it and creating some truly garish and annoying apps but that's why you like programming
Here is an overview of WPF Animation. Have a look and explore yourself.
2. How to host a WPF application in a browser?
XAML browser applications (XBAPs) combines features of both Web applications and rich-client applications. Like Web applications, XBAPs can be published to a Web server and launched from Internet Explorer. Like rich-client applications, XBAPs can take advantage of the capabilities of WPF. Developing XBAPs is also similar to rich-client development. This topic provides a simple, high-level introduction to XBAP development and underscores where XBAP development is different than standard rich-client development
Windows Presentation Foundation (WPF) Host (PresentationHost.exe) is the application that enables WPF applications to be hosted in compatible browsers (including Microsoft Internet Explorer 6 and later). By default, Windows Presentation Foundation (WPF) Host is registered as the shell and MIME handler for browser-hosted WPF content, which includes:
• Loose (uncompiled) XAML files (.xaml).
• XAML browser application (XBAP) (.xbap).
For files of these types, Windows Presentation Foundation (WPF) Host:
• Launches the registered HTML handler to host the Windows Presentation Foundation (WPF) content.
• Loads the right versions of the required common language runtime (CLR) and Windows Presentation Foundation (WPF) assemblies.
• Ensures the appropriate permission levels for the zone of deployment are in place.
3. Explain Loose XAML vs. XBAP vs. ClickOnce.
Loose XAML is basically the capability that allows you to open any XAML file on your hard disk (or a hyperlink on a web page) and "run" it within a browser without compilation. You can't embed any code within the XAML document - it has to just be pure XAML itself - so it's not suitable for building a large-scale application; however it's certainly a nice little feature for trying out a little XAML code or for something like a small animation that you might embed into a web page
Loose XAML files are markup-only files that use XAML to define the visual content to be rendered. They are not compiled with an application, but exist as discrete files on a Web server. Like WBAs, they can be hosted top-level in the browser or within an HTML IFrame. Loose XAML always runs in the Internet sandbox.
Because loose XAML is markup-only, it is constricted to content-only rendering. This means that loose XAML cannot support things like button event handlers or preserve application state on the client. However, they are a powerful and low-cost way of creating dynamic, server-generated WPF visuals.
Here is a video presentation on how to create and browse loose XAML files. Hope you find it useful.
XAML Browser Applications: XBAPs
While standalone WPF applications offer the most capability, they're not always the right choice. Plenty of situations make more sense with a client that runs in a Web browser rather than as a Windows application. To allow these clients to present modern user interfaces, WPF provides XBAPs.
As the figure below shows, an XBAP runs inside Internet Explorer. XBAPs can act as a client for Web applications built using ASP.NET, JavaServer Pages (JSP), or other Web technologies. To communicate back to this Web application, the XBAP can use HTTP or SOAP. Whatever server platform is used, an XBAP is always loaded via ClickOnce. It presents no dialogs or prompts to the user during this process, however; an XBAP loads just like a Web page. Because of this, XBAPs don't appear on the Start menu or in Add/Remove Programs.
Figure 7. An XBAP running inside Internet Explorer
While it's not strictly required, XBAPs typically present a navigational interface to the user. This lets the application behave like a Web client, which is probably what the user expects. In Internet Explorer 7, an XBAP uses the forward and back buttons of the browser itself, and the XAML pages a user accesses will appear in the browser's history list. In Internet Explorer 6, the XBAP displays its own forward and back buttons, along with maintaining its own history list. The XBAP can determine which environment it's running in and do the right thing; the developer need not create different versions for each browser.
Because it's loaded from the Web and runs inside a browser, an XBAP is given only limited trust by the .NET Framework's code access security. Because of this, there are a number of things that a standalone WPF application can do that an XBAP cannot. For example, an XBAP deployed from the Internet zone can't do any of the following:
- Create standalone windows.
- Display application-defined dialogs.
- Display a Save dialog launched by the XBAP itself.
- Access the file system beyond a limited Isolated Storage area.
- Act as a UI automation client.
- Use WCF. WCF applications must have full trust, and so XBAPs deployed from the Internet can't use it. They can instead use ASP.NET Web services, commonly known as ASMX, to communicate with the Web application from which they were loaded.
- Use any user interface code created with Windows Forms, Microsoft Foundation Classes (MFC), or direct Win32 calls. Although standalone WPF applications can interoperate with all of these older technologies, as described later, none of them are allowed in an XBAP's limited trust environment.
- Use unmanaged code.
As mentioned earlier, it's possible to use the same code base for both a standalone WPF application and an XBAP. To allow this, a developer might use conditional compilation, wrapping any functionality that's not allowed in an XBAP inside ifdefs. The XBAP version can do most of what the standalone application can do, including displaying documents, using two-dimensional and three-dimensional graphics, playing video and audio, and more. It can also take advantage of whatever graphics hardware is available in the machine it's running on.
Along with XBAPs, it's also possible to display pure XAML pages directly in Internet Explorer. Referred to as loose XAML, this can be useful for showing static pages in the browser. Handling events requires code, however, which means creating an XBAP.
XBAPs allow developers to use most of WPF's capabilities in a browser application. They also allow a common programming model, using mostly the same code, for standalone applications and browser applications. For Web applications whose clients target newer Windows platforms, XBAPs are likely to be an attractive choice.
Simply stated, a ClickOnce application is any Windows Presentation Foundation, Windows Forms, or console application published using ClickOnce technology. You can publish a ClickOnce application in three different ways: from a Web page, from a network file share, or from media such as a CD-ROM. A ClickOnce application can be installed on an end user's computer and run locally even when the computer is offline, or it can be run in an online-only mode without permanently installing anything on the end user's computer.
ClickOnce applications can be self-updating; they can check for newer versions as they become available and automatically replace any updated files. The developer can specify the update behavior; a network administrator can also control update strategies, for example, marking an update as mandatory. Updates can also be rolled back to an earlier version by the end user or by an administrator.
Because ClickOnce applications are isolated, installing or running a ClickOnce application cannot break existing applications. ClickOnce applications are self-contained; each ClickOnce application is installed to and run from a secure per-user, per-application cache. By default, ClickOnce applications run in the Internet or Intranet security zones. If necessary, the application can request elevated security permissions.
ClickOnce is a deployment technology that enables you to create self-updating Windows-based applications that can be installed and run with minimal user interaction. ClickOnce deployment overcomes three major issues in deployment:
Difficulties in updating applications. With Microsoft Windows Installer deployment, whenever an application is updated, the user must reinstall the whole application; with ClickOnce deployment, you can provide updates automatically. Only those parts of the application that have changed are downloaded, and then the full, updated application is reinstalled from a new side-by-side folder.
Impact to the user's computer. With Windows Installer deployment, applications often rely on shared components, with the potential for versioning conflicts; with ClickOnce deployment, each application is self-contained and cannot interfere with other applications.
Security permissions. Windows Installer deployment requires administrative permissions and allows only limited user installation; ClickOnce deployment enables non-administrative users to install and grants only those Code Access Security permissions necessary for the application.
For More Information:
4. How to Create a collection in XAML
If you have a custom collection definition, you could use it directly in XAML syntax. This produces more readable XAML. Here, a ComposerCollection has been defined in code and is used as follows:
<custom:Composer Name="Mozart, Wolfgang Amadeus" Birth="1756"/>
<custom:Composer Name="Górecki, Henryk Mikolaj" Birth="1933"/>
<custom:Composer Name="Massenet, Jules" Birth="1842"/>
5. What is the diffeence? Name vs. x:Name
x:Nameis a xaml concept, used mainly to reference elements. When you give an element the x:Name xaml attribute, "the specified
x:Namebecomes the name of a field that is created in the underlying code when xaml is processed, and that field holds a reference to the object." (MSDN) So, it's a designer-generated field, which has internal access by default.
Nameis the existing string property of a
FrameworkElement, listed as any other wpf element property in the form of a xaml attribute.
As a consequence, this also means
x:Namecan be used on a wider range of objects. This is a technique to enable anything in xaml to be referenced by a given name.
Source: stackoverflow.com There is basically no difference between the two.
The "x:Name" expression is used in XAML to assign a name to an object that will be used to access the object from the code-behind.
Many classes of the framework expose a Name property, which does exactly this. For these classes, both x:Name and the Name property can be used intercha Source: WPFwiki.com
6. What is ContentPresenter?
7. What is the Difference between TextBlock and Label in WPF.
8. What is Virtualization in WPF?
9. What is VirtualizingPanel?
10. What is VirtualizingStackPanel?
11. How to create a custom VirtualizingPanel?
12. Where is the data grid or gridview control in WPF?
13. Explain Binding vs. TemplateBinding in WPF.
14. What are the Various ways to bind?
15. Explain Source vs. RelativeSource vs. ElementName
16. Explain Logical Tree vs. Visual Tree in WPF
17. Explain DependencyProperty vs. CLR property
18. What is a DependencyObject?
19. What is an Adorner?
20. What is a Decorator?
21. What is a WPF Style?
22. Explain Style vs. Template?
23. How do I create a DataTemplate?
24. What is a Resource Dictionary Declaration?
25. What is Inline Declaration?
26. What is a read-only attached property?
27. How to declare a read-only attached property?
28. How to declare a dependency property?
29. How can you send input programatically?
30. How will you simulate basic keyboard events in WPF?
31. What are the Alternative ways to simulate input?
32. How to simulate basic mouse events?
33. Is there a 'Browse for folder' dialog in WPF?
34. What is ItemsControl?
35. What is the difference between Binding Source and Binding Target
36. What is the difference between Binding vs. BindingExpression
37. How can Expression Blend use DataBinding to a business object?
38. How to customize the way data is displayed
39. Explain ControlTemplate vs. ItemsPresenter vs. IsItemsHost
40. Explain Margin vs. Padding in WPF layout
41. Explain Right-to-left vs. custom controls in WPF
42. How does layout work?
43. What is the difference between StaticResource vs. DynamicResource
44. What is RoutedCommand?
45. What is an attached property?
46. What is a read-only dependency property?
47. How to declare an attached property?
48. How to declare a read-only dependency property?
49. How to create Custom Commands?
50. What is a RoutedEvent?
51. What are the Available Routing Strategies?
52. How to add an Event Handler using XAML?
53. How to add an Event Handler using Code?
54. How to add CLR accessors to a RoutedEvent?
55. Source vs. OriginalSource
56. How to raise a RoutedEvent?
57. How to create Custom Routed Events?
58. What is a DataTemplate?
59. What is a ControlTemplate?
60. Explain what a Gesture is?
61. What are the types of Gesture?
62. How to Add Gestures?
63. What is the difference? Inline Declaration vs. Resource Dictionary Declaration
64. How to create a ControlTemplate
65. How do I create a style or template?
66. Explain Trigger.
67. How do I use my custom control/class in XAML?
68. What is XAML?
69. How to create an object with non-default constructor?