# 20240919. 新增 BindableDictionary

虽然笔者目前还不知道 BindableDictionary 能用在什么使用场景下，但是还是应童鞋的要求实现了 BindableDictionary。

基本使用如下:

``` csharp
using System.Linq;
using UnityEngine;

namespace QFramework.Example
{
    public class BindableDictionaryExample : MonoBehaviour
    {
        private BindableDictionary<string, string> mDictionary = new BindableDictionary<string, string>();

        private void Start()
        {
            mDictionary.OnCountChanged.Register(count =>
            {
                print("count:" + count);
            }).UnRegisterWhenGameObjectDestroyed(gameObject);

            mDictionary.OnAdd.Register((key, value) =>
            {
                print("add:" + key + "," + value);
            }).UnRegisterWhenGameObjectDestroyed(gameObject);

            mDictionary.OnRemove.Register((key, value) =>
            {
                print("remove:" + key + "," + value);
            }).UnRegisterWhenGameObjectDestroyed(gameObject);
            
            mDictionary.OnReplace.Register((key, oldValue,newValue) =>
            {
                print("replace:" + key + "," + oldValue + "," + newValue);
            }).UnRegisterWhenGameObjectDestroyed(gameObject);

            mDictionary.OnClear.Register(() =>
            {
                print("clear");
            }).UnRegisterWhenGameObjectDestroyed(gameObject);
        }

        private string mKeyToDelete = null;
        private void OnGUI()
        {
            IMGUIHelper.SetDesignResolution(640,360);
            
            GUILayout.Label("Count:" + mDictionary.Count);
            GUILayout.BeginVertical("box");
            
            foreach (var kv in mDictionary)
            {
                GUILayout.BeginHorizontal("box");
                GUILayout.Label($"{kv.Key},{kv.Value}");
                if (GUILayout.Button("-"))
                {
                    mKeyToDelete = kv.Key;
                }

                GUILayout.EndHorizontal();
            }

            if (GUILayout.Button("add"))
            {
                var key = "key" + Random.Range(0, 100);
                if (!mDictionary.ContainsKey(key))
                {
                    mDictionary.Add("key" + Random.Range(0,100),"value" + Random.Range(0,100));    
                }
            }

            if (mDictionary.Count > 0)
            {
                if (GUILayout.Button("remove"))
                {
                    mDictionary.Remove(mDictionary.Keys.First());
                }

                if (GUILayout.Button("replace"))
                {
                    mDictionary[mDictionary.Keys.First()] = "replaced value" + Random.Range(0, 100);
                }

                if (GUILayout.Button("clear"))
                {
                    mDictionary.Clear();
                }
            }

            GUILayout.EndVertical();
            
            
            
            
            if (mKeyToDelete.IsNotNullAndEmpty())
            {
                mDictionary.Remove(mKeyToDelete);
                mKeyToDelete = null;
            }
        }
    }
}

```

运行结果如下:
![178a650f-97ee-44de-8f5c-447a853cdb6b](https://file.liangxiegame.com/178a650f-97ee-44de-8f5c-447a853cdb6b.gif)

输出结果如下:
![b8a5eb15-42cc-4c2d-9e5e-dff54e2a10e2](https://file.liangxiegame.com/b8a5eb15-42cc-4c2d-9e5e-dff54e2a10e2.png)

本文由 QFramework 教程年会员赞助，地址：[https://www.gamepixedu.com/goods/show/55](https://www.gamepixedu.com/goods/show/55)

* QFramework 主页：[qframework.cn](https://qframework.cn)
* QFramework 交流群: 541745166
* QFramework Github 地址: <https://github.com/liangxiegame/qframework>
* QFramework Gitee 地址：<https://gitee.com/liangxiegame/QFramework>
