Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are XAML markup extensions?

I tried reading the MSDN article on markup extensions, but I can’t find out what they are (the article discusses what they do).

I cannot find a clear explanation of why we need markup extensions. If we can access a control object directly, why would we need a markup extension to access a binding object?

Do we need markup extensions so XAML is aware of the code behind (otherwise there is no way to get access any of the built in classes)? But then how can we access all the control types?

like image 841
mihajlv Avatar asked Jan 02 '12 21:01

mihajlv


People also ask

What is the extension markup?

INCLUDE SCREENSHOTS WITH COMMENTS The MarkUp Chrome Extension allows you to include automated screenshots with every comment you make, providing an audit trail and allowing your team to see exactly what you were looking at when you made a comment and provided feedback.

What is the advantage of using XAML markup extensions?

XAML markup extensions help extend the power and flexibility of XAML by allowing element attributes to be set from sources other than literal text strings. In either case, the text string set to the Color attribute is converted to a Color value by the ColorTypeConverter class.

What are markup extensions in WPF?

The most common markup extensions used in WPF programming are those that support resource references ( StaticResource and DynamicResource ), and those that support data binding ( Binding ). StaticResource provides a value for a property by substituting the value of an already defined resource.

Is XAML a markup?

In WPF and UWP, XAML is a user interface markup language to define UI elements, data binding, and events.


2 Answers

Markup extensions are not about access but extending functionality of markup (as the name implies) by doing whatever you want them to, like creating associations (Binding, x:Reference) or getting the type of a class (x:Type).

They can be used for just about anything, they are only necessary where the markup does not suffice on its own.

like image 91
H.B. Avatar answered Oct 08 '22 12:10

H.B.


Rationale for markup extensions:

XAML is simple, which is a good thing. It is just an XML-based language used to declare objects and the relationships between them. One side effect of being simple is that it can be verbose. This cumbersome verbosity was one of the main reasons why the concept of markup extensions was introduced. A markup extension can be used to turn many lines of XAML into one concise expression...

Another side effect of XAML’s simplicity is that it does not have any “built in” knowledge of common artifacts used by WPF or the CLR; such as resource references, data binding, a null value, arrays, static members of a class, etc. Since XAML can be an integral part of application development there needs to be some way for developers to express those ideas in it.

<TextBox >
  <TextBox.Text>A text in TextBox</TextBox.Text>
</TextBox>

<TextBox Text="{x:Static system:Environment.UserName}" />

This latter syntax also provides a way to use values other than a literal string (i.e., which is a new object) such as an already constructed object or a static object in our assembly. In this sense markup extensions are objects which decide how a property's going to be set at runtime.

like image 29
Faqir Aamir Avatar answered Oct 08 '22 12:10

Faqir Aamir