Prism框架
1官脓、關(guān)于Prism框架
官方地址:http://prismlibrary.com
官方源碼:https://github.com/PrismLibrary/Prism
版本:8.1
2哗蜈、功能說明
Prism提供了一組設(shè)計(jì)模式的實(shí)現(xiàn)野建,有助于編寫結(jié)構(gòu)良好的可維護(hù)XAML應(yīng)用程序。
包括MVVM 依賴注入 命令 事件聚合器
Prism減重
Autofac 恬叹、Dryloc 、 Mef 同眯、Niniject 绽昼、StruyctureMap、Unity须蜗。
3硅确、Prism的關(guān)鍵程序
Prism.Core 實(shí)現(xiàn)MVVM的核心功能,屬于一個(gè)與平臺(tái)無關(guān)的項(xiàng)目明肮。
Prism.Wpf 包含了DialogService【彈窗】 Region【注冊(cè)】 Module 【模塊】 Navigation【導(dǎo)航】 其他的一些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柿估、數(shù)據(jù)處理循未,數(shù)據(jù)通知的五種方式
新建類: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!";
}
}
}
檢查狀態(tài)
第一種檢查狀態(tài):
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);
//觸發(fā)命令相關(guān)的可執(zhí)行的檢查邏輯
BtncheckCommand.RaiseCanExecuteChanged();
}
}
public MainViewModel()
{
BtncheckCommand = new DelegateCommand(doExecute,doCheckExecute);
}
public void doExecute()
{
this.Value = "Fuck You!";
}
public bool doCheckExecute()
{
return this.Value == "Hellow Word!";
}
}
}
第二種檢查狀態(tài):
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 : 監(jiān)聽一個(gè)屬性的變化的妖,當(dāng)這個(gè)屬性值發(fā)生變化后绣檬,進(jìn)行狀態(tài)檢查
//當(dāng)Value發(fā)生變化的時(shí)候主動(dòng)去觸發(fā)一個(gè)狀態(tài)檢查
//可以監(jiān)聽多個(gè)屬性 后面.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 進(jìn)行一個(gè)屬性值觀察,進(jìn)行動(dòng)態(tài)的狀態(tài)處理
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>
泛型參數(shù)
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);
//觸發(fā)命令相關(guān)的可執(zhí)行的檢查邏輯
// 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>
事件命令 [事件轉(zhuǎn)命令] 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>
備注:
頂部引用兩個(gè)命名空間:
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
xmlns:prism="http://prismlibrary.com/"
Prism框架引用初始化
PrismBootstrapper 啟動(dòng)器
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>
/// 返回一個(gè)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 構(gòu)造函數(shù)中啟動(dòng)
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 啟動(dòng)器
修改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/"
將頭尾標(biāo)簽改為 :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>
/// 返回一個(gè)shell 【窗口】
/// </summary>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
protected override Window CreateShell()
{
return Container.Resolve<MainWindow>();
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
}
}
}
基于Prism框架的登錄跳轉(zhuǎn)
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>
/// 返回一個(gè)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對(duì)象 幫助進(jìn)行 View與ViewModel的綁定
標(biāo)準(zhǔn)狀態(tài):
AutoWireViewModel宽涌,默認(rèn)行為true
ViewModel與視圖類型位于同一個(gè)程序集中
ViewModel位于.ViewModel子命名空間中
視圖位于.Views子命名空間中
ViewModel名稱與視圖名稱對(duì)應(yīng) 平夜,以“ViewModel”結(jié)尾
實(shí)例代碼如下:
新建子文件夾: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>
設(shè)置啟動(dòng)項(xiàng):
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)
{
}
}
}
目錄結(jié)構(gòu)圖如下:【新建的文件夾一定要符合 框架要求】
1646286093(1).jpg