2010-12-09 24 views
1

我在使用Castle Windsor嘗試註冊WPF ICommands時看到一些奇怪的行爲。Castle Windsor無法解決實現WPF ICommand的類

當運行包含的代碼,我得到以下錯誤:

無法創建組件「ConsoleApplication1.TestParent」,因爲它有依賴關係得到滿足。
ConsoleApplication1.TestParent等待以下相關:
鍵(與特定鍵部件),將其未註冊
testChild。

但是,如果我將TestChild上的接口從ICommand更改爲ITest,那麼代碼工作得很好。

有沒有其他人目睹這種行爲和/或知道如何解決它?

我使用VS2008,.NET 3.5和Castle 2.5.2。

感謝,
斯圖爾特

using System;  
    using System.Windows.Input;  
      
    using Castle.MicroKernel.Registration;  
    using Castle.Windsor;  
  
    namespace ConsoleApplication1  
    {  
        class Program  
        {  
            static void Main(string[] args)  
            {  
                var container = new WindsorContainer();  
                  
                container.Register(  
                    Component.For<TestParent>().ImplementedBy<TestParent>(),  
                    Component.For<TestChild>().ImplementedBy<TestChild>()  
                   );  
  
                var parent = container.Resolve<TestParent>();  
            }  
        }  
  
        public interface ITest {}  
  
        public class TestParent  
        {  
            public TestParent(TestChild testChild) { }  
        }  
  
        public class TestChild : ICommand  
        {  
            public event EventHandler CanExecuteChanged;  
            public bool CanExecute(object parameter) { return true; }  
            public void Execute(object parameter) {}  
        }  
    }  
+0

我在.NET 4和Castle 2.5.1中遇到同樣的問題。我發現它與ICommand接口上的TypeConverter屬性(通過反編譯器找到它)有關。 Castle似乎並不喜歡使用特定的TypeConverter。 – 2011-08-11 20:58:40

回答

1

我看到一些奇怪的行爲,同時嘗試註冊使用溫莎城堡WPF個ICommand。

當運行包含的代碼,我得到以下錯誤:

無法創建組件「ConsoleApplication1.TestParent」,因爲它有依賴關係得到滿足。
ConsoleApplication1.TestParent等待以下相關:

鍵(與特定的鍵組件)
- 其中未註冊testChild。

但是,如果我將TestChild上的接口從ICommand更改爲ITest,那麼代碼工作得很好。

有沒有其他人目睹這種行爲和/或知道如何解決它?

我使用VS2008,.NET 3.5和Castle 2.5.2。

謝謝,司徒

using System; 
using System.Windows.Input; 
using Castle.MicroKernel.Registration; 
using Castle.Windsor; 
namespace ConsoleApplication1  
{   
    class Program   
    {    
     static void Main(string[] args)    
     {     
      var container = new WindsorContainer(); 

      container.Register(      
       Component.For().ImplementedBy(),      
       Component.For().ImplementedBy()      
      ); 

      var parent = container.Resolve(); 
     } 
    } 

    public interface ITest {} 

    public class TestParent 
    {    
     public TestParent(TestChild testChild) { } 
    } 

    public class TestChild : ICommand   
    {    
     public event EventHandler CanExecuteChanged;    
     public bool CanExecute(object parameter) { return true; }    
     public void Execute(object parameter) {}   
    }  
}  
0

TestParent類只有一個構造函數,它要求傳遞的對象參數。如果它是基本數據類型,如intstring,則可以在App.config文件中指定其值。但是你不能在那裏設置對象。在這種情況下,我建議你添加一個空白的構造函數(不帶參數)。

1

忽略一個事實,即您的代碼示例由於不正確的調用Windsor註冊而無法編譯,難道您可能需要這樣的東西嗎?

class Program   
{    
    static void Main(string[] args)    
    {     
     var container = new WindsorContainer(); 

     container.Register(
      Component.For<TestParent>(), 
      Component.For<ICommand>().ImplementedBy<TestChild>() 
     ); 

     var parent = container.Resolve<TestParent>(); 
    } 
} 

public class TestParent 
{    
    public TestParent(ICommand testChild) { } 
} 

public class TestChild : ICommand   
{    
    public event EventHandler CanExecuteChanged;    
    public bool CanExecute(object parameter) { return true; }    
    public void Execute(object parameter) {}   
} 

}

注意,我刪除ITest接口的定義,不知道那是什麼用途。

0

感謝您的回覆。

由於企業過濾器在工作拒絕顯示「Captcha」人的驗證我不得不從我的iPhone剪切和粘貼...無論如何,這不知何故從代碼示例中刪除了泛型,因此,不支持編譯的道歉何況FUBAR佈局...

應該像這樣:

private static void Main(string[] args) 
    { 
     var container = new WindsorContainer(); 

     container.Register(
      Component.For<TestParent>().ImplementedBy<TestParent>(), 
      Component.For<TestChild>().ImplementedBy<TestChild>()); 

     var parent = container.Resolve<TestParent>(); 
    } 

註冊ICommand的不工作,但因爲我有我的真正的應用程序的多個命令它並沒有解決我的問題,我需要向城堡區分它們。

我已經設法解決這個問題,爲每個命令創建虛擬接口並註冊這些 - 不理想。

ITest界面允許從ICommand和ITest進行切換,從而解決問題 - 即該問題特定於WPF ICommand。

Stuart