如何使用功能区按钮执行代码?

时间:2017-05-30 16:26:23

标签: wpf vb.net ribbon

到目前为止,我已设法通过this tutorial创建一个带有几个按钮的功能区。

现在我想让按钮真正做到!我发现this example显示了如何执行已有的方法。但是,如果我这样做:Command =" 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。将代码转换为VB.NET后:

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" ...元素的引用:

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

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

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

1 个答案:

答案 0 :(得分:1)

重建你的项目。

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

如果添加一个新类,它会在重建项目之前给出错误,因此程序集包含该类。