1 按照《prism项目搭建》搭建项目
2 在Views文件夹里面创建用户控件ViewA,ViewB
<UserControl x:Class="Prism.ActiveDeactive.Views.ViewA"
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:Prism.ActiveDeactive.Views"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<TextBlock Text="ViewA" HorizontalAlignment="Center" />
</Grid>
</UserControl>
<UserControl x:Class="Prism.ActiveDeactive.Views.ViewB"
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:Prism.ActiveDeactive.Views"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<TextBlock Text="ViewB" HorizontalAlignment="Center" />
</Grid>
</UserControl>
3 在MainWindow里面添加四个按钮
<Window x:Class="Prism.ActiveDeactive.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:local="clr-namespace:Prism.ActiveDeactive"
xmlns:prism="http://prismlibrary.com/"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded">
<DockPanel LastChildFill="True">
<StackPanel>
<Button x:Name="ActiveA" Content="Activate ViewA" Click="ActiveA_Click"/>
<Button x:Name="DeactiveA" Content="Deactivate ViewA" Click="DeactiveA_Click"/>
<Button x:Name="ActiveB" Content="Activate ViewB" Click="ActiveB_Click"/>
<Button x:Name="DeactiveB" Content="Deactivate ViewB" Click="DeactiveB_Click"/>
</StackPanel>
<ContentControl prism:RegionManager.RegionName="ContentRegion" />
</DockPanel>
</Window>
5 在MainWindow里面添加四个局部变量
IRegionManager _regionManager;
IContainerExtension _container;
ViewA viewA;
ViewB viewB;
IRegion region;
6 通过构造函数注入regionManager和container
public MainWindow(IRegionManager regionManager,IContainerExtension container)
{
InitializeComponent();
_regionManager = regionManager;
_container = container;
}
7 在WIndow的加载函数中通过容易得到ViewA ViewB的实例并添加的region里面
private void Window_Loaded(object sender, RoutedEventArgs e)
{
region = _regionManager.Regions["ContentRegion"];
viewA = _container.Resolve<ViewA>();
viewB = _container.Resolve<ViewB>();
region.Add(viewA);
region.Add(viewB);
}
8 按钮事件实现如下
private void ActiveA_Click(object sender, RoutedEventArgs e)
{
region.Activate(viewA);
}
private void DeactiveA_Click(object sender, RoutedEventArgs e)
{
region.Deactivate(viewA);
}
private void ActiveB_Click(object sender, RoutedEventArgs e)
{
region.Activate(viewB);
}
private void DeactiveB_Click(object sender, RoutedEventArgs e)
{
region.Deactivate(viewB);
}
9 全部代码如下
using Prism.Ioc;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Navigation;
using System.Windows.Shapes;
namespace Prism.ActiveDeactive.Views
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
IRegionManager _regionManager;
IContainerExtension _container;
ViewA viewA;
ViewB viewB;
IRegion region;
public MainWindow(IRegionManager regionManager,IContainerExtension container)
{
InitializeComponent();
_regionManager = regionManager;
_container = container;
}
private void ActiveA_Click(object sender, RoutedEventArgs e)
{
region.Activate(viewA);
}
private void DeactiveA_Click(object sender, RoutedEventArgs e)
{
region.Deactivate(viewA);
}
private void ActiveB_Click(object sender, RoutedEventArgs e)
{
region.Activate(viewB);
}
private void DeactiveB_Click(object sender, RoutedEventArgs e)
{
region.Deactivate(viewB);
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
region = _regionManager.Regions["ContentRegion"];
viewA = _container.Resolve<ViewA>();
viewB = _container.Resolve<ViewB>();
region.Add(viewA);
region.Add(viewB);
}
}
}