03、CustomRegions
1、将App.xaml中的StartupUri="MainWindow.xaml"删除。
2、使用NuGet安装Prism.Wpf、Prism.Core、Prism.Unity。
3、添加类“Bootstrapper”,编辑如下:
1 using Microsoft.Practices.Unity; 2 using Prism.Regions; 3 using Prism.Unity; 4 using System.Windows; 5 using System.Windows.Controls; 6 using CustomRegions.Prism; 7 using CustomRegions.Views; 8 9 namespace CustomRegions 10 { 11 public class Bootstrapper:UnityBootstrapper 12 { 13 protected override DependencyObject CreateShell() 14 { 15 return Container.Resolve<MainWindow>(); 16 } 17 18 protected override void InitializeShell() 19 { 20 Application.Current.MainWindow.Show(); 21 } 22 23 protected override RegionAdapterMappings ConfigureRegionAdapterMappings() 24 { 25 RegionAdapterMappings mappings = base.ConfigureRegionAdapterMappings(); 26 mappings.RegisterMapping(typeof(StackPanel), Container.Resolve<StackPanelRegionAdapter>()); 27 return mappings; 28 } 29 } 30 }
4、创建文件夹Views,将MainWindow.xaml移动到此文件夹中。添加Prism文件夹,添加“StackPanelRegionAdapter.cs”类。

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Windows; 7 using System.Windows.Controls; 8 using Prism.Regions; 9 10 namespace CustomRegions.Prism 11 { 12 public class StackPanelRegionAdapter:RegionAdapterBase<StackPanel> 13 { 14 public StackPanelRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory) 15 : base(regionBehaviorFactory) 16 { 17 18 } 19 20 protected override void Adapt(IRegion region, StackPanel regionTarget) 21 { 22 region.Views.CollectionChanged += (s, e) => 23 { 24 if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add) 25 { 26 foreach (FrameworkElement element in e.NewItems) 27 { 28 regionTarget.Children.Add(element); 29 } 30 } 31 32 //handle remove 33 }; 34 } 35 36 protected override IRegion CreateRegion() 37 { 38 return new AllActiveRegion(); 39 } 40 } 41 }
1 <Window x:Class="CustomRegions.Views.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:CustomRegions.Views" 7 xmlns:prism="http://prismlibrary.com/" 8 mc:Ignorable="d" 9 Title="Shell" Height="450" Width="800"> 10 <Grid> 11 <ContentControl prism:RegionManager.RegionName="ContentRegion"></ContentControl> 12 </Grid> 13 </Window>
风中代表自由、寻觅代表不断前进~!

浙公网安备 33010602011771号