2009-05-29 93 views
0

我已经下载this AttachedCommandProject并运行它,它很好运行,使我能够例如将一个MouseDown命令放在一个Border元素上,并使用我的ViewModel中的命令处理该命令。如何在新项目中使用AttachedCommandBehavior?

现在我想添加此附加命令功能到我的MVVM Visual Studio Template

我复制了所有必需的文件到MVVM项目的我的命令文件夹:

13.12.2008 21:00    7.445 BehaviorBinding.cs 
05.12.2008 17:50    7.477 CommandBehavior.cs 
13.12.2008 21:01    3.908 CommandBehaviorBinding.cs 
13.12.2008 21:06    5.097 CommandBehaviorCollection.cs 
04.12.2008 21:48    3.564 EventHandlerGenerator.cs 
05.12.2008 17:52    2.376 ExecutionStrategy.cs 
05.12.2008 17:52    2.067 SimpleCommand.cs 

但是,当我尝试使用相同的语法使用它作为在原项目中,我得到的错误的属性'CommandBehavior.Event'在XML命名空间'clr-namespace:MvvmWithAttachedBehaviors.Commands'中不存在。

还有没有其他文件复制,没有其他的引用添加,据我所知。

这个错误可能试图告诉我什么?有没有人得到这个AttachedCommandBehavior功能在其他项目中工作?

<Window x:Class="MvvmWithAttachedBehaviors.Views.MainView" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:c="clr-namespace:MvvmWithAttachedBehaviors.Commands" 
    Title="Main Window" Height="400" Width="800"> 
    <DockPanel> 
     <StackPanel> 
      <TextBlock Text="{Binding Output}"/> 
      <Border Background="Yellow" Width="350" Margin="0,0,10,0" Height="35" CornerRadius="2" 
         c:CommandBehavior.Event="MouseDown" 
         c:CommandBehavior.Command="{Binding SomeCommand}" 
         c:CommandBehavior.CommandParameter="This is the parameter sent." 
         > 
       <TextBlock Text="MouseDown on this border to execute the command"/> 
      </Border> 
     </StackPanel> 
    </DockPanel> 
</Window> 

回答

1

是否将包含CommandBehavior的源文件复制到新项目中?如果是这样,我会检查它们所在的命名空间。这可能是该项目中的名称空间不同。行:{xmlns:c="clr-namespace:MvvmWithAttachedBehaviors.Commands"}正在设置前缀“c”以表示名为MvvmWithAttachedBehaviors.Commands的名称空间,该名称空间存在于本地程序集中。如果该名称空间位于不同的装配中,则必须在此声明中引用该装配。

你试过重建所有的东西吗?有时候,如果你重建所有的东西,设计者会给你xml命名空间的错误。

我希望这有助于有点...

+0

这就是我忽略了,必须将“命名空间AttachedCommandBehavior”更改为“命名空间MvvmWithAttachedBehaviors.Commands”,谢谢。 – 2009-05-29 13:43:30

0

你要引用的程序集AttachedCommandBehavior.dll在您的项目,并修改XAML命名空间声明这样的:

xmlns:c="clr-namespace:AttachedCommandBehavior;assembly=AttachedCommandBehavior"