wpf - 无应用程序对象模式下的 Caliburn micro,就像在 AutoCAD dll 插件中一样

标签 wpf caliburn.micro autocad

我正在使用 Caliburn Micro 开发 WPF 应用程序。此应用程序的一些 View 需要加载到 AutoCAD 环境中。 AutoCAD 编程环境允许开发 AutoCAD 插件(dll 类型)并将它们加载到 AutoCAD 环境中。

由于 AutoCAD 插件类型 (dll),插件没有应用程序对象,因此必须为此自定义 Bootstrap 。根据 Caliburn Micro 文档 here (Scroll down to "Using Caliburn.Micro in Office and WinForms Applications")我们可以继承非通用 Bootstrap 并将“false”传递给基本构造函数的“useApplication”参数。所以,我继续创建了定制的 Bootstrap 。

问题是永远不会调用 ConfigureContainer() 重写,也不会初始化任何内容。另外,我不确定如何使用 ViewModel 优先概念加载 ShellView。这是我到目前为止提出的一些代码。

Bootstrap

public class AutocadMefBootStrapper : Bootstrapper {

    private CompositionContainer container;
    private ElementHost host;

    public AutocadMefBootStrapper(ElementHost host) : base(false) {
        this.host = host;
    }

    protected override void Configure() { //Not getting invoked.
        ...
        var rootViewModel = container.GetExportedValue<IShell>();
        var rootView = ViewLocator.LocateForModel(rootViewModel, null, null);
        host.Child = rootView;
    }
}

我有一个 Windows 窗体,AutoCAD 在需要时加载它。在 Windows 窗体的加载事件中,我创建了一个实例化的 cuztomized caliburn 微型 Bootstrap ,并期望 Bootstrap 执行所有魔法并加载 Shell。但是外壳不加载。我在 AutoCAD 中显示空白窗口。以下是 Windows 窗体的编码方式。

public partial class WinFormHost : Form {

    private void WinFormHost_Load(object sender, EventArgs e) {
        ElementHost host = new ElementHost();
        host.Dock = DockStyle.Fill;
        Controls.Add(host);
        AutocadMefBootStrapper bootStrapper = new AutocadMefBootStrapper(host);
    }
}

这是我的 ShellView

<UserControl x:Class="RelayAnalysis_Autocad.Views.ShellView"
    ...
    <Grid>
        <TextBlock>Hello There</TextBlock>
    </Grid>
</UserControl>

和 ShellViewModel

[Export(typeof(IShell))]
public class ShellViewModel : Conductor<object>, IShell {
    protected override void OnActivate() {
        base.OnActivate();
    }
}

总而言之,我正在尝试在未使用应用程序对象加载的托管环境中使用 Caliburn Micro。我无法配置 Caliburn Micro,因为 ShellView 从不加载。

最佳答案

这个问题已经解决了。问题是在 AutoCAD 本身中加载支持程序集 (dll) 时出错。请参阅this线。一旦程序集被正确加载,我就可以使用 Caliburn Micro,它也可以在非 WPF 环境中工作。

编辑: 我将逻辑地展示这个过程。我在纯 wpf 应用程序中开发的 wpf 屏幕将在 AutoCAD 插件中重新使用,但由于 autocad 插件是类库 (dll),因此没有可用的应用程序对象。启动 AutoCAD 时,插件代码会在我可以初始化 caliburn 微型 Bootstrap 的位置执行。这是相关的插件代码。

MyPlugin.cs

public class MyPlugin : IExtensionApplication {

    //Called when plugin is loaded. This is where I load xaml resources, since there is no App.xaml available

    void IExtensionApplication.Initialize() { 
         if (System.Windows.Application.Current == null) {
            new System.Windows.Application { ShutdownMode = ShutdownMode.OnExplicitShutdown }; 
        }
        System.Windows.Application.Current.Resources.MergedDictionaries.Add(System.Windows.Application.LoadComponent(
                new Uri("RelayAnalysis_Autocad;component/Content/Styles/CommonBrushes.xaml", UriKind.Relative)) as ResourceDictionary);
        System.Windows.Application.Current.Resources.MergedDictionaries.Add(System.Windows.Application.LoadComponent(
                new Uri("RelayAnalysis_Autocad;component/Content/Styles/Button.xaml", UriKind.Relative)) as ResourceDictionary);
    ...
        //Load Other xaml resources
    ...
        //Initialize the Bootstrapper
        AutocadMefBootStrapper bootstrapper = new AutocadMefBootStrapper();
    }

    //Called when plugin is unloaded
    void IExtensionApplication.Terminate() {
        // Do plug-in clean up here
        System.Windows.Application.Current.Shutdown();
    }

请注意,该应用程序具有显式关闭模式。这是必需的,虽然我不记得为什么!

Bootstrapper 没有太大区别,除了我们将 false 传递给基本构造函数(如文档中所述)。这是 Bootstrapper 的样子

AutocadMefBootStrapper.cs

public class AutocadMefBootStrapper : Bootstrapper {

    public static CompositionContainer container;

    public AutocadMefBootStrapper()
        : base(false) {
    }

    protected override void Configure() {

        //Create and Add Catalogs.
        AssemblyCatalog currentAssemblyCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());

        AssemblyCatalog domainAssemblyCatalog =
            new AssemblyCatalog(Assembly.GetAssembly(typeof(RelayAnalysis_Domain.Entity.Rack)));
    ...
    }

这是配置部分,仅在加载插件并配置 calibur micro 时发生一次。在此之后,代码与 AutoCAD 有点相关,但为了完整起见,我将共享。

QueryRelay.cs 此类接受来自 AutoCAD 用户的命令输入,然后显示请求的 View

public class QueryRelay {

    //This command is used to display a particular View. This is entered from AutoCAD Command Window
    public void QueryRelayCommand() {

        //Get the ViewModel for the screen from Container
        AcadRelayListViewModel relayListViewModel = AutocadMefBootStrapper.container.GetExportedValue<AcadRelayListViewModel>();
        IWindowManager windowManager = AutocadMefBootStrapper.container.GetExportedValue<IWindowManager>();
        windowManager.ShowWindow(relayListViewModel);
        ...
    }
}

由于 AutoCAD 中的窗口是使用 AutoCAD 的 API 显示的,因此我不得不稍微自定义 Caliburn Micro WindowManager。这是 CustomWindowManager 的代码

CustomWindowManager.cs

public class CustomWindowManager : WindowManager {

    public override void ShowWindow(object rootModel, object context = null, IDictionary<string, object> settings = null) {
        Autodesk.AutoCAD.ApplicationServices.Application.ShowModalWindow(null, CreateWindow(rootModel, false, null, null), false);
    }
}

我要求 CaliburnMicro 从 ViewModel(上面代码中的 rootModel)创建 View ,然后使用 AutoCAD API 将其加载到 AutoCAD 中。 View 的显示方式取决于托管应用程序(在我的例子中是 AutoCAD)。

最后必须在 BootStrapper Configure 中注册 CustomWindowManager

    protected override void Configure() {
        ...
        var batch = new CompositionBatch();
        batch.AddExportedValue<IWindowManager>(new CustomWindowManager());
        container.Compose(batch);
        ...
    }

问候, 涅盘

关于wpf - 无应用程序对象模式下的 Caliburn micro,就像在 AutoCAD dll 插件中一样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13658711/

相关文章:

c# - 如果出现任何错误,如何自动重新打开/重新启动 WPF 应用程序?

c# - 如何检测重叠的多边形?

c# - Caliburn 微 : DialogResult

c# - 关闭通过事务获取的 AutoCAD 对象

c# - 如何引用不在代码隐藏中但在另一个对象中的事件处理程序?

wpf - 双击与单击 - 它们是互斥的吗

wpf - Caliburn.Micro带 Controller ?

wpf - Caliburn.Micro 在 WPF 中拖放文件

c - C中的异常读取位置

lisp - AutoCAD 将文件另存为另一个路径中的同名副本 : "Object reference not set to an instance of an object"