zoukankan      html  css  js  c++  java
  • 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();
            }
        }
    }
    
    
    • setp2App.xaml.cs注册绑定
      [7.1updated]
            protected override void ConfigureRegionAdapterMappings(RegionAdapterMappings regionAdapterMappings)
            {
                base.ConfigureRegionAdapterMappings(regionAdapterMappings);
                regionAdapterMappings.RegisterMapping(typeof(StackPanel), Container.Resolve<StackPanelRegionAdapter>());
            }
    

    我们现在可以为StackPanel实现用作Region了。我们再运行刚才抛给我们异常的程序,是不是已经跑起来了呢?

  • 相关阅读:
    JAVA实验3 类与对象
    多种排序功能的实现
    (数据结构)HashTable的实现
    对string型变量的频率统计(文章单词检索)
    对int型变量的频率统计
    二叉搜索树中序迭代器的实现
    二叉搜索树的实现
    Prim最小生成树
    Kruscal最小生成树
    算法分析实践大作业
  • 原文地址:https://www.cnblogs.com/hicolin/p/10406005.html
Copyright © 2011-2022 走看看