2017-05-30 90 views
0

到目前为止,我已经成功创建了一个带有几个按钮的功能区,按照this tutorial如何使用功能区按钮执行代码?

现在我想让按钮实际上做东西!我找到了this example,它显示了如何执行已有的方法。但是,如果我这样做:命令= “myCustomClass.myCustomMethod”,并使用

Class myCustomClass 
    Public Sub myCustomMethod() 
     'do stuff 
    End Sub 
End Class 

我得到的错误myCustomClass is not supported in a Windows Presentation Foundation (WPF) project

我也从同一篇文章中注意到这个this post

Public Class MyCommand 
    Implements ICommand 
    Public Sub Execute(parameter As Object) Implements ICommand.Execute 
     Dim hello As String = TryCast(parameter, String) 
     MsgBox(hello) 
    End Sub 

    Public Function CanExecute(parameter As Object) As Boolean Implements ICommand.CanExecute 
     Return True 
    End Function 

    Public Event CanExecuteChanged As EventHandler Implements ICommand.CanExecuteChanged 
End Class 

,并增加了<Window x:Class="MainWindow" ...元素的引用:代码转换为VB.NET后

<Window.Resources> 
    <local:MyCommand x:Key="mycmd"/> 
</Window.Resources> 

我得到以下错误:The name 'MyCommand' does not exist in the namespace "clr-namespace:RibbonSample"

我完全丧失了我需要做什么来实现这些按钮。是否有可能使用相同的框架获得任何完整的色带样本,我可以从中学习?我认为这个解决方案应该非常简单。

+0

重建您的项目,并确保命名空间匹配。 – SLaks

+0

@SLaks是的!那样做了!我仍然对为什么感到困惑,但很高兴它的工作。 – Sancarn

回答

1

重建您的项目。

WPF设计器从项目的编译程序集中加载类(以便它可以真正执行代码)。

如果你添加一个新的类,它会给你错误,直到你重建你的项目,所以程序集包含了这个类。