Prism框架初使用
一、创建普通WPF文件
二、NuGet添加Prism框架
三、建立ViewModels和Views文件夹,并且分别建立三个视图模块,以及MainViewModel类,处理视图数据
View1 View2 View3
三个视图页面模块为
<UserControl x:Class="PrismCSND.Views.View1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:PrismCSND.Views"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<TextBlock Text="这是页面一" FontSize="80"
HorizontalAlignment="Center"
VerticalAlignment="Center"/>
</Grid>
</UserControl>
<UserControl x:Class="PrismCSND.Views.View2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:PrismCSND.Views"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<TextBlock Text="这是页面二" FontSize="80"
HorizontalAlignment="Center"
VerticalAlignment="Center"/>
</Grid>
</UserControl>
<UserControl x:Class="PrismCSND.Views.View3"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:PrismCSND.Views"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<TextBlock Text="这是页面三" FontSize="80"
HorizontalAlignment="Center"
VerticalAlignment="Center"/>
</Grid>
</UserControl>
四、把第一级页面命名为MainView
并且修改其中的配置
代码为
<!--引入 xmlns:prism空间,并且把AutoWireViewModel开启自动注入,它会识别该规范下的命名-->
<Window x:Class="PrismCSND.Views.MainView"
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:PrismCSND.Views"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
mc:Ignorable="d"
Title="MainView" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition />
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" >
<Button Content="打开页面一" Margin="5" Command="{Binding OpenCommand}" CommandParameter="View1"/>
<Button Content="打开页面二" Margin="5" Command="{Binding OpenCommand}" CommandParameter="View2"/>
<Button Content="打开页面三" Margin="5" Command="{Binding OpenCommand}" CommandParameter="View3"/>
</StackPanel>
<!--使用Prism设置当前模块区域内的名字-->
<ContentControl Grid.Row="1" prism:RegionManager.RegionName="ContentRegion"/>
</Grid>
</Window>
五、MainViewModel类
namespace PrismApp.ViewModels
{
public class MainViewModel : BindableBase
{
/// <summary>
/// 创建一个带有参数的DelegateCommand委托
/// </summary>
///
public DelegateCommand<string> OpenCommand { get; set; }
/// <summary>
/// 实例化一个DelegateCommand去实现注入
/// </summary>
/// <param name="regionManager"></param>
public MainViewModel(IRegionManager regionManager)
{
OpenCommand = new DelegateCommand<string>(Open);
this.regionManager = regionManager;
}
private readonly IRegionManager regionManager;
private void Open(string obj)
{
//通过IRegionManager接口获取全局的可用区域
//往这个区域动态的去设置这个内容
//内容设置当方式是通过依赖注入去设置
regionManager.Regions["ContentRegion"].RequestNavigate(obj);
}
}
}
六、App.xaml.cs类
namespace PrismApp
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : PrismApplication
{
protected override Window CreateShell()
{
return Container.Resolve<MainView>();
}
/// <summary>
/// 通过依赖注入的方式交给框架去处理,让框架能够识别到该视图可以被prism识别了
/// </summary>
/// <param name="containerRegistry"></param>
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterForNavigation<ViewA>();
containerRegistry.RegisterForNavigation<ViewB>();
containerRegistry.RegisterForNavigation<ViewC>();
}
}
}
Abro.:
不正经的kimol君: 支持博主,欢迎回赞哦~
潇洒小白: 写的不错,顶个贴,点个赞,嘿嘿!
不正经的kimol君: TQL,大大大佬
潇洒小白: 反手就是一个赞!