zoukankan      html  css  js  c++  java
  • 从PRISM开始学WPF(二)认识Prism-更新至Prism7.1

    0x1 PRISM?

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

    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来初始化程序和启动窗口(bootstrapper在7.1中不再被推荐使用)。在 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方法。

  • 相关阅读:
    Leetcode Palindrome Linked List
    Leetcode Delete Node in a Linked List
    Leetcode Ugly Number
    Python 列表解析
    Python 生成器以及应用
    Python 迭代器协议以及可迭代对象、迭代器对象
    Python 装饰器
    Python 函数的嵌套
    Python 闭包函数
    Python 名称空间与作用域
  • 原文地址:https://www.cnblogs.com/hicolin/p/8694828.html
Copyright © 2011-2022 走看看