【问题标题】:Why are MvvmLight.Command _and_ MvvmLight.Extras.WP7 both needed?为什么 MvvmLight.Command _and_ MvvmLight.Extras.WP7 都需要?
【发布时间】:2011-01-19 21:11:25
【问题描述】:

我有(注意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。如果我只有一个或另一个,事情就行不通!

这看起来很笨拙,与我认为其他人的建议不符。为什么我都需要?

【问题讨论】:

    标签: silverlight xaml windows-phone-7 mvvm-light


    【解决方案1】:

    Extras 程序集存在是因为 EventToCommand 需要引用 System.Windows.Interactivity,而 ButtonBaseExtensions、RelayCommand、Messenger 等不需要它。如果可以避免,有些人不愿意添加对程序集的引用。所以对于那些不需要EventtoCommand的人,他们只使用基础程序集,其他需要整个程序的人可以添加Extras。

    干杯, 洛朗

    【讨论】:

      【解决方案2】:

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

      从长远来看,可以在两个程序集中使用 XmlnsDefinitionAttribute 和 XmlnsPrefixAttribute,如 this MSDN article 中所述,这使得相同的 xmlns 和前缀能够与两个程序集中的相同命名空间相关联,但这是 Laurent 的决定制作,或为贡献者提供给项目:)

      【讨论】:

        猜你喜欢
        • 2012-02-17
        • 2023-04-04
        • 1970-01-01
        • 1970-01-01
        • 2019-06-09
        • 1970-01-01
        • 2010-12-12
        • 2020-05-03
        • 1970-01-01
        相关资源
        最近更新 更多