Caliburn.Micro: Create and Bind View programmatically

被刻印的时光 ゝ 提交于 2019-12-21 12:19:27

问题


I am currently experimenting with view composition in Caliburn.Micro. I have a working example where I have multiple user control based views injected into my main shell via the "View.Model" attached property route. So far so good.

In my application proper I am working with a mixed environment of mainly WinForms, with some WPF, so there is no WPF "shell" for Caliburn to manage. I'd like to be able to create my views on demand and add them to placeholders in my WinForms app.

I would like to know how I go about creating a view (which will be a user control containing sub user controls) programmatically using Caliburn so that all conventions, model bindings and sub-view injection is carried out.


回答1:


The Caliburn ViewModelBinder can be used to crank the handle once you have a view instance and a corresponding view-model. Calling Bind resolves injected views and applies convention based binding, etc:

    SomeCompositionView view = new SomeCompositionView();

    ISomeCompositionViewModel viewModel = IoC.Get<ISomeCompositionViewModel>();

    ViewModelBinder.Bind(viewModel, view, null);

    ElementHost.Child = view;  



回答2:


Code snippet from BootstrapperBase.DisplayRootViewFor:

var viewModel = IoC.GetInstance(viewModelType, null);
var view = ViewLocator.LocateForModel(viewModel, null, null);

ViewModelBinder.Bind(viewModel, view, null);

var activator = viewModel as IActivate;
if(activator != null)
    activator.Activate();


来源:https://stackoverflow.com/questions/6333032/caliburn-micro-create-and-bind-view-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!