为什么同时需要 MvvmLight.Command 和 MvvmLight.Extras.WP7?
我有(注意 Extras.WP7
):
<phone:PhoneApplicationPage
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WP7"
>
...对于我的 EventToCommand 东西来说它工作得很好,如下所示:
<phone:PhoneApplicationPage.Resources>
<i:EventTrigger x:Key="KeyPadButtonTrigger" EventName="Click">
<cmd:EventToCommand Command="{Binding Path=KeyPadButtonCommand}" CommandParameter="{Binding ElementName=Self, Path=Content }" />
</i:EventTrigger>
</phone:PhoneApplicationPage.Resources>
但是后来我想使用 MmvmLight 的 ButtonBaseExtensions
像这样:
<Button x:Name="button1"
cmd:ButtonBaseExtensions.Command="{Binding KeyPadButtonCommand}"
cmd:ButtonBaseExtensions.CommandParameter="{Binding ElementName=button1, Path=Content }"/>
。 ..但是当我这样做时,我得到了“在‘ButtonBaseExtensions’类型中找不到可附加属性‘Command’”错误。
我发现我还必须为 assemble=GalaSoft.MvvmLight.WP7
添加命名空间,如下所示:
<phone:PhoneApplicationPage
xmlns:cmdxtras="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WP7"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.WP7"
>
请注意,我有 xmlns:cmdxtras
和 xmlns: cmd
。如果我只有其中之一,事情就行不通!
这看起来真的很笨拙,并且与我认为其他人的建议不符。为什么我需要两者?
I had (note Extras.WP7
):
<phone:PhoneApplicationPage
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WP7"
>
...and it was working fine for my EventToCommand stuff like this:
<phone:PhoneApplicationPage.Resources>
<i:EventTrigger x:Key="KeyPadButtonTrigger" EventName="Click">
<cmd:EventToCommand Command="{Binding Path=KeyPadButtonCommand}" CommandParameter="{Binding ElementName=Self, Path=Content }" />
</i:EventTrigger>
</phone:PhoneApplicationPage.Resources>
But then I wanted to use MmvmLight's ButtonBaseExtensions
like this:
<Button x:Name="button1"
cmd:ButtonBaseExtensions.Command="{Binding KeyPadButtonCommand}"
cmd:ButtonBaseExtensions.CommandParameter="{Binding ElementName=button1, Path=Content }"/>
...but when I did that I got "The attachable property 'Command' was not found in type 'ButtonBaseExtensions'"
errors.
I found I had to add a namespace for assembly=GalaSoft.MvvmLight.WP7
as well, like this:
<phone:PhoneApplicationPage
xmlns:cmdxtras="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WP7"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.WP7"
>
Note that I have BOTH xmlns:cmdxtras
and xmlns:cmd
. Things don't work if I only have one or the other!
This seems really clumsy and doesn't jive with what I think others are suggesting. Why do I need both?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Extras 程序集存在是因为 EventToCommand 需要对 System.Windows.Interactivity 的引用,而 ButtonBaseExtensions、RelayCommand、Messenger 等不需要它。如果可以避免的话,有些人不愿意添加对程序集的引用。因此,对于那些不需要 EventtoCommand 的人来说,他们只使用基本程序集,而其他想要整个程序的人可以添加 Extras。
干杯,
洛朗
The Extras assembly exists because EventToCommand requires a reference to System.Windows.Interactivity, while ButtonBaseExtensions, RelayCommand, Messenger etc do not need it. Some people are reluctant to add references to assemblies if they can avoid it. So for those people who don't need EventtoCommand, they onlu use the base assembly, and the others who want the whole program can add Extras.
Cheers,
Laurent
MvvmLight.Extras.WP7 程序集提供了一个特定于 WP7 的程序集,其中包含“Extras”,即您可能想要或可能不想使用的那些东西,其中包括 EventToCommand。 MvvmLight.WP7 程序集是 WP7 特定的程序集,提供核心功能,其中包括 ButtonBaseExtensions。碰巧在您的场景中,两个类位于同一名称空间中,因为它们都与命令相关。不幸的是,.NET Framework 不提供从两个不同程序集引用相同命名空间的机制,因此您需要进行重复的 xmlns 定义。
从长远来看,可以在两个程序集中使用 XmlnsDefinitionAttribute 和 XmlnsPrefixAttribute,如 这篇 MSDN 文章,它允许相同的 xmlns 和前缀与两个程序集中的相同命名空间关联,但这是 Laurent 做出的决定,或者由贡献者向项目提供的决定:)
The MvvmLight.Extras.WP7 assembly provides a WP7-specific assembly that contains the "Extras", i.e. those things that you may or may not want to use, which includes EventToCommand. The MvvmLight.WP7 assembly is the WP7-specific assembly that provides the core functionality, which includes ButtonBaseExtensions. It just so happens that in your scenario, both classes are in the same namespace because they both relate to commands. Unfortunately, the .NET Framework does not provide a mechanism to reference the same namespace from two different assemblies, hence the duplicate xmlns definitions that you need to make.
Longer term, it would be possible to use the XmlnsDefinitionAttribute and XmlnsPrefixAttribute in both assemblies as described in this MSDN article, which enable the same xmlns and prefix to be associated with the same namespace in both assemblies, but that's a decision for Laurent to make, or for a contributor to provide to the project :)