03、CustomRegions
生活随笔
收集整理的這篇文章主要介紹了
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>
?
轉載于:https://www.cnblogs.com/bjxingch/articles/9546855.html
總結
以上是生活随笔為你收集整理的03、CustomRegions的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Node.js 连接 MySQL 插入
- 下一篇: 直播系统开发如何才可以抓住用户眼球