首页 > 解决方案 > 如何将 WPF 视图注入从 VSTO 插件 Prism 7 调用的窗口中

问题描述

我正在使用 Prism 7 创建一个大型 WPF 桌面应用程序,我需要为 Word 和 Excel 创建几个 VSTO 加载项,这些加载项是较大整体的较小部分。我想使用 Prism 7 将我已经创建的视图和视图模型注入到 xaml 窗口中,单击功能区上的按钮时会显示该窗口。

我不知道在哪里初始化引导程序,因为我不熟悉 VSTO 加载项。

我已经创建了一个引导程序,它使用包含必要视图的正确模块配置模块目录。引导程序如下。

public sealed class Bootstrapper : PrismApplication
{
    protected override Window CreateShell()
    {
        return Container.Resolve<MainWindow>(); //Where MainWindow is the window that should be shown when the button is clicked.
    }

    protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
    {
        base.ConfigureModuleCatalog(moduleCatalog);

        moduleCatalog.AddModule(typeof(MyModule)); //Where MyModule is the Prism module for the project that contains the views I want to inject
    }
}

当我从 ThisAddIn.cs 文件初始化引导程序时,如下所示,引导程序正确地创建自己并配置模块目录。之后容器能够解析 RegionManager,但它也会自动向用户显示 MainWindow。

public partial class ThisAddIn
{
    private Bootstrapper _bootstrapper;

    private void ThisAddIn_Startup(object sender, EventArgs e)
    {
        _bootstrapper = new Bootstrapper(); //MainWindow is displayed after everything runs
    }
}

如果我打开 MainWindow 并单击应该将视图注入 MainWindow 的按钮,则视图被正确注入。但是,我显然不想在用户每次打开 Word 文档时自动显示一个空白窗口。有没有办法阻止引导程序此时显示 MainWindow 或者有更好的方法来解决这个问题?

标签: c#bootstrapperprism-7view-injection

解决方案


推荐阅读