如何实现新的PrismApplication来替换Bootstrapper类

Tom*_*Tom 17 prism

由于BootstrapperClass已经过时使用Prism 7,我想使用PrismApplicationClass 更改我的C#WPF应用程序.

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

我问,因为有关这些预发行版的文档非常有限,而且我不是最了解我需要做什么才能看到Prism源代码.

Hau*_*ger 14

这取决于你的引导程序的功能,但是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/"/>
Run Code Online (Sandbox Code Playgroud)

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

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

    protected override Window CreateShell()
    {
        throw new NotImplementedException();
    }
}
Run Code Online (Sandbox Code Playgroud)


Ste*_*ric 9

我从GitHub 上Prism示例的示例 1 开始,并立即收到警告,指出使用 aUnityBootstrapper已被弃用。所以我试图将它升级到当前的机制。

第一步是将应用程序的基类从 替换ApplicationPrismApplicationin 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>

Run Code Online (Sandbox Code Playgroud)

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

/// <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)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

我还添加了一些标记以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>
Run Code Online (Sandbox Code Playgroud)

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