【问题标题】:Using Caliburn Micro in class library在类库中使用 Caliburn Micro
【发布时间】:2016-01-20 16:34:52
【问题描述】:

我有一个包含多个用户控件的类库。该库使用 caliburn micro 将UserControls 绑定到它们对应的ViewModels,使用同一类库中的x:Name 方法。

现在我有一个宿主应用程序,它将这些用户控件托管在一个窗口中。但是这个主机应用程序根本不使用 Caliburn Micro。那么如何在库本身中初始化 caliburn 引导程序。

主机应用程序:

<Window>       
  <ContentControl>
       <library:MainUserControl DataContext="{Binding MainUserControlViewModel}"/>
  </ContentControl>
</Window>

类库

<UserControl x:Class="MainUserControl">
   <ContentControl x:Name="OtherUserControlInSameLibrary"/>
</UserControl>

我尝试继承像AppBootstrapper: Bootstrapperbase 这样的引导程序基类并将false 传递给构造函数参数useApplication。然后我调用了Initialize() 方法。但是仍然没有应用约定。

MainUserControlViewModel 构造函数

public MainUserControlViewModel()
{            
     AppBootstrapper appBootstrapper = new AppBootstrapper();
     appBootstrapper.Initialize();
     if (Debugger.IsAttached)
            LogManager.GetLog = type => new DebugLog(type);            
}

BootstrapperBase 覆盖

 public class AppBootstrapper : BootstrapperBase
    {
        public AppBootstrapper():base(false)
        {
        }
    }

我也尝试运行 caliburn 调试器,但我的输出窗口中没有显示任何内容。

【问题讨论】:

标签: c# wpf caliburn.micro


【解决方案1】:

将 Caliburn.Micro mantainers 提到的 feedback 和 Andy Race 提到的 this blog post 放在一起,这似乎是可行的,但客户端应用程序代码仍然需要引用 CM。

除了其他需要的设置(参见参考资料)之外,约束是由于自定义控件视图模型如何绑定到其在 XAML 中声明的视图:唯一的方法是通过 @987654323 显式设置视图模型@。

...
xmlns:controls="clr-namespace:Control.Library;assembly=Control.Library" 
...
<controls:SomeControlView 
    cal:Bind.Model="Control.Library.SomeControlViewModel" />

对于其余部分,客户端应用程序可以完全忽略 Caliburn.Micro。

【讨论】:

    猜你喜欢
    • 2013-08-08
    • 2020-05-22
    • 1970-01-01
    • 2012-12-29
    • 1970-01-01
    • 2012-01-29
    • 2012-10-24
    • 2012-05-08
    • 1970-01-01
    相关资源
    最近更新 更多