Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can XAML 2009-related markup extensions be used in WPF?

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 Markup Extension

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:

  1. What would be an alternative to x:Reference?
  2. XAML 2009 - x:Reference - Any downside to using this?
  3. 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,

  1. Can XAML 2009 be used (in whole or in part) in WPF applications?
  2. Can these specific extensions be used in writing WPF applications? Are there any limitations in using them?
  3. Why is there so much confusion regarding all of this?
like image 736
GregRos Avatar asked Mar 14 '14 20:03

GregRos


People also ask

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.

What is XAML markup extensions?

Markup extensions are a XAML technique for obtaining a value that's not a primitive or a specific XAML type. For attribute usage, markup extensions use the known character sequence of an opening curly brace { to enter the markup extension scope, and a closing curly brace } to exit.

What is WPF XAML?

WPF and XAML XAML is a new descriptive programming language developed by Microsoft to write user interfaces for next-generation managed applications. XAML is used to build user interfaces for Windows and Mobile applications that use Windows Presentation Foundation (WPF), UWP, and Xamarin Forms.


1 Answers

I'll start from the end:


Why is there so much confusion regarding all of this?

Perhaps because so far, including a version of WPF 4.5, almost completely lack of support XAML 2009. Freestyle quote from the Matthew MacDonald WPF 4.5, Chapter 2 book:

Minimum improve XAML 2009 is still not fully implemented. They only support unrelated files XAML, but not resource type of Page (compiled resource), which are used everywhere. Probably, XAML 2009 will never be a fully integrated part of WPF, because it provided improvements are particularly not important and as any change in XAML compiler generates problems of security and performance.

Regarding the future of XAML 2009, then we can not say for sure when it will fully support, but we know that at the moment it does not exist.


Can these specific extensions be used in writing WPF applications? Are there any limitations in using them?

Yes, some extensions may be used in WPF 4.0 and WPF 4.5, for example: x:Array and other types, as well as x:Reference, x:FactoryMethod. I personally do use types in XAML 2009 and did not notice any bugs.

About the x:Reference can say, that in Visual Studio 2010 it does not work as it should: when using x:Reference as the Target of a some Control, the Visual Studio designer throws an InvalidOperationException exception with the message:

Service provider is missing the INameResolver service.

The project will compile and execute without any issues, but the Design canvas where the x:Reference appears will be disabled because of the exception. Personally, I have it sometimes appears, sometimes not, but it should be ignored.

But I almost know exactly what in version WPF 4.5 and perhaps for the Visual Studio more than in 2010 this bug has been fixed. For more info see this link.

About x:FactoryMethod here too, not everything went smoothly, for me in WPF 4.0, VisualStudio 2010 program is not compiled with it. Was also created a bug-report on connect.microsoft.com, but representatives of Microsoft said that:

The WPF team has recently reviewed this issue and will not be addressing this issue as at this time the team is focusing on the bugs impacting the highest number of WPF developers. If you believe that this was resolved in error, please reactivate this bug with any necessary supporting details.


Can XAML 2009 be used (in whole or in part) in WPF applications?

A definite answer: partially can be used, but maybe not for every version of WPF and Visual Studio.

If your project is planned in the future ported to the WPF version < 4.0 or more later, or on other platforms, such as Silverlight, Windows Phone, I think is better to avoid XAML 2009, because there is no guarantee that they will be it, at least partially supported. For example, in Silverlight there is no support in XAML 2009, there is even the XAML 2006 is not fully supported. It may be that a large part of the project can be dependent on XAML 2009, accordingly have to spend a certain amount of time and resources to to get rid of of this dependence. In this situation it is better to use a more universal solutions.

If you are using WPF 4.0 and higher, and there is no plan ported to the other platforms, I think partially it will be possible to use XAML 2009, after having tested it.

For more information, I recommend to familiarize yourself with XAML 2009 notation:

MSDN: Microsoft Domain-Specific Languages

like image 193
Anatoliy Nikolaev Avatar answered Oct 15 '22 09:10

Anatoliy Nikolaev