2017-05-30 95 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設計器從項目的編譯程序集中加載類(以便它可以真正執行代碼)。

如果你添加一個新的類,它會給你錯誤,直到你重建你的項目,所以程序集包含了這個類。