Introduction to XAML
XAML stands for Extensible Application Markup Language. Its a simple language based on XML to create and initialize .NET objects with hierarchical relations. Altough it was originally invented for WPF it can by used to create any kind of object trees.
Today XAML is used to create user interfaces in WPF, Silverlight, declare workflows in WF and for electronic paper in the XPS standard.
All classes in WPF have parameterless constructors and make excessive usage of properties. That is done to make it perfectly fit for XML languages like XAML.
Advantages of XAML
All you can do in XAML can also be done in code. XAML ist just another way to create and initialize objects. You can use WPF without using XAML. It's up to you if you want to declare it in XAML or write it in code. Declare your UI in XAML has some advantages:
- XAML code is short and clear to read
- Separation of designer code and logic
- Graphical design tools like Expression Blend require XAML as source.
- The separation of XAML and UI logic allows it to clearly separate the roles of designer and developer.
XAML vs. Code
As an example we build a simple StackPanel with a textblock and a button in XAML and compare it to the same code in C#.
<StackPanel>
<TextBlock Margin="20">Welcome to the World of XAML</TextBlock>
<Button Margin="10" HorizontalAlignment="Right">OK</Button>
</StackPanel>
The same expressed in C# will look like this:
// Create the StackPanel
StackPanel stackPanel = new StackPanel();
this.Content = stackPanel;
// Create the TextBlock
TextBlock textBlock = new TextBlock();
textBlock.Margin = new Thickness(10);
textBlock.Text = "Welcome to the World of XAML";
stackPanel.Children.Add(textBlock);
// Create the Button
Button button = new Button();
button.Margin= new Thickness(20);
button.Content = "OK";
stackPanel.Children.Add(button);
Level 1
- Q: What the first class that is loaded by any new WPF Application project?
A: App. App.xaml and App.xaml.cs. - Q: What is a DependencyProperty?
A: Represents a property that can be set through methods such as, styling, data binding, animation, and inheritance. [1] - Q: Name as many layout controls as you can:
A: Grid, DockPanel, Canvas, WrapPanel, UniformGrid, StackPanel - Q: What tool should you use sketch a mock of your WPF application?
A: SketchFlow
Level 2
- Q: Describe how you would go about writing your own button style?
A: Make sure the interviewee understands the following:
- How to copy the default style using Expression Blend.
- How to change the style
- How there must be a Style for each state: Enabled, Disabled, Mouseover, pushed, etc… - Q: You need to display a list of items. Which controls are possible options? Of those, which would you use and why?
A: ItemsControl, ListBox, ListView, DataGrid.
- Make sure they know that ListBox and ListView can be selected while ItemsControl does not support selection.
- ListView is more feature rich, as it inherits ListBox, which inherits ItemsControl, and adds features.
- Make sure they understand what a DataGrid is and how it is different and why they would display a list in a DataGrid. - Q: What can you do in Expression Blend that you cannot do in Visual Studio?
A: Configure brushes and gradient brushes in the Properties window.
A: Configure Visual States.
A: Configure sample data.
A: Configure Object Timelines
A: Animation
A: Many design features… - What can you do in Visual Studio that you cannot do in Expression Blend?
A: Reformat source code, including XAML, with a hot key.
A: Include Solution Folders.
Level 3
- What are common Localization/Globalization practices for Localizing/Globalizing WPF?
A: 1) Use Resources.resx, 2) Use BAML, 3) Use a ResourceDictionary to manage localizable strings. - Q: What are some best practices when using WPF if you plan to build a Localized/Globalized application?
A: Write UI in XAML
A: Avoid sizing and positionings, but let objects automatically flow.
A: Enable TextWrapping.
WPF Interview Questions – Binding
Level 1
- Q: How do you bind a bool value to Visibility?
A: Add a BoolToVisibilityConverter to your resources and then use it as the converter in your Binding. - Q: What is IValueConverter used for?
A: So that a control or control property can be bound to an element of any data type and the conversion happens in the binding process. - Q: Here is a TextBox. Change it so that it will bind to the ‘Text’ property of any DataContext.
<TextBox />
A: <TextBox Text=”{Binding Path=Text}” /> - Q: What happens if the value you are binding to does not exist?
A: The error is ignored. If running from Visual Studio, a message is logged in the Output window.
Level 2
- You want to use a custom Binding but the control does not support binding in the area you want. What are your options? Which would you choose and why?
A1: Inherit object and add a Dependency Property – If the object is not sealed this is likely an easier option.
A2: Create an attached property – The object might be sealed. - Q: How do you bind a button’s Command property to a method?
A: You don’t. You bind to an ICommand. You create a class that implements ICommand and in that object you connect to your method. - Q: Provide an example of when and why you would use MultiBinding.
A: When you have multiple values that your which to combine.
A: When you want to use String.Format in XAML. - Q: What is the Binding syntax for binding to a Static value?
A: {x:Static s:MyStaticClass.StaticValue2}
Level 3
- Q: Provide a situation in which you have or would use a ConverterParameter.
A: Any time you have a converter that returns something different based on a parameter.
A: Look for them to share an example of needing to do this. - What is the correct syntax for binding to a property of a Singleton?
A: {Binding Source={x:Static sing:MySingletonClass.Instance}, Path=SomeProperty}
No comments:
Post a Comment