WPF中可以使用XAML 2009相关的标记扩展吗? [英] Can XAML 2009-related markup extensions be used in WPF?

查看:84
本文介绍了WPF中可以使用XAML 2009相关的标记扩展吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我说的是诸如 x:Reference x:FactoryMethod 之类的扩展名,它们共同出现在 WPF中的XAML 2009语言支持

在WPF中,可以使用XAML 2009功能,但只能用于未使用WPF标记编译的XAML.标记编译的XAML和BAML形式的XAML当前不支持XAML 2009语言关键字和功能.请注意,用于在WPF中加载松散XAML的现有技术也可能对CLR类型和类型系统具有可能的安全性和访问限制,这些限制比对标记编译的XAML更为严格.有关更多信息,请参见安全性(WPF)或WPF安全性策略-平台安全性.XAML 2009还引入了其他功能,这些功能可以修改以前的XAML 2006构造,也可以修改基本的标记形式.

x:参考标记扩展

x:Reference是XAML 2009中定义的构造.在WPF中,可以使用XAML 2009功能,但只能用于未WPF标记编译的XAML.标记编译的XAML和BAML形式的XAML当前不支持XAML 2009语言关键字和功能.

但是,下一段恰好出现在上一段之前(即关于 x:Reference 的那一段).

在WPF和XAML 2006中,元素引用由ElementName绑定的框架级功能解决.对于大多数WPF应用程序和方案,仍应使用ElementName绑定.本通用指南的例外情况可能包括以下情况:由于存在数据上下文或其他范围考虑因素,使得数据绑定不可行,并且不涉及标记编译.

这里没有直接的矛盾,但是本段似乎表明可以在应用程序中使用 x:Reference .另外,关于 x:Reference 的段落似乎是自动生成的,并且可能已过时.

然后,我们从"WPF 4 Unleashed"这本书中摘录了以下内容:

x:Reference标记扩展名经常被错误地与XAML2009关联.在撰写本文时,只能在宽松的XAML中使用的功能.虽然x:Reference是WPF 4中的一项新功能,只要您的项目针对的是.NET Framework版本4或更高版本,它就可以从XAML2006使用.

我们还有以下Stackoverflow问题,其中一些答案相互矛盾,没有一个显然是正确的:

  1. 什么可以替代x:Reference?
  2. XAML 2009-x:参考-使用此功能有不利之处吗?
  3. 何时x:WPF中的引用已解决,为什么XAML元素顺序会影响它?

最后,我个人使用了 x:Reference 扩展名,无论MSDN怎么说,它似乎都可以在WPF应用程序中使用,即使Visual Studio有时会抱怨一些奇怪的事情(或者可能是ReSharper),例如标记中的空引用异常.

了解实际问题,

  1. XAML 2009可以全部或部分用于WPF应用程序吗?
  2. 可以在编写WPF应用程序时使用这些特定的扩展吗?使用它们有任何限制吗?
  3. 为什么在所有这些方面有这么多困惑?

解决方案

我将从头开始:


为什么在所有这些方面有这么多困惑?

也许是因为到目前为止,包括WPF 4.5版本在内,几乎完全缺乏对XAML 2009的支持. Matthew MacDonald WPF 4.5第2章的自由引用:

最小改进 XAML 2009 仍未完全实现.它们仅支持无关的文件XAML,但不支持 Page 的资源类型(编译的资源),这些文件在各处都使用. XAML 2009 可能永远不会成为WPF的完全集成的一部分,因为它提供的改进尤其不重要,并且XAML编译器中的任何更改都会带来安全性和性能问题.

关于XAML 2009的未来,我们不能确定何时将完全支持XAML 2009,但是我们知道目前它不存在.


可以在编写WPF应用程序时使用这些特定的扩展吗?使用它们有任何限制吗?

是的,某些扩展名可以在WPF 4.0和WPF 4.5中使用,例如: x:Reference 链接 ..>

关于 x:FactoryMethod 也在这里,并不是一切都进行得很顺利,对我来说,在WPF 4.0中,VisualStudio 2010程序没有用它编译.还在 connect.microsoft.com ,但是Microsoft的代表说:

WPF团队最近已审查了此问题,因为当前团队正在关注影响最大数量的WPF开发人员的错误,因此将不解决此问题.如果您认为此错误已解决,请使用所有必要的支持详细信息重新激活此错误.


可以在WPF应用程序中完全或部分使用XAML 2009吗?

一个明确的答案:可以部分使用,但可能不适用于WPF和Visual Studio的每个版本.

如果将来计划将您的项目移植到WPF版本<4.0或更高版本,或者在其他平台(例如Silverlight,Windows Phone)上,我认为最好避免使用XAML 2009,因为不能保证至少会部分支持XAML 2009.例如,在 Silverlight 中,XAML 2009中不提供支持,甚至XAML 2006也没有得到完全支持.可能项目的很大一部分可能依赖于XAML 2009,因此必须花费一定的时间和资源来摆脱这种依赖.在这种情况下,最好使用更通用的解决方案.

如果您使用的是WPF 4.0及更高版本,并且没有计划移植到其他平台,我认为在经过测试之后,可以部分使用XAML 2009.

有关更多信息,我建议您熟悉XAML 2009表示法:

MSDN:Microsoft特定于域的语言

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?

解决方案

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

这篇关于WPF中可以使用XAML 2009相关的标记扩展吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆