2011-01-19 62 views
0

我(注Extras.WP7):为什么MvvmLight.Command _and_ MvvmLight.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 }"/> 

......但是当我这样做时,我得到"The attachable property 'Command' was not found in type 'ButtonBaseExtensions'"错误。

,我发现我不得不添加一个命名空间assembly=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:cmdxtrasxmlns:cmd。如果我只有一个或另一个,事情就不会起作用!

这看起来非常笨拙,并没有与我认为别人所暗示的一致。为什么我需要两个?

回答

5

Extras组件存在,因为EventToCommand需要对System.Windows.Interactivity的引用,而ButtonBaseExtensions,RelayCommand,Messenger等不需要它。如果可以避免的话,有些人不愿意添加对组件的引用。因此对那些不需要EventtoCommand的人来说,他们使用基本程序集,而其他想要整个程序的人可以添加Extras。

干杯, Laurent

1

MvvmLight.Extras.WP7程序集提供了一个WP7特定程序集,其中包含“Extras”,即您可能会或可能不想使用的那些东西,其中包括EventToCommand。 MvvmLight.WP7程序集是提供核心功能的WP7特定程序集,其中包括ButtonBaseExtensions。恰巧在你的场景中,两个类都在同一个命名空间中,因为它们都与命令有关。不幸的是,.NET框架没有提供一种机制来引用来自两个不同程序集的相同命名空间,因此需要创建重复的xmlns定义。

从长期来看,这将有可能使用XmlnsDefinitionAttribute和XmlnsPrefixAttribute在这两个组件作为this MSDN article,这使得相同的xmlns和前缀与两个组件相同的命名空间相关的描述,但是这是一个决定的洛朗到做或为项目提供者:)