如何在新项目中使用AttachedCommandBehavior?

发布于 2024-07-21 07:32:34 字数 2180 浏览 6 评论 0 原文

我已经下载了 这个 AttachedCommandProject 并运行它它运行良好,使我能够将 MouseDown 命令放在 Border 元素上,并使用 ViewModel 中的命令处理该命令。

现在我想将此 AttachedCommand 功能添加到我的 MVVM Visual Studio 模板

我将所有必要的文件复制到 MVVM 项目的 Commands 文件夹中:

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>

I've downloaded this AttachedCommandProject and ran it and it works well, enables me to e.g. put a MouseDown command on a Border element and handle that with a command in my ViewModel.

Now I would like to add this AttachedCommand functionality to my MVVM Visual Studio Template.

I copied all the necessary files into the my Commands folder of the MVVM project:

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

But when I try to use it with the same syntax as in the original project, I get the error The property 'CommandBehavior.Event' does not exist in XML namespace 'clr-namespace:MvvmWithAttachedBehaviors.Commands'..

There are no other files to copy and no other references to add as far as I can see.

What could this error be trying to tell me? Has anyone gotten this AttachedCommandBehavior functionality to work in other projects?

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

偏爱你一生 2024-07-28 07:32:34

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

您尝试过全部重建吗? 有时,设计器会给您 xml 命名空间错误,如果您重建所有错误,这些错误就会消失。

希望这会有帮助...

Did the source files that contain CommandBehavior get copied into the new project? If so, I would check the namespace they are in. It could be that the namespace is different in this project. The line: { xmlns:c="clr-namespace:MvvmWithAttachedBehaviors.Commands" } is setting the prefix "c" to represent a namespace called MvvmWithAttachedBehaviors.Commands that exists in the local assembly. If this namespace is in a different assembly you will have to reference the assembly in this declaration.

Have you tried rebuilding all? Sometimes the designer will give you xml namespace errors that clear up if you rebuild all.

I hope this helps a little...

甜尕妞 2024-07-28 07:32:34

您必须在项目中引用程序集 AttachedCommandBehavior.dll,并修改 XAML 命名空间声明,如下所示:

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

You have to reference the assembly AttachedCommandBehavior.dll in your project, and modify the XAML namespace declaration like that :

xmlns:c="clr-namespace:AttachedCommandBehavior;assembly=AttachedCommandBehavior"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文