/// <summary> /// /// </summary> /// <param name="code"></param> /// <returns></returns> public static ICodeButton GetAssociatedControl(string code) { if (!ControlsDictionary.ContainsKey(code)) { throw new ArgumentException("Элемент с кодом '" + code + "' не зарегистрирован"); } return(ControlsDictionary[code]); }
/// <summary> /// /// </summary> /// <param name="control"></param> public static void UnregisterControl(this ICodeButton control) { string code = GetCode(control); if (!string.IsNullOrEmpty(code)) { ControlsDictionary.Remove(code); ControlsCallbacks.Remove(code); } }
public void SetControlsDictionary(ControlsDictionary controlsDictionary) { if (controlsDictionary != null) { foreach (var item in controlsDictionary) { this.controlsDictionary[item.Key] = item.Value; } } }
/// <summary> /// /// </summary> /// <param name="control"></param> /// <param name="code"></param> /// <param name="callback"></param> /// <returns></returns> public static string RegisterControl(this ICodeButton control, long code, ControlCallback callback) { string strCode = code.ToString(); if (ControlsDictionary.ContainsKey(strCode)) { throw new ArgumentException("Элемент с кодом '" + strCode + "' уже зарегистрирован"); } ControlsDictionary.Add(strCode, control); ControlsCallbacks.Add(strCode, callback); (control as ICodeButton).ButtonClick += (s, e) => callback.Invoke(strCode); ; return(strCode); }
/// <summary> /// /// </summary> /// <param name="control"></param> /// <param name="callback"></param> /// <returns></returns> public static string RegisterControl(this ICodeButton control, ControlCallback callback) { string strCode = GenerateCode().ToString(); long counter = 0; while (ControlsDictionary.ContainsKey(strCode)) { if (counter >= END_RANGE) { break; } strCode = GenerateCode().ToString(); counter++; } ControlsDictionary.Add(strCode, control); ControlsCallbacks.Add(strCode, callback); (control as ICodeButton).ButtonClick += (s, e) => callback.Invoke(strCode); ; return(strCode); }
/// <summary> /// /// </summary> /// <param name="control"></param> /// <returns></returns> private static string GetCode(ICodeButton control) { return(ControlsDictionary.FirstOrDefault(x => x.Value.GetHashCode() == control.GetHashCode()).Key); }