Prism定制Region控件
并不是所有控件都可以被用作Region了吗?我们将Gird块的代码变成这样:
<Grid>
<ContentControl prism:RegionManager.RegionName="ContentRegion" />
<StackPanel prism:RegionManager.RegionName="ContentRegion2" />
</Grid>
似乎看上去一切正常,让我们来启动他。
Oops!!!程序并没有按照我们想象的那样启动,而是抛给了我们一个异常:
Prism.Regions.UpdateRegionsException: 'An exception occurred while trying to create region objects.
Prism在生成一个Region对象的时候报错了,看上去,StackPanel并不支持用作Region。那么有其他的方法让他可以被用作Region吗?~~因为我们很喜欢用StackPanel啊( ﹁ ﹁ ) →(不要管我为什么喜欢,你不,我就偏要)~ 这难不倒Prism,毕竟创建Region对象就是他自己的事情,做分内的事应该没问题的。
你需要为一个将被用作Region的添加RegionAdapter(适配器)。RegionAdapter的作用是为特定的控件创建相应的Region,并将控件与Region进行绑定,然后为Region添加一些行为。一个RegionAdapter需要实现IRegionAdapte接口,如果你需要自定义一个RegionAdapter,可以通过继承RegionAdapterBase类来省去一些工作。
那,为什么ContentControl就可以呢?因为:
The Composite Application Library provides three region adapters out-of-the-box:
- ContentControlRegionAdapter. This adapter adapts controls of type System.Windows.Controls.ContentControl and derived classes.
- SelectorRegionAdapter. This adapter adapts controls derived from the class System.Windows.Controls.Primitives.Selector, such as the System.Windows.Controls.TabControl control.
- ItemsControlRegionAdapter. This adapter adapts controls of type System.Windows.Controls.ItemsControl and derived classes.
因为这三个是内定的(蛤蛤),就是已经帮你实现了RegionAdapter。接下来,我们看看怎么为StackPanel实现RegionAdapter。
- step1 新建一个类
StackPanelRegionAdapter.cs,继承RegionAdapterBase ,这个类已经帮我们实现了IRegionAdapte接口。
using Prism.Regions;
using System.Windows;
using System.Windows.Controls;
namespace Regions.Prism
{
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();
}
}
}
- setp2 在
App.xaml.cs注册绑定
[7.1updated]
protected override void ConfigureRegionAdapterMappings(RegionAdapterMappings regionAdapterMappings)
{
base.ConfigureRegionAdapterMappings(regionAdapterMappings);
regionAdapterMappings.RegisterMapping(typeof(StackPanel), Container.Resolve<StackPanelRegionAdapter>());
}
我们现在可以为StackPanel实现用作Region了。我们再运行刚才抛给我们异常的程序,是不是已经跑起来了呢?
Prism定制Region控件的更多相关文章
- Android自定义View(CustomCalendar-定制日历控件)
转载请标明出处: http://blog.csdn.net/xmxkf/article/details/54020386 本文出自:[openXu的博客] 目录: 1分析 2自定义属性 3onMeas ...
- 动态合并或定制GridView控件Header头某些列
开发时,有时会对GridView控件头做一些字段合并.多行表头,多列合并,明白了其中的原理,实现起来,均能运用自如.下面Insus.NET分享自己的做法. 创建站点,创建aspx网页,拉GridVie ...
- iOS开发技巧 - 使用和定制开关控件(UISwitch)
1. 初始化加载到视图界面 (Swift) import UIKit class ViewController: UIViewController { // 1. create a property ...
- iOS关于定制某个控件四个角是否为圆角
UIView *myView=[[UIView alloc]initWithFrame:CGRectMake(50, 70, 200, 200)]; UIBezierPath * bezierPath ...
- 第六篇、AVplayer定制视频播放控件
1.引用头文件#import AVFoundation 2.自定义AVPlayer(播放的机器) 3.自定义AVPlayerItem(胶片) >> 视频的URL转成AVAsset 4.AV ...
- ASP.NET自定义控件组件开发 第五章 模板控件开发
原文:ASP.NET自定义控件组件开发 第五章 模板控件开发 第五章 模板控件开发 系列文章链接: ASP.NET自定义控件组件开发 第一章 待续 ASP.NET自定义控件组件开发 第一章 第二篇 接 ...
- KRBTabControl(中文)Windows选项卡控件
本文阐述了如何在C#使自定义Windows选项卡控件. Download demo project - 82.4 KB Download source - 252 KB 介绍 本文讨论如何使用.NET ...
- 【番外篇】ASP.NET MVC快速入门之免费jQuery控件库(MVC5+EF6)
目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...
- PropertyGrid控件由浅入深(一):文章大纲
Winform中PropertyGrid控件是一个非常好用的对象属性编辑工具,对于Key-Value形式的数据的处理也是非常的好用. 因为Property控件设计良好,在很小的空间内可以展示很多的内容 ...
随机推荐
- bzoj3534 [Sdoi2014]重建
变形的$Martix-Tree$定理 发现我们要求的是$\prod_{i \in E}{p_{i}} * \prod_{i \notin E}{(1-p_{i})}$ 然后呢? 矩阵树对重边也有效对吧 ...
- BZOJ_3132_上帝造题的七分钟_树状数组
BZOJ_3132_上帝造题的七分钟_树状数组 Description “第一分钟,X说,要有矩阵,于是便有了一个里面写满了0的n×m矩阵. 第二分钟,L说,要能修改,于是便有了将左上角为(a,b), ...
- WPF 列表开启虚拟化的方式
正确开启虚拟化的方式 列表如ListBox,ListView,TreeView,GridView等,开启虚拟化 ScrollViewer设置CanContentScroll=True 直接在模板中,设 ...
- Linux文件系统选择
通过综合使用多种标准文件系统Benchmarks对Ext3, Ext4, Reiserfs, XFS, JFS, Reiser4的性能测试对比,对不同应用选择合适的文件系统给出以下方案,供大家参考.文 ...
- Java 11 新功能来了!
关键时刻,第一时间送达! 目前 Oracle 已经发布了 Java Development Kit 10,下个版本 JDK 11 也即将发布.本文介绍 Java 11 的新功能. 根据Oracle新出 ...
- 教你如何使用Java手写一个基于链表的队列
在上一篇博客[教你如何使用Java手写一个基于数组的队列]中已经介绍了队列,以及Java语言中对队列的实现,对队列不是很了解的可以我上一篇文章.那么,现在就直接进入主题吧. 这篇博客主要讲解的是如何使 ...
- 【Teradata SQL】从中文数字字母混合字符串中只提取数字regexp_substr
目标:从中文数字字母的字符串中只提取数字 sel regexp_substr('mint choc中文11国1','\d+')
- ZZZPHP1.61 代码审计-从SQL注入到Getshell
近期有很多小伙伴在后台留言想看关于代码审计的文章,其实有关审计的文章网上资源是比较多的,但是从代码审计开始到结束的这类文章却少之甚少. 今天要讲解的ZZZPHP1.61这套审计漏洞比较多,SQL注入漏 ...
- 详解线程池execute和submit用法
在使用线程池时,我们都知道线程池有两种提交任务的方式,那么他们有什么区别呢? 1.execute提交的是Runnable类型的任务,而submit提交的是Callable或者Runnable类型的任务 ...
- 浅谈unity中gamma空间和线性空间
转载请标明出处:http://www.cnblogs.com/zblade/ 一.概述 很久没有写文章了,今天写一篇对gamma空间和线性空间的个人理解总结,在查阅和学习了各个资料后,算是一个个人笔记 ...