c# - 具有两个键和一个对象的最佳 C# 集合是什么?

标签 c# wpf collections prism

我有一个 MenuManager 类,每个模块都可以向其中添加一个键和要加载到主要内容中的元素:

private Dictionary<string,object> _mainContentItems = new Dictionary<string,object>();
public Dictionary<string,object> MainContentItems
{
    get { return _mainContentItems; }
    set { _mainContentItems = value; }
}

所以客户模块像这样注册它的 View :

layoutManager.MainContentViews.Add("customer-help", this.container.Resolve<HelpView>());
layoutManager.MainContentViews.Add("customer-main", this.container.Resolve<MainView>());

所以稍后我会说一个特定的观点:

layoutManager.ShowMainContentView("customer-help");

为了获得默认 View (注册的第一个 View ),我说:

layoutManager.ShowDefaultView("customer");

这很好用。

但是,我想用分隔模块名称和 View 名称的连字符消除“代码味道”,所以我想用这个命令注册:

layoutManager.MainContentViews.Add("customer","help", this.container.Resolve<HelpView>());

但是替换我当前的词典的最佳方法是什么,例如想到的是这些:

  • Dictionary<string, string, object> (doesn't exist)
  • Dictionary<KeyValuePair<string,string>, object>
  • Dictionary<CUSTOM_STRUCT, object>

新的集合需要能够做到这一点:

  • 获取包含模块和 View 键的 View (例如“客户”、“帮助”返回 1 个 View )
  • 通过模块键获取所有 View 的集合(例如“客户”返回 5 个 View )

最佳答案

严格满足您的标准,使用 Dictionary<string, Dictionary<string, object>> ;

var dict = new Dictionary<string, Dictionary<string, object>>();
...
object view = dict["customer"]["help"];
Dictionary<string, object>.ValueCollection views = dict["customer"].Values;

关于c# - 具有两个键和一个对象的最佳 C# 集合是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1171838/

相关文章:

WPF - 非常基本的 ListBox.ItemTemplate 问题

C# XAML - For 循环只显示最后一次迭代,中间没有任何内容

collections - 主干_.each collection.model为空

c# - 避免在字符串的开头和结尾输入双引号

c# - 如何在 .Net 上使用 Saxon 计算 Xpath 表达式

c# - Entity Framework - 1 到 ... 太多到 1 选择

Java Immutable Collection,也修复引用

c# - 继承调用错误的 Equals() 方法

wpf - 模态和非模态行为的窗口

java - 从 arrayList 到 long[]