城堡温莎和Caliburn Mirco在进行时的奇怪行为

时间:2015-12-04 10:14:35

标签: c# wpf castle-windsor caliburn.micro

结合Castle Windsor和Caliburn.Micro(最新版本)时,我有一种奇怪的行为。我有ShellViewModel,是IWorkspace的指挥。当我启动我的应用程序时,我将一个(IWorkspace的第一个实现)添加到ShellViewModel.Items集合中。这是默认情况下应该如何吗?我的引导程序配置错了吗?

AppBootstrapper.cs

public class AppBootstrapper : BootstrapperBase
{
     IWindsorContainer container = new WindsorContainer();

    public AppBootstrapper()
    {
        container.Register(Component.For<IShell>().ImplementedBy<ShellViewModel>());

        container.Register(
            Classes.FromThisAssembly().BasedOn<IWorkspace>().WithServiceAllInterfaces().WithServiceSelf().LifestyleTransient()
            );


        container.Register(Component.For<IEventAggregator>().ImplementedBy<EventAggregator>());
        container.Register(Component.For<IWindowManager>().ImplementedBy<WindowManager>());
        Initialize();
    }

    protected override object GetInstance(Type service, string key)
    {
        return string.IsNullOrWhiteSpace(key) ? container.Resolve(service) : container.Resolve<object>(key);
    }

    protected override IEnumerable<object> GetAllInstances(Type service)
    {
        return new[] { container.ResolveAll(service) };
    }

    protected override void OnStartup(object sender, StartupEventArgs e)
    {
        DisplayRootViewFor<IShell>();
    }
}

ShellViewModel.cs

 public class ShellViewModel : Conductor<IWorkspace>.Collection.OneActive, IShell
{

}

ShellView.xaml

<Window x:Class="CastleCaliburnTest.Shell.ShellView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:CastleCaliburnTest.Shell"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300" Background="HotPink">
<Grid>
        <TextBlock Text="{Binding Items.Count}" FontSize="48"></TextBlock>
</Grid>

Workspace1ViewModel.cs

 public class Workspace1ViewModel : IWorkspace
{
    public Workspace1ViewModel()
    {
        Console.WriteLine("Workspace 1 created");
    }
}

接口:

public interface IShell: IConductor, IGuardClose
{

}
public interface IWorkspace
{

}

0 个答案:

没有答案