Castle Windsor 无法解析实现 WPF ICommand 的类

发布于 2024-10-07 03:20:20 字数 3145 浏览 1 评论 0原文

我在尝试使用温莎城堡注册 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) {} 
        } 
    } 

I am seeing some strange behaviour whilst trying to register WPF ICommands using Castle Windsor.

When running the included code, I get the following error:

Can't create component 'ConsoleApplication1.TestParent' as it has dependencies to be satisfied.
ConsoleApplication1.TestParent is waiting for the following dependencies:
Keys (components with specific keys)
testChild which was not registered.

However if I change the interface on TestChild from ICommand to ITest then the code works just fine.

Has anyone else witnessed this behaviour and/or know how to resolve it?

I am using VS2008, .NET 3.5 and Castle 2.5.2.

Thanks,
Stuart  

    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) {} 
        } 
    } 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

み青杉依旧 2024-10-14 03:20:20

我在尝试使用温莎城堡注册 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) {}          
    }      
}    

I am seeing some strange behaviour whilst trying to register WPF ICommands using Castle Windsor.

When running the included code, I get the following error:

Can't create component 'ConsoleApplication1.TestParent' as it has dependencies to be satisfied.
ConsoleApplication1.TestParent is waiting for the following dependencies:

Keys (components with specific keys)
- testChild which was not registered.

However if I change the interface on TestChild from ICommand to ITest then the code works just fine.

Has anyone else witnessed this behaviour and/or know how to resolve it?

I am using VS2008, .NET 3.5 and Castle 2.5.2.

Thanks, Stuart

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) {}          
    }      
}    
蓦然回首 2024-10-14 03:20:20

忽略您的代码示例由于不正确调用 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 接口定义,不知道它的用途。

Ignoring the fact that your code sample does not compile due to incorrect calling of Windsor registration, could it be you could want something like this?

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) {}          
} 

}

Note that I removed the ITest interface definition, no clue what that was used for.

小忆控 2024-10-14 03:20:20

TestParent 类只有一个构造函数,需要传递一个对象参数。如果它是像 intstring 这样的基本数据类型,您可以在 App.config 文件中指定其值。但你不能在那里设置对象。在这种情况下,我建议您添加一个空白构造函数(不带参数)。

the TestParent class has only one constructor, which requires passing an object parameter. If it were a basic data type like int or string, you could specify its value in the App.config file. But you can't set objects there. In this case, I recommend you add a blank constructor (without parameters).

灯下孤影 2024-10-14 03:20:20

感谢您的回复。

由于工作中的公司过滤器拒绝显示“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 确实有效,但是它并不能解决我的问题,因为我的真实应用程序中有多个命令,并且我需要 Castle 来区分它们。

我已经设法通过为每个命令创建虚拟接口并针对这些命令进行注册来解决此问题 - 这并不理想。

ITest 接口允许从 ICommand 和 ITest 进行切换,这解决了该问题 - 即该问题特定于 WPF ICommand。

斯图尔特

Thanks for the replies.

Since the corporate filter at work refuses to display the "Captcha" human verification I had to cut and paste from my iphone... anyway this somehow removed the generics from the code sample so apologies for the lack of compilation not to mention the fubar layout...

Should look like:

    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>();
    }

Registering ICommand does work, however it doesn't solve my problem since I have multiple commands in my real app and I need to Castle to distinguish between them.

I have managed to work around this by creating dummy interfaces for every one of the commands and registering against those - not ideal.

The ITest interface was to allow switching from ICommand and ITest which resolves the issue - i.e. the issue is specific to WPF ICommand.

Stuart

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文