Hello Prism
上一节我们完成了Prism 开发环境的部署,接下来我们开始正式的学习吧。
身为初学者,一定要先从简单的开始。而最简单的方法莫过于现学现卖和照葫芦画瓢。掌握了这一精髓,接下来我们从Prism官方的Code Example开始这一过程。
GitHub上的Code Example是基于7.1版本的Prism写了,不是最新的7.2版本,但我们可以在这个基础上结合强大的IDE和Prism Source Code进行简单的修改,就可以轻松的做到现学先卖。接下来开始做第一次尝试。
Prism Code Example
GitHub下载地址:链接地址
PS:所有的Example都在这里。
Code:01-BootstrapperShell
直接在PrismLibrary_Wpf.sln 中修改原Solution下的工程,去掉Test相关工程,在BootstrapperShell工程Add Reference中添加Prism,Prism.Unity.WPF,Prism.WPF工程引用。
先Build,检查下当前存在的Error。
最主要的错误是红色高亮中的,UnityBootstrapper已经被弃用:It is recommended to use the new PrismApplication as the app’s base class. This will require updating the App.xaml and App.xaml.cs files.
简单介绍下UnityBootstrapper是干什么的,Bootstrapper就是一个启动器。在7.1版本以及之前版本WPF程序运行时会先运行这里的CreateShell方法和InitializeShell方法,然后才能显示出来程序页面。
接下来我们按照提示的进行操作,UnityBootstrapper已经被弃用,那么BootStrapper.cs文件就没有存在的意义了。在删除这个cs文件前,我们需要在Prism源代码中看一下:PrismApplication这个类是干什么用的。既然可以顶替UnityBootstrapper类,那么至少有相似的CreateShell和InitializeShell方法。
我们在Prism.Unity.WPF工程中找到了此cs文件,同时发现它是PrismApplicationBase的派生类。而PrismApplicationBase则继承了Application类。熟悉WPF的都知道,App.xaml这里的App类继承的就是Application类。在Application类下,有一个关键的方法叫做StartUp,上Code
// Summary:
// Raises the System.Windows.Application.Startup event.
//
// Parameters:
// e: A System.Windows.StartupEventArgs that contains the event data.
protected virtual void OnStartup(StartupEventArgs e);
通过这段code的Summary我们就能够知道Startup方法的作用:程序在启动后,会自动触发此事件。
同样,在PrismApplicationBase类中,我们能够看到它也重写了此方法,并在方法中调用了InitializeInternal方法初始化Prism的各种资源、Module等,这样我们就知道PrismApplicationBase类已经包含了UnityBootstrapper类的初始化方法的。
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
InitializeInternal();
}
/// <summary>
/// Run the initialization process.
/// </summary>
void InitializeInternal()
{
ConfigureViewModelLocator();
Initialize();
OnInitialized();
}
关于PrismApplication和PrismApplicationBase类我们先了解这些就够了。至于细节的东西,以后会有机会接触到的。我们先修改Code Example。
首先,修改App.xaml文件,将这里xaml标签的改成。这里VS IDE会提示添加引用:xmlns:unity=“http://prismlibrary.com/” 。
<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:App类继承PrismApplication类。并将App类中重写的OnStartup方法删除掉。
- IDE提示:有2个虚方法:RegisterTypes和CreateShell没有实现,我们把他们重写下。
- 重写时发现CreateShell方法需要个Window返回值,一看Window大家肯定想到了MainWindow.xaml。OK,接下来就是让程序在启动时能够找到MainWindow.xaml了。
- 这里大家可以参考原来BootStrapper.cs中的CreateShell方法了。将里面的内容复制粘贴过来即可。
- 移除BootStrapper.cs文件。
- Build,出错,发现缺少System.ValueTuple.dll将其引入到工程。再次Build。
- Yeah,Build通过。最终App.xaml.cs中App类的内容如下:
public partial class App : PrismApplication
{
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
}
protected override Window CreateShell()
{
return Container.Resolve<MainWindow>();
}
}
- 简单修改MainWindow.xaml中的内容,再次Build。
<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 FontSize="25" Foreground="BlueViolet" HorizontalAlignment="Center" VerticalAlignment="Center">Hello Prism</TextBlock>
</Grid>
</Window>
- 最后输出页面如下:
至此,第一个程序Hello Prism已经修改完成并且执行通过。通过此程序,我们学会了如何使用Prism制作一个最基础的WPF Application。简要的步骤如下:
- 添加引用。
- 修改App.xaml和App.xaml.cs。
- 在App.xaml.cs文件中,重写2个虚方法,并且在CreateShell方法中获取MainWindow。
- Build,执行即可完成。
但是这些也只是运行起来了Prism的程序,但是还没有提到Prism的任何特点。我们会在后续的Code Example中,会慢慢接触到。