【问题标题】:How can I use a Panel as a Region in Prism?如何将面板用作 Prism 中的区域?
【发布时间】:2009-12-09 22:14:19
【问题描述】:

prism 文档指出有三个可用的区域适配器:

ContentControlRegionAdapter。此适配器适应 System.Windows.Controls.ContentControl 类型的控件和派生类。

SelectorRegionAdapter。此适配器适应从类System.Windows.Controls.Primitives.Selector 派生的控件,例如System.Windows.Controls.TabControl 控件。

ItemsControlRegionAdapter。此适配器适应 System.Windows.Controls.ItemsControl 类型的控件和派生类。

不幸的是,Panel 不属于这些类别中的任何一个,我希望能够在我的.xaml.cs 中写下这个:

<Canvas cal:RegionManager.RegionName="{x:Static local:RegionNames.MainCanvas}">

我们怎样才能做到这一点?

【问题讨论】:

  • 这里的问题是画布没有强加任何布局。除非您使您的 regionadapter 更智能地放置在该区域注册的元素(这也是不支持 Panel 的原因),否则您将遇到问题。我很好奇......你有什么应用程序?
  • 我正在更新一个现有的代码库,该代码库具有预期位于 Canvas 内的浮动控件容器(如工具栏)。他们处理自己的“标题栏”区域并在拖动时更新其画布位置。显然,这可以通过其他方式实现,但与往常一样,时间至关重要,这似乎是我可以将现有结构转换为与 Prism 一起使用的最快方式。
  • 让控件更新其位置似乎有点不合逻辑......有点“走出你的盒子”。 +1 能够转换现有的代码库。
  • 我完全同意。如果有时间,它们可能应该移到 Windows(甚至可能是 Popups)。我只是看不到现在可用的时间 :)

标签: wpf prism panel


【解决方案1】:

这个问题的答案可以在这个非常好的descriptive blog post中找到。

但是,我也希望将答案存储在 StackOverflow 上 :) 从 Google 获得此信息需要进行一些搜索。这是我的代码,适用于基本面板。

第 1 步 - 创建新的区域适配器

public class PanelHostRegionAdapter : RegionAdapterBase<Panel>
{
    public PanelHostRegionAdapter(IRegionBehaviorFactory behaviorFactory)
        : base(behaviorFactory)
    {
    }

    protected override void Adapt(IRegion region, Panel regionTarget)
    {
        region.Views.CollectionChanged += (s, e) =>
               {
                   if (e.Action == NotifyCollectionChangedAction.Add)
                   {
                       foreach (FrameworkElement element in e.NewItems)
                       {
                           regionTarget.Children.Add(element);
                       }
                   }
                   else if (e.Action == NotifyCollectionChangedAction.Remove)
                   {
                       foreach (FrameworkElement CurrentElement in e.OldItems)
                           regionTarget.Children.Remove(CurrentElement);
                   }
               };
    }

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

第 2 步 - 更新您的引导程序

public class Bootstrapper : UnityBootstrapper
{
    protected override DependencyObject CreateShell()
    {
       ...
    }

    protected override IModuleCatalog GetModuleCatalog()
    {
       ...
    }

    protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
    {
        RegionAdapterMappings Mappings = base.ConfigureRegionAdapterMappings();
        Mappings.RegisterMapping(typeof(Panel), Container.Resolve<PanelHostRegionAdapter>());
        return Mappings;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-29
    • 1970-01-01
    • 2013-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-25
    相关资源
    最近更新 更多