从PRISM开始学WPF(二)Prism-更新至Prism7.1

53 篇文章 5 订阅

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类

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();
        }
    }
}

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

当然你也可以不用使用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方法。

[7.1update]Prism.UnityUnityBootstrapper被标记为 deprecated,并且建议使用 PrismApplication作为应用的基类,并且在7.1中Bootstrapper类已经不再使用,入口代码整合到app.xaml及app.xaml.cs中去了,但这一节不影响我们来了解wpf及prism,在接下来的例子中我会将实例代码更新到7.1。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值