I'm talking about extensions such as x:Reference
and x:FactoryMethod
, collectively appearing here. I'm reading a lot of contradictory information online, including on MSDN, Stackoverflow, and from other sources.
I'll talk about x:Reference
as an example, but I'm actually referring to the other markup extensions as well. The primary causes of confusion are the following excerpts from MSDN:
XAML 2009 Language Support in WPF
In WPF, you can use XAML 2009 features, but only for XAML that is not WPF markup-compiled. Markup-compiled XAML and the BAML form of XAML do not currently support the XAML 2009 language keywords and features. Note that existing techniques for loading loose XAML in WPF also have possible security and access restrictions to CLR types and the type system that are more restrictive than for markup-compiled XAML. For more information, see Security (WPF) or WPF Security Strategy - Platform Security. XAML 2009 also introduces additional features that either modify the previous XAML 2006 constructs or that modify the basic markup forms.
x:Reference is a construct defined in XAML 2009. In WPF, you can use XAML 2009 features, but only for XAML that is not WPF markup-compiled. Markup-compiled XAML and the BAML form of XAML do not currently support the XAML 2009 language keywords and features.
However, the following paragraph appears just before the preceding paragraph (that is, the one about x:Reference
).
In WPF and XAML 2006, element references are addressed by the framework-level feature of ElementName binding. For most WPF applications and scenarios, ElementName binding should still be used. Exceptions to this general guidance might include cases where there are data context or other scoping considerations that make data binding impractical and where markup compilation is not involved.
There is no direct contradiction here, but this paragraph seems to say that it is possible to use x:Reference
in applications. Also, that paragraph about x:Reference
seems to be automatically generated, and may be outdated.
Then, we have the following excerpt from the book "WPF 4 Unleashed":
The x:Reference markup extension is often mistakenly associated with the XAML2009 features that can only be used from loose XAML at the time of this writing. Although x:Reference is a new feature in WPF 4, it can be used from XAML2006 just fine as long as your project is targeting version 4 or later of the .NET Framework.
We also have the following Stackoverflow questions, some with contradictory answers, and none being obviously correct:
- What would be an alternative to x:Reference?
- XAML 2009 - x:Reference - Any downside to using this?
- When is x:Reference in WPF resolved and why does XAML element order affect it?
Finally, I've personally used the x:Reference
extension and it does seem to work in WPF applications, whatever MSDN says, even though Visual Studio sometimes complains about strange things (or it might be ReSharper), such as null reference exceptions in the markup.
Getting to the actual questions,
- Can XAML 2009 be used (in whole or in part) in WPF applications?
- Can these specific extensions be used in writing WPF applications? Are there any limitations in using them?
- Why is there so much confusion regarding all of this?