c# - 自定义区域适配器 (PRISM)

标签 c# wpf mvvm prism regionadapter

我已经为工具栏实现了一个自定义区域适配器,如此链接 http://compositewpf.codeplex.com/discussions/250892 中所述.我收到此错误:“ToolBarRegionAdapter”不包含采用 0 个参数的构造函数。
这是我的代码:

public class ToolBarRegionAdapter : RegionAdapterBase<ToolBar>
{
    public ToolBarRegionAdapter(IRegionBehaviorFactory behaviorFactory)
        : base(behaviorFactory)
    {
    }

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

    protected override void Adapt(IRegion region, ToolBar regionTarget)
    {
        region.Views.CollectionChanged += (sender, e) =>
        {
            switch (e.Action)
            {
                case NotifyCollectionChangedAction.Add:
                    foreach (FrameworkElement element in e.NewItems)
                    {
                        regionTarget.Items.Add(element);
                    }
                    break;

                case NotifyCollectionChangedAction.Remove:
                    foreach (UIElement elementLoopVariable in e.OldItems)
                    {
                        var element = elementLoopVariable;
                        if (regionTarget.Items.Contains(element))
                        {
                            regionTarget.Items.Remove(element);
                        }
                    }
                    break;
            }
        };
    }
}

我已经在我的 Bootstrap 中覆盖了 ConfigureRegionAdapterMappings() 方法(我的 Bootstrap 继承自 MefBootstrapper)。这里的代码:
protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
{
    RegionAdapterMappings regionAdapterMappings = base.ConfigureRegionAdapterMappings();
    regionAdapterMappings.RegisterMapping(typeof(ToolBar), new ToolBarRegionAdapter());
    return regionAdapterMappings;
}

编译时出现此错误:“ToolBarRegionAdapter”不包含采用 0 个参数的构造函数。这实际上是真的,构造函数采用 IRegionBehaviorFactory 但我的代码中没有该对象。但是在我看到的示例中,区域适配器是在没有任何参数的情况下实例化的。
知道为什么吗?谢谢!

最佳答案

虽然始终首选构造函数注入(inject),但当不可能时,如您的情况,请使用服务定位器...

ServiceLocator.Current.GetInstance<IRegionBehaviorFactory >()

...如您提供的链接所示,顺便说一句...

关于c# - 自定义区域适配器 (PRISM),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40578399/

相关文章:

c# - MonoDevelop、linux、Ubuntu 18.04 Mint、ConsoleApplication 控制台不弹出

c# - MVVM动态UserControl创建

wpf - 放大WPF的方法

viewbox 中的 C# wpf 控件,但无法获得实际大小

c# - XML 命名空间 'ViewModelLocator' 中的未知类型 'clr-namespace:namespaceblabla;assembly=blabla'

javascript - 使用 jQuery 从 Html 表获取值并映射到 C# 列表

C# 性能 - 指向热循环中跨度的指针

c# - datagridview 单元格点击事件

WPF ComboBox SelectedItem

mvvm - 接口(interface)绑定(bind)到没有 MVVM 包的 ViewModel