且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从PRISM开始学WPF(二)Prism?

更新时间:2022-09-22 22:54:28

原文:从PRISM开始学WPF(二)Prism?

目录:

从PRISM开始学WPF(一)WPF?

从PRISM开始学WPF(二)Prism?

从PRISM开始学WPF(三)Prism-Region?

从PRISM开始学WPF(四)Prism-Module?

从PRISM开始学WPF(五)MVVM(一)ViewModel?

从PRISM开始学WPF(六)MVVM(二)Command?

从PRISM开始学WPF(七)MVVM(三)事件聚合器EventAggregator?

0x1 PRISM?

PRISM项目地址:https://github.com/PrismLibrary/Prism

先看下简介:

Prism is a framework for building loosely coupled, maintainable, and testable XAML applications in WPF, Windows 10 UWP, and Xamarin Forms.

谷歌翻译:

Prism是一个框架,用于在WPF,Windows 10 UWP和Xamarin Forms中构建松散耦合,可维护和可测试的XAML应用程序。

可以看出PRISM并不仅仅是一个MVVM框架,他提供了一系列设计模式的实现。这听上去就很Dior了。

0x2 Run

PRISM 不再使用App.xaml来为程序设置入口,而是使用 Bootstrapper来初始化程序和启动窗口。在 PRISM的项目中,需要删除App.xaml中的StartupUri ,因为你不再需要使用他了。

通常情况下,你的App.xaml是这样的:

<Application x:Class="WpfApp1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApp1"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
         
    </Application.Resources>
</Application>

而PRISM项目中的App.xaml是这样的:

<Application 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">
    <Application.Resources>
         
    </Application.Resources>
</Application>

PRISM项目中,在 App.xaml.cs中重写了OnStartup 方法,让app从Bootstrapper启动:

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            var bootstrapper = new Bootstrapper();
            bootstrapper.Run();
        }

Bootstrapper.cs,中的CreateShell方法来创建shell,InitializeShell初始化shell。这里的shell是宿主应用程序,就相当于是我们的主窗体程序,其他的view和module都将会被加载到shell中显示。下面是Bootstrapper.cs类内容:

using Microsoft.Practices.Unity;
using Prism.Unity;
using BootstrapperShell.Views;
using System.Windows;

namespace BootstrapperShell
{
    class Bootstrapper : UnityBootstrapper
    {
        protected override DependencyObject CreateShell()
        {
            return Container.Resolve<MainWindow>();
        }

        protected override void InitializeShell()
        {
            Application.Current.MainWindow.Show();
        }
    }
}

当然你也可以不用使用MainWindow,作为shell的窗口,可以改成任何你想要的名字,但他一定是Window类型,你还可以将他放在任何一个位置,为了将来适配MVVM思想,我们将他放在Views目录下面,以后我们所有的View都将放到这个目录下面。

那么,我们在初识WPF的时候,认识的App.g.cs呢?他里面不是有Main方法吗?我们在同样的位置找到他:

using BootstrapperShell;
using System.Windows.Shell;

namespace BootstrapperShell {   
    /// <summary>
    /// App
    /// </summary>
    public partial class App : System.Windows.Application {
        
        /// <summary>
        /// Application Entry Point.
        /// </summary>
        [System.STAThreadAttribute()]
        [System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
        public static void Main() {
            BootstrapperShell.App app = new BootstrapperShell.App();
            app.Run();
        }
    }
}

蛤蛤,他叛变了,App.g.cs看上去更像是一个在编译的时候才会生成的中间文件,根据App.xaml.cs中的OnStartup方法来重新生成了Main方法。