脚本宝典收集整理的这篇文章主要介绍了03Prism WPF 入门实战 - Region,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
源码及PPT地址:https://github.com/JusterZhu/wemail
视频地址:https://www.bilibili.com/video/BV1KQ4y1C7tg?sharesource=copyweb
在实战项目当中,需根据业务需求来划分Region。
RegionManager主要实现维护区域集合、提供对区域的访问、合成视图、区域导航、定义区域。
区域定义方式有两种:
RegionAdapter(区域适配)主要作用为特定的控件创建相应的Region,并将控件与Region进行绑定,然后为Region添加一些行为。
因为并不是所有的控件都可以作为Region的,需要为需要定义为Region的控件添加RegionAdapter。一个RegionAdapter需要实现IRegionAdapter接口,如果你需要自定义一个RegionAdapter,可以通过继承RegionAdapterBase类来省去一些工作。
Prism为开发者提供了几个默认RegionAdapter:
Region和RegionManager实战应用。
(1)定义Region及选择好容器控件
<TabControl prism:RegionManager.RegionName="TabRegion" />
(2)ViewModel注册视图到TabRegion当中 public class MainWindowViewModel : BindableBase { //Region管理对象 private IRegionManager _regionManager; private string _title = "Prism Application";
public string Title
{
get { return _title; }
set { SetProperty(ref _title, value); }
}
public MainWindowViewModel(IRegionManager regionManager)
{
//Prism框架内依赖注入的RegionManager
_regionManager = regionManager;
//在ContentRegion中注册视图TempView(TabItem1)
_regionManager.RegisterViewWithRegion("TabRegion", typeof(TempView));
//TabItem2
_regionManager.RegisterViewWithRegion("TabRegion", typeof(Temp2View));
//TabItem3
_regionManager.RegisterViewWithRegion("WorkRegion", typeof(Temp3View));
//对视图的访问、操作
//var contentRegion = _regionManager.Regions["ContentRegion"];
//contentRegion.Context
//contentRegion.Remove()
//contentRegion.Activate()
//foreach (var item in contentRegion.ActiveViews)
//{
// contentRegion.Activate(item);
//}
}
}
RegionAdapter实战应用。
如果在实际开发工作当中遇到了特殊场景需要而Prism并没有设置对应的RegionAdapter。这时候可以通过继承实现RegionAdapterBase内置对象来扩展一个新的RegionAdapter。
(1)实现一个新的RegionAdapter
/// <summary>
/// custom region adapter.
/// </summary>
public class StackPanelRegionAdapter : RegionAdapterBase<StackPanel>
{
public StackPanelRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory) : base(regionBehaviorFactory)
{
}
protected override void Adapt(IRegion region, StackPanel regionTarget)
{
//该事件监听往StackPanel添加view时的操作
region.Views.CollectionChanged += (sender, e) =>
{
//监听到增加操作时则往StackPanel添加Children,枚举出来的操作在后面一段代码中体现
if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
{
regionTarget.Children.Clear();
foreach (var item in e.NewItems)
{
regionTarget.Children.Add(item as UIElement);
}
}
};
}
protected override IRegion CreateRegion()
{
return new Region();
}
}
// Summary:
// Describes the action that caused a System.Collections.Specialized.INotifyCollectionChanged.CollectionChanged
// event.
public enum NotifyCollectionChangedAction
{
//
// Summary:
// An item was added to the collection.
Add,
//
// Summary:
// An item was removed from the collection.
Remove,
//
// Summary:
// An item was replaced in the collection.
Replace,
//
// Summary:
// An item was moved within the collection.
Move,
//
// Summary:
// The contents of the collection changed dramatically.
Reset
}
(2)在App.cs文件中注册新的RegionAdapter
public partial class App
{
/// <summary>
/// 应用程序启动时创建Shell
/// </summary>
/// <returns></returns>
protected override Window CreateShell()
{
return Container.Resolve<MainWindow>();
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
}
/// <summary>
/// 配置区域适配
/// </summary>
/// <param name="regionAdapterMappings"></param>
protected override void ConfigureRegionAdapterMappings(RegionAdapterMappings regionAdapterMappings)
{
base.ConfigureRegionAdapterMappings(regionAdapterMappings);
//添加自定义区域适配对象,会自动适配标记上prism:RegionManager.RegionName的容器控件为Region
regionAdapterMappings.RegisterMapping(typeof(StackPanel), Container.Resolve<StackPanelRegionAdapter>());
}
}
(3)在xaml中使用
<StackPanel prism:RegionManager.RegionName="StackPanelRegion"></StackPanel>
以上是脚本宝典为你收集整理的03Prism WPF 入门实战 - Region全部内容,希望文章能够帮你解决03Prism WPF 入门实战 - Region所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。