1、NuGet安装Prism.DryIoc,如下图。项目使用.NET8。
2、修改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/">
<Application.Resources>
</Application.Resources>
</prism:PrismApplication>
3、修改App.xaml.cs代码如下。
using System.Configuration;
using System.Data;
using System.Windows;
namespace WpfApp1
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : PrismApplication
{
protected override Window CreateShell()
{
return Container.Resolve<MainWindow>();
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
}
}
}
4、修改MainWindow.xaml文件,在文件中添加xmlns:prism="http://prismlibrary.com/"引用,添加<ContentControl prism:RegionManager.RegionName="ContentRegion"/>,定义一个名称为ContentRegion的区域。代码如下。
<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"
xmlns:prism="http://prismlibrary.com/"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<StackPanel>
<ContentControl prism:RegionManager.RegionName="ContentRegion"/>
</StackPanel>
</Window>
5、Prism中不是所有的WPF控件都可以定义区域,比如当我们想使用StackPanel控件定义一个区域的话,就需要进行一下操作。
5.1、首先定义一个类StackPanelRegionAdapter.cs,代码如下。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows;
namespace WpfApp1
{
public class StackPanelRegionAdapter : RegionAdapterBase<StackPanel>
{
public StackPanelRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory)
: base(regionBehaviorFactory)
{
}
protected override void Adapt(IRegion region, StackPanel regionTarget)
{
region.Views.CollectionChanged += (s, e) =>
{
if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
{
foreach (FrameworkElement element in e.NewItems)
{
regionTarget.Children.Add(element);
}
}
//handle remove
};
}
protected override IRegion CreateRegion()
{
return new AllActiveRegion();
}
}
}
5.2、在App.xaml.cs对StackPanelRegionAdapter类进行注册,代码如下。
using System.Configuration;
using System.Data;
using System.Windows;
using System.Windows.Controls;
namespace WpfApp1
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : PrismApplication
{
protected override Window CreateShell()
{
return Container.Resolve<MainWindow>();
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
}
protected override void ConfigureRegionAdapterMappings(RegionAdapterMappings regionAdapterMappings)
{
base.ConfigureRegionAdapterMappings(regionAdapterMappings);
regionAdapterMappings.RegisterMapping(typeof(StackPanel), Container.Resolve<StackPanelRegionAdapter>());
}
}
}
5.3、MainWindow.xaml引入与上面的方法一样,代码如下。下面的代码中定义了一个名为ContentRegion的区域。
<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"
xmlns:prism="http://prismlibrary.com/"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<StackPanel>
<StackPanel prism:RegionManager.RegionName="ContentRegion" />
</StackPanel>
</Window>
6、将用户控件添加到区域当中。
6.1.1、添加一个用户控件UCView1.xaml,代码如下。
<UserControl x:Class="WpfApp1.UCView1"
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:WpfApp1"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<TextBlock Text="UCView1" FontSize="36"/>
</Grid>
</UserControl>
6.1.2、MainWindow.xaml.cs中修改代码如下。
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.Navigation;
using System.Windows.Shapes;
namespace WpfApp1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow(IRegionManager regionManager)
{
InitializeComponent();
// 将用户控件UCView1注册到ContentRegion区域
regionManager.RegisterViewWithRegion("ContentRegion", typeof(UCView1));
}
}
}
6.1.3、运行效果如下图所示。
7、通过按钮将用户控件添加到区域。
7,1、在 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"
xmlns:prism="http://prismlibrary.com/"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<StackPanel>
<Button Content="添加UCView" Click="Button_Click" HorizontalAlignment="Center" Width="200" Height="30" Margin="0,5,0,0"/>
<StackPanel prism:RegionManager.RegionName="ContentRegion" />
</StackPanel>
</Window>
7.2、MainWindow.xaml.cs代码修改如下。
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.Navigation;
using System.Windows.Shapes;
namespace WpfApp1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
IContainerExtension _container;
IRegionManager _regionManager;
public MainWindow(IContainerExtension container, IRegionManager regionManager)
{
InitializeComponent();
_container = container;
_regionManager = regionManager;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var view = _container.Resolve<UCView1>();
IRegion region = _regionManager.Regions["ContentRegion"];
region.Add(view);
}
}
}
7.3、运行效果如下图。
8、 区域中激活用户控件和停用用户控件。注意这里使用的区域是ContentControl而不是StackPanel。
8.1、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"
xmlns:prism="http://prismlibrary.com/"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<StackPanel>
<Button Content="添加UCView" Click="Button_Click" HorizontalAlignment="Center" Width="200" Height="30" Margin="0,5,0,0" Tag="Active"/>
<Button Content="停用UCView2" Click="Button_Click" HorizontalAlignment="Center" Width="200" Height="30" Margin="0,5,0,0" Tag="Deactive"/>
<ContentControl prism:RegionManager.RegionName="ContentRegion" />
</StackPanel>
</Window>
8.2、MainWindow.xaml.cs代码如下
using Prism.Navigation.Regions;
using System.Windows;
using Prism.Ioc;
using System.Windows.Controls;
namespace WpfApp1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
IContainerExtension _container;
IRegionManager _regionManager;
IRegion _region;
UCView1 _view1;
public MainWindow(IContainerExtension container, IRegionManager regionManager)
{
InitializeComponent();
_container = container;
_regionManager = regionManager;
this.Loaded += MainWindow_Loaded;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
_view1 = _container.Resolve<UCView1>();
_region = _regionManager.Regions["ContentRegion"];
_region.Add(_view1);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Button btn = sender as Button;
if (btn.Tag.ToString() == "Deactive")
{
_region.Deactivate(_view1);
}
else
{
_region.Activate(_view1);
}
}
}
}
8.3、运行效果如下图。
标签:regionManager,Windows,xaml,System,Regions,Prism,区域,using,MainWindow From: https://blog.csdn.net/xingchengaiwei/article/details/142380846