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控件设计良好,在很小的空间内可以展示很多的内容 ...
随机推荐
- 【强连通分量】Bzoj1051 HAOI2006 受欢迎的牛
Description 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这种关系是具有传递性的,如果A认为B受欢迎,B认为C受欢迎,那么牛A也认 ...
- GopherChina第一天小结
GopherChina第一天小结 今天参加了Asta举办的第五届GopherChina,第一天参加完,颇有感受,晚上回来趁着还有记忆,来做一下记录. 写在前面 一早从9点开始,一天下来一共八个主题,各 ...
- asp.net core系列 59 Ocelot 构建基础项目示例
一.入门概述 从这篇开始探讨Ocelot,Ocelot是一个.NET API网关,仅适用于.NET Core,用于.NET面向微服务/服务的架构中.当客户端(web站点.ios. app 等)访问we ...
- FreeSql 过滤器使用介绍
FreeSql.Repository 实现了过滤器,它不仅是查询时过滤,连删除/修改/插入时都会进行验证,避免数据安全问题. 过滤器 目前过滤器依附在仓储层实现,每个仓储实例都有 IDataFilte ...
- PoolEntry 参数讲解
public abstract class PoolEntry<T, C> { private final String id; private final T route; //路由 p ...
- JSON Web Token入门教程
目录 一.跨域认证的问题 二.JWT的原理 三.JWT的数据结构 3.1Header 3.2Payload 3.3 Signature 3.4 Base64URL 四.JWT的使用方式 五.JWT的几 ...
- 企业自主可控免费开源ERP:Odoo采购管理解决方案
供应商基础资料 1. 所有的供应商基础资料,Odoo开账启用时,期初的客户数据如果大于200条,可以批量导入: 2. 点“采购/采购/供应商”菜单可以查看.编辑修改.搜索所有的供应商基础资料: 3. ...
- cesium 之图层管理器篇(附源码下载)
前言 cesium 官网的api文档介绍地址cesium官网api,里面详细的介绍 cesium 各个类的介绍,还有就是在线例子:cesium 官网在线例子,这个也是学习 cesium 的好素材. 内 ...
- python线程(二)代码部分
使用threading创建线程: from threading import Thread def work(name): print(f"我是线程{name}") if __na ...
- C语言中#undef作用
#undef 作用:取消定义的宏 指令格式:#undef 标识符 1 #include <stdio.h> 2 3 #define MAX 5 4 5 int main() { 6 pri ...