微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

wpf prism v7.2 region的使用

1 新建wpf程序

2 通过nuget添加prism.unity的引用注意要选V7.2版本,自动添加其他依赖的引用

3 在项目中添加目录Prism,并新建类StackPanelregionadapter

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;

namespace OldRegions.Prism
{
    public class StackPanelregionadapter : regionadapterBase<StackPanel>
    {
        public StackPanelregionadapter(IRegionBehaviorFactory regionBehiaviorFactory)
            : base(regionBehiaviorFactory)
        {

        }
        protected override void Adapt(IRegion region, StackPanel regionTarget)
        {
            region.Views.CollectionChanged += (s,e)=>
            {
                if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
                {
                    foreach (FrameworkElement item in e.NewItems)
                    {
                        regionTarget.Children.Add(item);
                    }
                }
            };
        }


        protected override IRegion CreateRegion()
        {
            return new AllActiveRegion();
        }
    }
}

4 在项目中新建Bootstrapper,使其继承与UnityBootstrapper

using OldRegions.Prism;
using Prism.Regions;
using Prism.Unity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using Unity;

namespace OldRegions
{
    public class Bootstrapper:UnityBootstrapper
    {
        protected override DependencyObject CreateShell()
        {
            return Container.Resolve<MainWindow>();
        }

        protected override void InitializeShell()
        {
            Application.Current.MainWindow.Show();
        }

        protected override regionadapterMappings ConfigureregionadapterMappings()
        {
            regionadapterMappings mappings = base.ConfigureregionadapterMappings();
            mappings.RegisterMapping(typeof(StackPanel), Container.Resolve<StackPanelregionadapter>());
            return mappings;
        }
    }
}

5 删除App.xaml中的StartupUri属性,在App.xaml.cs中重写OnStartup方法

 protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            var boot = new Bootstrapper();
            boot.Run();
        }

6 在MainWindow.xaml中添加

xmlns:prism="http://prismlibrary.com/"

并在Grid里面添加下面的代码

 <StackPanel prism:RegionManager.RegionName="ContentRegion"/>

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐