如何在 Prism 中使用面板作为区域?

发布于 2024-08-14 07:22:38 字数 661 浏览 2 评论 0原文

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

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

选择器区域适配器。此适配器适应从 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}">

我们如何实现此目的?

The prism documentation states that there are three region adapters available:

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.

Unfortunately, Panel does not fall into any of those categories, and I want to be able to write this in my .xaml.cs:

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

How can we accomplish this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

℡寂寞咖啡 2024-08-21 07:22:38

这个问题的答案可以在这个非常好的描述性博客文章

不过,我也希望将答案存储在 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;
    }
}

The answer to this can be found in this very nice, descriptive blog post.

However, I want the answer stored on StackOverflow as well :) It took a bit of searching to get this from Google. Here is my code that works with a basic Panel.

Step 1 - create a new region adapter

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();
    }
}

Step 2 - update your bootstrapper

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文