Wpf 无法正确形成命令绑定的xaml
我正在尝试实现命令绑定,以便我的用户可以使用Alt+进行导航。我找到了这个示例,但在形成xmlns和CommandBinding xaml时遇到了问题 这就是我目前所拥有的。我知道它的形状不正确Wpf 无法正确形成命令绑定的xaml,wpf,vb.net,Wpf,Vb.net,我正在尝试实现命令绑定,以便我的用户可以使用Alt+进行导航。我找到了这个示例,但在形成xmlns和CommandBinding xaml时遇到了问题 这就是我目前所拥有的。我知道它的形状不正确 xmlns:local="clr-namespace:xxxxx" <Window.CommandBindings> <CommandBinding Command="{x:Static local:xxxxx.ProjectMsg}"
xmlns:local="clr-namespace:xxxxx"
<Window.CommandBindings>
<CommandBinding
Command="{x:Static local:xxxxx.ProjectMsg}"
Executed="ProjectMsg"/>
</Window.CommandBindings>
我得到一个错误,名称xxxxx在名称空间clr名称空间中不存在:xxxxx
在主窗口vb中,没有声明名称空间。如果我尝试添加一个错误,就会出现许多错误
我必须在主窗口vb中有名称空间吗
我是wpf的noob,所以我非常感谢任何帮助或建议。谢谢
我必须在主窗口vb中有名称空间吗
否。请在Visual Studio中的Project->Properties->application->root namespace下检查应用程序的根命名空间。默认情况下,它与应用程序的名称相同,例如WPFAApplication1:
xmlns:local="clr-namespace:WpfApplication1"
还应通过属性公开命令:
local是VB命名空间xxxxx的别名。xxxxx是否包含在xxxxx中?不,它不包含在它自身中。本地:xxxxx最多可以表示类似xxxxx.xxxxx的意思,只是不能将名称空间放在那里
绑定到静态命令属性的正确方法如下:
Command="{x:Static local:WhateverClassName.StaticCommandProperty}"
什么是静态命令属性
它是一个返回命令的静态属性。命令是实现接口System.Windows.Input.ICommand的任何对象。不是那样的。这是一种方法
请参阅mm8的优秀答案,了解如何通过该途径解决您的问题
下面是一种更纯粹的XAML方法
我认为您在MainWindow_Loaded中创建的命令绑定是可以的,但请尝试在XAML中注释这些内容。VB中唯一使用的是ProjectMsg执行处理程序方法
<Window.Resources>
<!-- Create a command object. -->
<RoutedUICommand
x:Key="ProjectMsgCmdResource"
/>
</Window.Resources>
<Window.CommandBindings>
<!-- Bind the command object to your command execute method. -->
<CommandBinding
Command="{StaticResource ProjectMsgCmdResource}"
Executed="ProjectMsg"
/>
</Window.CommandBindings>
<Window.InputBindings>
<!-- And bind a key input binding to your command. -->
<KeyBinding
Key="P"
Modifiers="Ctrl"
Command="{StaticResource ProjectMsgCmdResource}"
/>
</Window.InputBindings>
根据项目属性,根命名空间为xxxxx。在vb编码中,我似乎无法在正确的位置获得ProjectTab_AltP属性,而不会出现错误。它到底去哪了?我把它放在类MainWindow下,并单独放在一个类中,但得到的错误是该属性不可访问,xxxxx中不存在xxxxx。或者扩展属性无法初始化。Thanks@EManning我在回答中解释了这一点。如果您无法理解,请复制mm8的内容。他告诉你该怎么做。只要从他的答案中复制并粘贴这个XAML:Command={x:Static local:MainWindow.ProjectTab_AltP}。我发现在一个命令绑定语句的末尾有一个。alt+P在移除它们后效果良好。我保留了公共共享项目选项卡\u AltP。。。相同的。如果它真的应该变成一个财产,那么我上面的问题仍然适用。谢谢你们两位的帮助。
<Window.CommandBindings>
<CommandBinding
Command="{x:Static local:MainWindow.ProjectTab_AltP}"
Executed="ProjectMsg"/>
</Window.CommandBindings>
Command="{x:Static local:WhateverClassName.StaticCommandProperty}"
<Window.Resources>
<!-- Create a command object. -->
<RoutedUICommand
x:Key="ProjectMsgCmdResource"
/>
</Window.Resources>
<Window.CommandBindings>
<!-- Bind the command object to your command execute method. -->
<CommandBinding
Command="{StaticResource ProjectMsgCmdResource}"
Executed="ProjectMsg"
/>
</Window.CommandBindings>
<Window.InputBindings>
<!-- And bind a key input binding to your command. -->
<KeyBinding
Key="P"
Modifiers="Ctrl"
Command="{StaticResource ProjectMsgCmdResource}"
/>
</Window.InputBindings>