【问题标题】:How to implement the new PrismApplication to replace the Bootstrapper class如何实现新的 PrismApplication 来替换 Bootstrapper 类
【发布时间】:2017-12-20 15:28:11
【问题描述】:

由于 Prism 7 已废弃 Bootstrapper 类,我想使用 PrismApplication 类更改我的 C# WPF 应用程序。

有人知道如何使用 Bootstrapper : UnityBootstrapper 将 Prism 6 应用程序重构为新的 PrismApplication 吗?

我之所以这么问,是因为有关这些预发布版本的文档非常有限,而且我并不是最能从 Prism 源代码中理解我需要做什么。

【问题讨论】:

    标签: prism


    【解决方案1】:

    这取决于你的引导程序做什么,但Prism.Unity.PrismApplication 有类似的方法可以覆盖,所以你应该能够从引导程序复制代码。可能你只需要RegisterTypesCreateShell。记得更新App.xaml来改变你的应用类型...

    App.xaml 应该是这样的:

    <prism:PrismApplication x:Class="WpfApp1.App"
                            x:ClassModifier="internal" 
                            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                            xmlns:prism="http://prismlibrary.com/"/>
    

    为了完整起见,App.xaml.cs:

    internal partial class App
    {
        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            throw new NotImplementedException();
        }
    
        protected override Window CreateShell()
        {
            throw new NotImplementedException();
        }
    }
    

    【讨论】:

    • 好的,但ConfigureModuleCatalog()doesnt 似乎在那里/不再可覆盖。我现在应该如何注册我的模块?
    • 参见第 203 行:github.com/PrismLibrary/Prism/blob/master/Source/Wpf/Prism.Wpf/…:protected virtual void ConfigureModuleCatalog(IModuleCatalog moduleCatalog) { }
    • 记得更新App.xaml 以更改您的应用程序的类型...这是什么意思?需要改变什么?
    • 应用程序的基本类型在此处定义,默认为System.Windows.Application。在这里,我们希望它是 Prism.Unity.PrismApplication...
    • 我的问题是我没有将标签从 更新为
    【解决方案2】:

    我从 Prism samples on GitHub 的示例 1 开始,我立即收到警告说不推荐使用 UnityBootstrapper。所以我想把它升级到现在的机制。

    第一步是将应用程序的基类从Application 替换为PrismApplication 中的App.xaml。它现在应该是这样的;

    <unity:PrismApplication x:Class="BootstrapperShell.App"
                            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                            xmlns:local="clr-namespace:BootstrapperShell" 
                            xmlns:unity="http://prismlibrary.com/">
        <Application.Resources>
    
        </Application.Resources>
    </unity:PrismApplication>
    
    

    然后在App.xaml.cs中,去掉对Application的引用,实现PrismApplication的抽象方法。将Bootstrapper.cs 中的InitializeShell 的内容复制到CreateShell() 方法中。最终的结果应该是这样的;

    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App
    {
        protected override Window CreateShell()
        {
            return Container.Resolve<MainWindow>();
        }
    
        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
        }
    }
    

    我还为MainWindow.xaml 添加了一些标记,以确保正确解析;

    <Window x:Class="BootstrapperShell.Views.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Shell" Height="350" Width="525">
        <Grid>
            <TextBlock HorizontalAlignment="Center"
                       VerticalAlignment="Center"
                       FontSize="24">Hello</TextBlock>
        </Grid>
    </Window>
    

    一切都应该像以前一样工作。

    【讨论】:

    • 您能否详细说明为什么该应用需要公开?通常,人们会尽量保持组件的表面尽可能小......
    【解决方案3】:

    好的,所以叫我笨蛋或其他什么,但我遇到了同样的问题和同样的错误:

    App.RegisterTypes(IContainerRegistery):找不到合适的方法来覆盖

    我的问题是我去复制并粘贴了Here 中的App.xamlHere 中的cs,并且没有将App.xaml 中的名称空间从x:Class="Regions.App" 更改为x:Class="WpfApp.App"

    【讨论】:

    • 遇到了同样的问题,我重新启动了 Visual Studio,它后来工作正常
    猜你喜欢
    • 1970-01-01
    • 2020-12-13
    • 2013-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-31
    • 2013-10-06
    • 1970-01-01
    相关资源
    最近更新 更多