【问题标题】:How to "Navigate" with a Prism Custom adapter?如何使用 Prism 自定义适配器“导航”?
【发布时间】:2022-01-06 05:13:23
【问题描述】:

我有一个 Prism 自定义区域适配器,用于在 DevExpress“DocumentGroup”的不同选项卡中显示每个视图。

为了做到这一点,我有以下 RegionAdapter:

public class DocumentGroupRegionAdapter : RegionAdapterBase<DocumentGroup>
{
    public DocumentGroupRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory)
        : base(regionBehaviorFactory)
    {
    }
    protected override void Adapt(IRegion region, DocumentGroup regionTarget)
    {
        region.Views.CollectionChanged += (sender, args) =>
        {
            if (args.Action == NotifyCollectionChangedAction.Add)
            {
                foreach (FrameworkElement element in args.NewItems)
                {
                    DocumentPanel documentPanel = new DocumentPanel {Content = element, DataContext = element.DataContext};
                    regionTarget.Items.Add(documentPanel);
                }
            }
        };
    }
    protected override IRegion CreateRegion()
    {
        return new AllActiveRegion();
    }
}

AllActiveRegion 为:

public class AllActiveRegion : Region
{
    public override IViewsCollection ActiveViews
    {
        get { return Views; }
    }

    public override void Deactivate(object view)
    {
        throw new InvalidOperationException(Resources.DeactiveNotPossibleException);
    }
}

我们为这个区域注册了几个视图:

_regionManager.RegisterViewWithRegion(Regions.MainSections, typeof(Views.Layout.RootView));
_regionManager.RegisterViewWithRegion(Regions.MainSections, typeof(Views.Configure.RootView));
_regionManager.RegisterViewWithRegion(Regions.MainSections, typeof(Views.Dashboard.RootView));

到目前为止它运行良好,但现在,在某些选项上,我们需要激活其中一个选项卡。这将通过调用item.IsActive = true 来完成。

如何指定我也想导航的项目?

我应该覆盖什么来设置这个活动项目?

【问题讨论】:

    标签: wpf xaml devexpress prism


    【解决方案1】:

    对于感兴趣的人,我不得不做几件事来解决这个问题:

    1. 切换到SingleActiveRegion 而不是AllActiveRegion。 AllActiveRegion 不跟踪选定(活动)项目,基本上是 region.Views = region.ActiveViews。因此,您无法注册 ActiveViews 更改。
    2. 收听region.ActiveViews.CollectionChanged 事件。当它触发时,我必须激活 DevExpress 组件
    3. 在用户单击选项卡时监听 DevExpress 组件。这是必需的,因为否则 Prism 可能会认为控件已经处于活动状态,而不再处于活动状态。

    代码如下:

    public class DocumentGroupRegionAdapter : RegionAdapterBase<DocumentGroup>
    {
        public DocumentGroupRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory)
            : base(regionBehaviorFactory)
        {
        }
        protected override void Adapt(IRegion region, DocumentGroup regionTarget)
        {
            region.Views.CollectionChanged += (_, args) =>
            {
                if (args.Action == NotifyCollectionChangedAction.Add)
                {
                    foreach (FrameworkElement element in args.NewItems)
                    {
                        DocumentPanel documentPanel = new() {Content = element, DataContext = element.DataContext};
                        
    
                        regionTarget.Items.Add(documentPanel);
                    }
                }
            };
    
            region.ActiveViews.CollectionChanged += (_, args) =>
            {
                if (args.Action == NotifyCollectionChangedAction.Add)
                {
                    foreach (FrameworkElement element in args.NewItems)
                    {
                        DocumentPanel existingItem = regionTarget.Items.Cast<DocumentPanel>().FirstOrDefault(i => i.Content == element);
                        if (existingItem != null)
                        {
                            existingItem.IsActive = true;
                        }
                    }
                }
            };
    
            regionTarget.SelectedItemChanged += ((_, args) =>
            {
                region.Activate(((DocumentPanel)args.Item).Content);
            });
        }
        
        protected override IRegion CreateRegion()
        {
            return new SingleActiveRegion();
        }
    }
    

    现在我已经看到了 Prism 的源代码,我认为编写一个自定义区域也许也可以完成这项工作,我不确定什么时候调用 Activate()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-12
      • 2016-01-01
      • 1970-01-01
      相关资源
      最近更新 更多