Caliburn.Micro让它将MainView中的UserControl绑定到他们的ViewModels

我有一个MainView.xaml,绑定到MainViewModel就好了。

我想尝试的是将我在主窗体上的许多控件分成UserControls。

现在,我将UserControls与MainView一起放在Views文件夹中,并将它们命名为LeftSideControlView.xaml和RightSideControlView.xaml。 相应的ViewModel位于名为LeftSideControlViewModel等的ViewModels文件夹中。

我成功将usercontrols添加到主视图:

        

它们在设计师中正确显示。 这是xaml中的其中一个:

     

我使用Castle.Windsor在AppBootstrapper for Caliburn中添加了viewmodels及其接口。

  public class ApplicationContainer : WindsorContainer { public ApplicationContainer() { // Register all dependencies here Register( Component.For().ImplementedBy().LifeStyle.Is(LifestyleType.Singleton), Component.For().ImplementedBy().LifeStyle.Is(LifestyleType.Singleton), Component.For().ImplementedBy(), Component.For().ImplementedBy() ); RegisterViewModels(); } private void RegisterViewModels() { Register(AllTypes.FromAssembly(GetType().Assembly) .Where(x => x.Name.EndsWith("ViewModel")) .Configure(x => x.LifeStyle.Is(LifestyleType.Transient))); } 

这是LeftSideControlViewModel类:

  using Screen = Caliburn.Micro.Screen; namespace TwitterCaliburnWPF.Library.ViewModels { public class LeftSideControlViewModel : Screen, ILeftSideControlViewModel { private string _text = "Hello from the Left side!"; private string _textBox1 = "Enter Text Here"; public string Text { get { return _text; } } public string TextBox1 { get { return _textBox1; } } } } 

这是MainViewModel,我将在Caliburn.Micro文档中读到的内容,就像之前我尝试过的那样,MainViewModel中没有任何内容告诉它加载这两个控件或显示这两个控件。

仍然当应用程序启动并运行时,值不会绑定到各自视图模型中的用户控件。

 namespace TwitterCaliburnWPF.Library.ViewModels { public class MainViewModel : Conductor { public MainViewModel() { ShowLeftControl(); ShowRightControl(); } private void ShowRightControl() { ActivateItem(new RightSideControlViewModel()); } private void ShowLeftControl() { ActivateItem(new LeftSideControlViewModel()); } public string TextToDisplay { get { return "Coming from the ViewModel!"; } } } } 

您无需在此处使用Conductor 。 这基本上用于导航场景。 只需在MainViewModel上创建两个公共属性,一个用于RightSideControlViewModel,一个名为RightSide,另一个用于LeftSideControlViewModel ,名为LeftSide。 然后,不是直接在MainView中实例化UserControl,而是创建两个ContentControls ,一个用x:Name="LeftSide" ,另一个用x:name="RightSide"这是一个视图模型第一种完成它的方法。 如果要先查看视图,请在MainView中保留用户控件定义,但更改Bind.Model以使其指向您创建的新属性,如Bind.Model="{Binding LeftSide}"

基本上,你有事物的方式定义….绑定只是没有指向正确的对象,或多或少。 你有导体在那里,你不需要完成这个。 如果您打算使用某种导航架构,可能需要保留它。 请记住,当您在Conductor上调用ActivateItem时,您基本上正在更改其ActiveItem属性; 只有一个模型一次处于活动状态。 在上面的情况下,您激活两个项目,但只有第二个项目保持活动状态。 此外,在您的视图中,ActiveItem没有任何约束。

我希望这是有道理的!