问题与MEF - Caliburn.Micro中的ExportFactory <t> </t>

时间:2011-01-04 15:21:08

标签: wpf mef caliburn.micro

我在Caliburn.Micro中使用MEF作为导出视图模型类。我想使用ExportFactory类而不是Export属性,因为我尝试解决此处描述的相同问题: Object destruction problem with MEF

我尝试解决这个问题,例如:

某些视图模型 - WPF窗口:

[Export(typeof(IChatViewModel))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class ChatViewModel : Screen, IViewModelIdentity, 
    IChatViewModel, IHandle<Rp>, IHandle<DetailData>
{}

我在其他视图模型类中使用窗口管理器打开聊天屏幕:

private IWindowManager _windowManager;
var chatScreen = IoC.Get<IChatViewModel>();
_windowManager.Show(chatScreen);

然后我关闭了屏幕,但是这个类的对象还活着。我尝试了同样的问题,如下所述: Object destruction problem with MEF

我有视图模型 - 它是WPF窗口

情景/解决方案

我有3个视图模型/屏幕:

  • LogOnViewModel - 用户控件
  • MessengerViewModel - 用户控件
  • ChatViewModel - WPF窗口

这些视图模型的代码在这里:

//#1 screen - user control
public class LogOnViewModel : Screen, ILogOnViewModel
{
    private IShellViewModel _shell;

    [ImportingConstructor]
    public LogOnViewModel(IShellViewModel shell)
    {
        _shell = shell;
    }

    //....

    //Active #2 screen Messenger screen
     _shell.ShowMessenger(_account);

    //....
}

//#2 screen - user control
public class MessengerViewModel : Screen,IMessengerViewModel
{       
    private IViewModelsControler _viewModelControler;
    private IWindowManager _windowManager;

    [ImportingConstructor]
    public MessengerViewModel(IViewModelsControler viewModelControler, IWindowManager windowManager)
    {
        _windowManager = windowManager;
        _viewModelControler = viewModelControler;

    }

    //...

    //Open #3 sceen - WPF window
    var vindow = _viewModelControler.CreateChatViewModel();
    _windowManager.Show(window);

    //...
}

//#3 screen - WPF window
[PartCreationPolicy(CreationPolicy.NonShared)]
public class ChatViewModel : Screen, IChatViewModel
{

}

我的shell看起来像这样:

  //SHELL is WPF window  
    [Export(typeof(IShellViewModel))]
    public class ShellViewModel : Conductor<IScreen>.Collection.OneActive, IShellViewModel
    {
        [Import]
        public IViewModelsControler ViewModelControler{ get; set;}

        protected override void OnInitialize()
        {
            ShowLogOn();
            base.OnInitialize();
        }

        public void ShowLogOn()
        {
            //var vm = IoC.Get<ILogOnViewModel>();
            var vm = ViewModelControler.CreateLogOnViewModel();
            ActivateItem(vm);
        }

        public void ShowMessenger(Account account)
        {
            ActiveItem.Deactivate(true);
            //var vm = IoC.Get<IMessengerViewModel>();
            var vm = ViewModelControler.CreateMessengerViewModel();
            vm.Account = account;
            ActivateItem(vm);
        }

    }

我想使用ExportFactory类来创建视图模型类。我尝试实现与此处相同的逻辑:http://mef.codeplex.com/wikipage?title=PartCreator

所以我为创建视图模型类创建了自己的控制器:

这是它:

public  interface IViewModelsControler
{
    ILogOnViewModel CreateLogOnViewModel();
    IMessengerViewModel CreateMessengerViewModel();
    IChatViewModel CreatChatViewModel();
}

[Export(typeof(IViewModelsControler))]
public class ViewModelsControler:IViewModelsControler
{
    [Import]
    public ExportFactory<ILogOnViewModel> LogOnViewFactory { get; set; }

    public  ILogOnViewModel CreateLogOnViewModel()
    {
        return LogOnViewFactory.CreateExport().Value;
    }

    [Import]
    public ExportFactory<IMessengerViewModel> MessengerViewFactory { get; set; }

    public IMessengerViewModel CreateMessengerViewModel()
    {
        return MessengerViewFactory.CreateExport().Value;
    }

    [Import]
    public ExportFactory<IChatViewModel> ChatViewFactory { get; set; }

    public IChatViewModel CreatChatViewModel()
    {
        return ChatViewFactory.CreateExport().Value;
    }
}

我在这些类中导入ViewModelsControler类:

  • ShellViewModel
  • MessengerViewModel

并且希望在创建视图模型类上使用类ViewModelsControler的方法。   - 列出项目

我不知道我做错了什么,如果我尝试编译这段代码,我会收到这个错误:

无法找到合同Spirit.ViewModels.IShellViewModel的任何实例。 Staktrace:    在C:\ Users \ Jan \ Documents \ Visual Studio 2010 \ Projects \ C#\ Pokec_Messenger \ ver.beta \ Pokec__Messenger \ Spirit_Caliburn_Micro_v1.0 \ BootStraper \ MefBootStrapper.cs中的Spirit.BootStraper.MefBootStrapper.GetInstance(类型serviceType,String key) :第59行    at Caliburn.Micro.IoC.GetT in C:\ Users \ Jan \ Documents \ Visual Studio 2010 \ Projects \ C#\ Pokec_Messenger \ ver.beta \ Pokec__Messenger \ Caliburn_Micro_Completed \ CaliburnMicro \ src \ Caliburn.Micro.Silverlight \ IoC.cs:第33行    at Caliburn.Micro.Bootstrapper`1.DisplayRootView()在C:\ Users \ Jan \ Documents \ Visual Studio 2010 \ Projects \ C#\ Pokec_Messenger \ ver.beta \ Pokec__Messenger \ Caliburn_Micro_Completed \ CaliburnMicro \ src \ Caliburn.Micro.Silverlight \ Bootstrapper.cs:第175行    at Caliburn.Micro.Bootstrapper.OnStartup(Object sender,StartupEventArgs e)位于C:\ Users \ Jan \ Documents \ Visual Studio 2010 \ Projects \ C#\ Pokec_Messenger \ ver.beta \ Pokec__Messenger \ Caliburn_Micro_Completed \ CaliburnMicro \ src \ Caliburn.Micro .Silverlight \ Bootstrapper.cs:第128行    在System.Windows.Application.OnStartup(StartupEventArgs e)    在System.Windows.Application。&lt; .ctor&gt; b__1(对象未使用)    在System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托回调,对象args,Int32 numArgs)    在MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source,Delegate方法,Object args,Int32 numArgs,Delegate catchHandler)

如果我在创建视图模型类上使用“导出”属性,则效果很好。

这样的事情:

[Export(typeof(ILogOnViewModel))]
public class LogOnViewModel : Screen,ILogOnViewModel
{}

和shell:

   [Export(typeof(IShellViewModel))]
    public class ShellViewModel : Conductor<IScreen>.Collection.OneActive, IShellViewModel
    {
        protected override void OnInitialize()
        {
            ShowLogOn();
            base.OnInitialize();
        }

        public void ShowLogOn()
        {
            var vm = IoC.Get<ILogOnViewModel>();
            //var vm = _viewModelControler.CreateLogOnViewModel();
            ActivateItem(vm);
        }

        public void ShowMessenger(Account account)
        {
            ActiveItem.Deactivate(true);
            var vm = IoC.Get<IMessengerViewModel>();
            //var vm = _viewModelControler.CreateMessengerViewModel();
            vm.Account = account;
            ActivateItem(vm);
        }

    }

哪里有问题?在MEF增强课程?我使用clasic MEF boostrape: http://devlicio.us/blogs/rob_eisenberg/archive/2010/07/08/caliburn-micro-soup-to-nuts-pt-2-customizing-the-bootstrapper.aspx

感谢您的建议,时间和帮助。

1 个答案:

答案 0 :(得分:1)

您仍需要导出要导出的类。使用ExportFactory可以创建多个实例,但如果您没有要创建实例的东西的导出属性,则它不起作用。