1 新建wpf应用程序,并引用prism.unity(v8.0)
2 修改App.xaml
<prism:PrismApplication x:Class="ViewInjection.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ViewInjection"
xmlns:prism="http://prismlibrary.com/"
>
<Application.Resources>
</Application.Resources>
</prism:PrismApplication>
3 修改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 ViewInjection
{
/// <summary>
/// App.xaml 的交互逻辑
/// </summary>
public partial class App : PrismApplication
{
protected override Window CreateShell()
{
return Container.Resolve<MainWindow>();
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
}
}
}
4 修建Views目录,添加两个用户控件ViewA,ViewB
<UserControl x:Class="ViewInjection.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:ViewInjection.Views"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<TextBlock Text="ViewA" FontSize="36"/>
</Grid>
</UserControl>
<UserControl x:Class="ViewInjection.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:ViewInjection.Views"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<TextBlock Text="ViewB" FontSize="36"/>
</Grid>
</UserControl>
5 修改MainWindow.xaml
<Window x:Class="ViewInjection.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:ViewInjection"
xmlns:prism="http://prismlibrary.com/"
mc:Ignorable="d"
Title="Shell" Height="450" Width="800">
<DockPanel LastChildFill="True">
<StackPanel DockPanel.Dock="Left">
<Button x:Name="btnActive1" Click="btnActive1_Click" Content="Active View1"/>
<Button x:Name="btnDeactive1" Click="btnDeactive1_Click" Content="Deactive View1"/>
<Button x:Name="btnActive2" Click="btnActive2_Click" Content="Active View2"/>
<Button x:Name="btnDeactive2" Click="btnDeactive2_Click" Content="Deactive View2"/>
</StackPanel>
<ContentControl prism:RegionManager.RegionName="ContentRegion" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</DockPanel>
</Window>
6 修改MainWindow.xaml.cs
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;
using Unity;
using ViewInjection.Views;
namespace ViewInjection
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
IUnityContainer _container;
IRegionManager _manager;
IRegion _region;
ViewA _viewA;
ViewB _viewB;
public MainWindow(IUnityContainer container,IRegionManager manager)
{
InitializeComponent();
this._container = container;
this._manager = manager;
this.Loaded += MainWindow_Loaded;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
_viewA = _container.Resolve<ViewA>();
_viewB = _container.Resolve<ViewB>();
_region = _manager.Regions["ContentRegion"];
_region.Add(_viewA);
_region.Add(_viewB);
}
private void btnActive1_Click(object sender, RoutedEventArgs e)
{
_region.Activate(_viewA);
}
private void btnDeactive1_Click(object sender, RoutedEventArgs e)
{
_region.Deactivate(_viewA);
}
private void btnActive2_Click(object sender, RoutedEventArgs e)
{
_region.Activate(_viewB);
}
private void btnDeactive2_Click(object sender, RoutedEventArgs e)
{
_region.Deactivate(_viewB);
}
}
}