2010-12-09 23 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