WPF Prism框架


Prism框架
1、關於Prism框架

​ 官方地址:http://prismlibrary.com

​ 官方源碼:https://github.com/PrismLibrary/Prism

​ 版本:8.1

2、功能說明

​ Prism提供了一組設計模式的實現,有助於編寫結構良好的可維護XAML應用程序。

​ 包括MVVM 依賴注入 命令 事件聚合器

​ Prism減重

​ Autofac 、Dryloc 、 Mef 、Niniject 、StruyctureMap、Unity。

3、Prism的關鍵程序

​ Prism.Core 實現MVVM的核心功能,屬於一個與平台無關的項目。

​ Prism.Wpf 包含了DialogService【彈窗】 Region【注冊】 Module 【模塊】 Navigation【導航】 其他的一些WPF 功能。

​ Prism.Unity Prism.Unity.Wpf.dll Prism.Dryloc.Wpf.dll

4、獲取Prism框架

​ Prism.dll Prism.core

​ Prism.Wpf.dll Prism.Wpf

​ Prism.Unity.Wpf.dll Prism.Unity

​ Prism.Dryloc.Wpf.dll Prism.Dryloc

5、數據處理,數據通知的五種方式

​ 新建類:MainViewModel 繼承:BindableBase

using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Text;

namespace WpfApp2
{
    public class MainViewModel : BindableBase
    {
        private int _value = 100;
        public int Value
        {
            get { return _value; }
            set
            {
                //通知的五種方式
                //第一種方式
                //SetProperty(ref _value, value);  
                //第二種方式
                //this.OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("Value"));
                //第三種方式
                //this.RaisePropertyChanged();
                //第四種方式   Value :可以通知其他屬性  
                SetProperty(ref _value, value, "Value");
                //第五種方式
                SetProperty(ref _value, value, OnPropertyChanged);
            }
        }
        public void OnPropertyChanged()
        {

        }
        public MainViewModel()
        {
        }
    }
}

xaml代碼:

<Window x:Class="WpfApp2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp2"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.DataContext>
        <local:MainViewModel></local:MainViewModel>
    </Window.DataContext>
    <Grid>
        <StackPanel>
            <TextBlock Text="{Binding Value}"></TextBlock>
        </StackPanel>
    </Grid>
</Window>

6、行為處理

​ 1、Prism的行為處理

​ DelegateCommand

​ 基本用法 :

新建類:MainViewModel 繼承 BindableBase

using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Input;

namespace WpfApp4
{
    public class MainViewModel:BindableBase
    {
        private string _value = "Hellow  Word!";
         public ICommand BtnCommand { get => new DelegateCommand(doExecute); } 
        public string  Value
        {
            get { return _value; }
            set {  
                SetProperty(ref _value, value); 
            }
        }
        public MainViewModel() 
        {
             
        }

        public void doExecute() 
        {
            this.Value = "Fuck You!";
        }
      
    }
}

​ 檢查狀態

第一種檢查狀態:

using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Input;

namespace WpfApp4
{
    public class MainViewModel:BindableBase
    {
        private string _value = "Hellow  Word!";
       
         public DelegateCommand BtncheckCommand { get; }
        public string  Value
        {
            get { return _value; }
            set {  
                SetProperty(ref _value, value);
                 //觸發命令相關的可執行的檢查邏輯 
                BtncheckCommand.RaiseCanExecuteChanged();
            }
        }
        public MainViewModel() 
        {
            BtncheckCommand = new DelegateCommand(doExecute,doCheckExecute);
        }

        public void doExecute() 
        {
            this.Value = "Fuck You!";
        }
        public bool doCheckExecute()
        {
            return this.Value == "Hellow  Word!";
        }
    }
}

第二種檢查狀態:

using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Input;

namespace WpfApp4
{
    public class MainViewModel:BindableBase
    {
        private string _value = "Hellow  Word!";
       
         public DelegateCommand BtncheckCommand { get; }
        public string  Value
        {
            get { return _value; }
            set {  
                SetProperty(ref _value, value); 
            }
        }
        public MainViewModel() 
        {
            //ObservesProperty : 監聽一個屬性的變化,當這個屬性值發生變化后,進行狀態檢查
            //當Value發生變化的時候主動去觸發一個狀態檢查
            //可以監聽多個屬性  后面.ObservesProperty(()=>Value)
            BtncheckCommand = new DelegateCommand(doExecute, doCheckExecute).ObservesProperty(()=>Value); 
        } 吧
        public void doExecute() 
        {
            this.Value = "Fuck You!";
        }
        public bool doCheckExecute()
        {
            return this.Value == "Hellow  Word!";
        }
    }
} 

第三種檢查方式:

using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Input;

namespace WpfApp4
{
    public class MainViewModel:BindableBase
    {
        private string _value = "Hellow  Word!";
         public ICommand BtnCommand { get => new DelegateCommand(doExecute); }
         public DelegateCommand BtncheckCommand { get; }
        public string  Value
        {
            get { return _value; }
            set {  
                SetProperty(ref _value, value); 
            }
        }
        private bool _state;

        public bool State
        {
            get { return Value == "Hellow  Word!"; }
            set {
                SetProperty(ref _state, value); 
            }
        }

        public MainViewModel() 
        { 
             //第三種檢查方式  通過  ObservesCanExecute  進行一個屬性值觀察,進行動態的狀態處理
             BtncheckCommand = new DelegateCommand(doExecute).ObservesCanExecute(() => State);
        }

        public void doExecute() 
        {
            this.Value = "Fuck You!";
            this.State =!this.State;
        } 
    }
}

xaml代碼:

<Window x:Class="WpfApp4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp4"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.DataContext>
        <local:MainViewModel></local:MainViewModel>
    </Window.DataContext>
    <Grid>
        <StackPanel>
            <TextBlock Text="{Binding Value}"></TextBlock>
            <Button Content="基本命令" Height="30" Command="{Binding BtnCommand}"></Button>
            <Button Content="屬性檢查命令" Height="30" Command="{Binding BtncheckCommand}"></Button>
        </StackPanel>
    </Grid>
</Window>
			

​ 異步處理

using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace WpfApp4
{
    public class MainViewModel:BindableBase
    {
        private string _value = "Hellow  Word!"; 
        public ICommand BtnAsyncCommand { get => new DelegateCommand(doExecuteAsync); } 
         public DelegateCommand BtncheckCommand { get; }
        public string  Value
        {
            get { return _value; }
            set {  
                SetProperty(ref _value, value); 
            }
        }
      

        public MainViewModel() 
        {
       
        }

 
        public async void doExecuteAsync() 
        {
            await Task.Delay(5000);
            this.Value = "再見!";
        }
    }
}

xaml代碼:

<Window x:Class="WpfApp4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp4"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.DataContext>
        <local:MainViewModel></local:MainViewModel>
    </Window.DataContext>
    <Grid>
        <StackPanel>
            <TextBlock Text="{Binding Value}"></TextBlock> 
            <Button Content="異步命令" Height="30" Command="{Binding BtnAsyncCommand}"></Button>
        </StackPanel>
    </Grid>
</Window>

​ 泛型參數

using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace WpfApp4
{
    public class MainViewModel:BindableBase
    {
        private string _value = "Hellow  Word!"; 
        public ICommand BtnAsyncCommand { get => new DelegateCommand<string>(doExecuteAsync); }

         public DelegateCommand BtncheckCommand { get; }
        public string  Value
        {
            get { return _value; }
            set {  
                SetProperty(ref _value, value);
                //觸發命令相關的可執行的檢查邏輯
               // BtncheckCommand.RaiseCanExecuteChanged();
            }
        }
         
        public async void doExecuteAsync(string str) 
        {
            await Task.Delay(5000);
            this.Value = str;
        }
    }
}

xaml代碼:

<Window x:Class="WpfApp4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp4"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.DataContext>
        <local:MainViewModel></local:MainViewModel>
    </Window.DataContext>
    <Grid>
        <StackPanel>
            <TextBlock Text="{Binding Value}"></TextBlock> 
            <Button Content="異步命令" Height="30" Command="{Binding BtnAsyncCommand}" CommandParameter="再見"></Button>
        </StackPanel>
    </Grid>
</Window>

​ 事件命令 [事件轉命令] InvokeCommandAction

​ 新建類:MainViewModel:BindableBase

using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace WpfApp4
{
    public class MainViewModel:BindableBase
    { 
        public ICommand BtnEventCommand { get => new DelegateCommand<object>(doEventExecute); } 
        public void doEventExecute(object args) 
        {
        
        }
    }
}

xaml代碼:

<Window x:Class="WpfApp4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
        xmlns:prism="http://prismlibrary.com/"
        xmlns:local="clr-namespace:WpfApp4"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.DataContext>
        <local:MainViewModel></local:MainViewModel>
    </Window.DataContext>
    <Grid>
        <StackPanel>
            <TextBlock Text="{Binding Value}"></TextBlock>
            <Button Content="基本命令" Height="30" Command="{Binding BtnCommand}"></Button>
            <Button Content="屬性檢查命令" Height="30" Command="{Binding BtncheckCommand}"></Button>
            <Button Content="異步命令" Height="30" Command="{Binding BtnAsyncCommand}" CommandParameter="再見"></Button>

            <ComboBox  SelectedIndex="0">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="SelectionChanged">
                        <prism:InvokeCommandAction Command="{Binding BtnEventCommand}"
                                                   TriggerParameterPath=""></prism:InvokeCommandAction>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
                <ComboBoxItem Content="111"></ComboBoxItem>
                <ComboBoxItem Content="222"></ComboBoxItem>
                <ComboBoxItem Content="333"></ComboBoxItem>
                <ComboBoxItem Content="444"></ComboBoxItem>
            </ComboBox>
        </StackPanel>
    </Grid>
</Window> 
備注:
頂部引用兩個命名空間:
  xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
  xmlns:prism="http://prismlibrary.com/"
   Prism框架引用初始化

​ PrismBootstrapper 啟動器

​ 1、新建Startup 類,繼承:PrismBootstrapper

using Prism.Ioc;
using Prism.Unity;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;

namespace WpfApp5
{
    public class Startup : PrismBootstrapper
    {  
        /// <summary>
        /// 返回一個Shell  :主窗口
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        protected override DependencyObject CreateShell()
        {
            throw new NotImplementedException();
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="containerRegistry"></param>
        /// <exception cref="NotImplementedException"></exception>
        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            throw new NotImplementedException();
        }
    }
}

2、將App.xaml代碼注釋

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

3、在App.xaml.cs 構造函數中啟動

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace WpfApp5
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        public App()
        {
            new Startup().Run();
        }
    }
}

用 PrismApplication 啟動器

修改App.xaml代碼

<prism:PrismApplication 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"
             xmlns:prism="http://prismlibrary.com/">
             <!--StartupUri="MainWindow.xaml"  注釋掉-->
    <Application.Resources>
         
    </Application.Resources>
</prism:PrismApplication>
備注: 引用    xmlns:prism="http://prismlibrary.com/" 
       將頭尾標簽改為 :prism:PrismApplication 

App.xaml.cs 繼承 PrismApplication,代碼

using Prism.Ioc;
using Prism.Unity;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : PrismApplication
    {
        /// <summary>
        /// 返回一個shell 【窗口】
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        protected override Window CreateShell()
        {
             return Container.Resolve<MainWindow>();    
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
             
        }
    }
}

基於Prism框架的登錄跳轉

App.xaml代碼:

<prism:PrismApplication 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"
             xmlns:prism="http://prismlibrary.com/"
                        >
             <!--StartupUri="MainWindow.xaml"-->
    <Application.Resources>
         
    </Application.Resources>
</prism:PrismApplication>

App.xaml.cs代碼:

using Prism.Ioc;
using Prism.Unity;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : PrismApplication
    {
        /// <summary>
        /// 返回一個shell 【窗口】
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        protected override Window CreateShell()
        {
            return Container.Resolve<MainWindow>();
        }
        /// <summary>
        /// 初始化 shell  主窗口
        /// </summary>
        /// <param name="shell"></param>
        protected override void InitializeShell(Window shell)
        {
            var loginWindow = Container.Resolve<Login>();
            if (loginWindow == null || loginWindow.ShowDialog() == false)
            {
                Application.Current.Shutdown();
            }
        }
        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {

        }
    }
}

​ 登錄代碼:Login.xaml

<Window x:Class="WpfApp1.Login"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="Login" Height="450" Width="800">
    <Grid>
        <Button Click="Button_Click"></Button>
    </Grid>
</Window>

Login.xaml.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApp1
{
    /// <summary>
    /// Login.xaml 的交互邏輯
    /// </summary>
    public partial class Login : Window
    {
        public Login()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }
    }
}

MainWindow.xaml 代碼:

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>

    </Grid>
</Window>

ViewModelLocator對象 幫助進行 View與ViewModel的綁定

​ 標准狀態:

​ AutoWireViewModel,默認行為true

​ ViewModel與視圖類型位於同一個程序集中

​ ViewModel位於.ViewModel子命名空間中

​ 視圖位於.Views子命名空間中

​ ViewModel名稱與視圖名稱對應 ,以“ViewModel”結尾

​ 實例代碼如下:

​ 新建子文件夾:ViewModels/MainWindowViewModel 類,代碼如下:

using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Text;

namespace WpfApp6.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        private string _value = "  Hellow Word";

        public string Value
        {
            get { return _value; }
            set
            {
                SetProperty(ref _value, value);
            }
        }

    }
}

新建子文件夾,Views/MainWindow.xaml,代碼如下:

<Window x:Class="WpfApp6.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:prism="http://prismlibrary.com/"
        xmlns:local="clr-namespace:WpfApp6.Views"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <TextBlock Text="{Binding Value}"></TextBlock>
    </Grid>
</Window>

設置啟動項:

App.xaml,代碼如下:

<prism:PrismApplication x:Class="WpfApp6.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApp6"
             xmlns:prism="http://prismlibrary.com/"
            >
    <Application.Resources>
         
    </Application.Resources>
</prism:PrismApplication>

App.xaml.cs,代碼如下:

using Prism.Ioc;
using Prism.Unity;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace WpfApp6
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : PrismApplication
    {
        protected override Window CreateShell()
        {
             return  Container.Resolve<Views.MainWindow>();   
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
             
        }
    }
}

目錄結構圖如下:【新建的文件夾一定要符合 框架要求】
在這里插入圖片描述


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM