C# WPF-名称不为';名称空间中不存在
我在尝试在WPF中实现一些自定义绑定时遇到了名称空间问题。我收到错误“名称”CustomCommands“不存在于名称空间”clr namespace:GraphicsBook;assembly=Testbed2D” 在我的XAML中,我有:C# WPF-名称不为';名称空间中不存在,c#,wpf,namespaces,undefined,C#,Wpf,Namespaces,Undefined,我在尝试在WPF中实现一些自定义绑定时遇到了名称空间问题。我收到错误“名称”CustomCommands“不存在于名称空间”clr namespace:GraphicsBook;assembly=Testbed2D” 在我的XAML中,我有: <Window x:Class="GraphicsBook.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://sche
<Window x:Class="GraphicsBook.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:k="clr-namespace:GraphicsBook;assembly=Testbed2D"
Title="Window1"
<Window.CommandBindings>
<CommandBinding Command="k:CustomCommands.AddCircle" CanExecute="AddCircleCommand_CanExecute" Executed="AddCircleCommand_Executed"></CommandBinding>
</Window.CommandBindings>
<Menu>
<MenuItem Header="Add">
<MenuItem Command="k:CustomCommands.AddCircle" />
</MenuItem>
</Menu>
错误来自行“MenuItem Command=“k:CustomCommands.AddCircle””
非常感谢您的帮助!!您的XML/CLR命名空间映射错误:您有
k
别名GraphicsBook
,但是CustomCommands
在GraphicsBook.Assignment
中声明
您还可以尝试使用
{x:Static k:CustomCommands.AddCircle}
而不是简单地k:CustomCommands.AddCircle和CustomCommands
在Testbed2D
程序集中声明?是的,它是Testbed2D中的一个.cs文件。您是否尝试使用x:Static
?Hm仍然给我同样的错误不幸的是命令=“{x:Static k:CustomCommands.AddCircle}”“
出了问题。窗口是在同一个部件中,还是在不同的部件中?如果是其他程序集,则包含该窗口的项目是否引用了该命令的程序集?是否确定程序集名称与项目名称(Testbed2D)匹配?如果重命名了一个,则需要重命名另一个。
namespace GraphicsBook
{
public partial class CustomCommandSample : Window
{
public CustomCommandSample()
{
...
}
private void AddCircleCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
}
public static class CustomCommands
{
public static readonly RoutedUICommand AddCircle = new RoutedUICommand
(
"AddCircle",
"AddCircle",
typeof(CustomCommands),
new InputGestureCollection()
{
new KeyGesture(Key.F4, ModifierKeys.Alt)
}
);
}
}