Why are MvvmLight.Command _and_ MvvmLight.Extras.WP7 both needed?
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 see开发者_开发知识库ms really clumsy and doesn't jive with what I think others are suggesting. Why do I need both?
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
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 :)
精彩评论