From: jpbruyere Date: Mon, 29 Feb 2016 09:43:47 +0000 (+0100) Subject: debug bindings, clear only necessary bindings depending on source Instance changes X-Git-Tag: v0.4~100 X-Git-Url: https://git.osiis.dedyn.io/?a=commitdiff_plain;h=35b50178a77099f9f8b6432e2a6ef8391f120502;p=jp%2Fcrow.git debug bindings, clear only necessary bindings depending on source Instance changes --- diff --git a/src/CompilerServices/CompilerServices.cs b/src/CompilerServices/CompilerServices.cs index c224f01d..76a38c49 100644 --- a/src/CompilerServices/CompilerServices.cs +++ b/src/CompilerServices/CompilerServices.cs @@ -48,6 +48,8 @@ namespace Crow public class Binding{ static int bindingCpt = 0; string dynMethodId = ""; + bool resolved = false; + public string NewDynMethodId { get { if (!string.IsNullOrEmpty (dynMethodId)) @@ -60,7 +62,24 @@ namespace Crow public string DynMethodId { get { return dynMethodId; } } - public bool Resolved = false; + + + public bool Resolved { + get { + return resolved; + } + set { + if (value == resolved) + return; + #if DEBUG_BINDING + if (value == true) + Debug.WriteLine ("\tOk => " + this.ToString()); + else + Debug.WriteLine ("\tresolved state reseted => " + this.ToString()); + #endif + resolved = value; + } + } public MemberReference Target; public MemberReference Source; @@ -100,8 +119,10 @@ namespace Crow ptr++; } while (ptr < bindingExp.Length - 1) { - if (tmp == null) + if (tmp == null) { + Debug.WriteLine ("\tERROR: target not found => " + this.ToString()); return false; + } if (bindingExp [ptr] == "..") tmp = tmp.LogicalParent; else if (bindingExp [ptr] == ".") { @@ -113,8 +134,10 @@ namespace Crow ptr++; } - if (tmp == null) + if (tmp == null) { + Debug.WriteLine ("\tERROR: target not found => " + this.ToString()); return false; + } string[] bindTrg = bindingExp [ptr].Split ('.'); diff --git a/src/GraphicObjects/GraphicObject.cs b/src/GraphicObjects/GraphicObject.cs index cb99fc11..2ddcecca 100644 --- a/src/GraphicObjects/GraphicObject.cs +++ b/src/GraphicObjects/GraphicObject.cs @@ -425,6 +425,12 @@ namespace Crow set { if (dataSource == value) return; + #if DEBUG_BINDING + Debug.WriteLine("******************************"); + Debug.WriteLine("New DataSource for => " + this.ToString()); + Debug.WriteLine("\t- " + DataSource); + Debug.WriteLine("\t+ " + value); + #endif this.ClearBinding (); @@ -436,7 +442,8 @@ namespace Crow } get { return dataSource == null ? LogicalParent == null ? null : - (LogicalParent as GraphicObject).DataSource : dataSource; + LogicalParent is GraphicObject ? + (LogicalParent as GraphicObject).DataSource : null : dataSource; } } [XmlAttributeAttribute] @@ -1045,38 +1052,45 @@ namespace Crow if (Bindings.Count == 0) return; #if DEBUG_BINDING - Debug.WriteLine ("ResolveBinding => " + this.ToString ()); + Debug.WriteLine ("Resolve Bindings => " + this.ToString ()); #endif + //grouped bindings by Instance of Source Dictionary> resolved = new Dictionary>(); + foreach (Binding b in Bindings) { if (b.Resolved) continue; + if (b.Target.Member.MemberType == MemberTypes.Event) { if (b.Expression.StartsWith("{")){ CompileEventSource(b); continue; } - } - if (!b.FindTarget ()) { - Debug.WriteLine ("BINDING ERROR: target not found => " + b.ToString()); - continue; - } - if (b.Target.Member.MemberType == MemberTypes.Event) { + if (!b.FindTarget ()) + continue; //register handler for event if (b.Source.Method == null) { - Debug.WriteLine ("Handler Method not found: " + b.ToString()); + Debug.WriteLine ("\tError: Handler Method not found: " + b.ToString()); continue; } - - MethodInfo addHandler = b.Target.Event.GetAddMethod (); - Delegate del = Delegate.CreateDelegate (b.Target.Event.EventHandlerType, b.Source.Instance, b.Source.Method); - addHandler.Invoke (this, new object[] { del }); - b.Resolved = true; - #if DEBUG_BINDING - Debug.WriteLine ("\tHandler binded => " + b.ToString()); - #endif + try { + MethodInfo addHandler = b.Target.Event.GetAddMethod (); + Delegate del = Delegate.CreateDelegate (b.Target.Event.EventHandlerType, b.Source.Instance, b.Source.Method); + addHandler.Invoke (this, new object[] { del }); + + #if DEBUG_BINDING + Debug.WriteLine ("\tHandler binded => " + b.ToString()); + #endif + b.Resolved = true; + } catch (Exception ex) { + Debug.WriteLine ("\tERROR: " + ex.ToString()); + } continue; } + + if (!b.FindTarget ()) + continue; + List bindings = null; if (!resolved.TryGetValue (b.Source.Instance, out bindings)) { bindings = new List (); @@ -1084,9 +1098,7 @@ namespace Crow } bindings.Add (b); b.Resolved = true; - #if DEBUG_BINDING - Debug.WriteLine ("\tmarked as resolved => " + b.ToString()); - #endif + } MethodInfo stringEquals = typeof(string).GetMethod @@ -1255,6 +1267,10 @@ namespace Crow /// Event binding details public void CompileEventSource(Binding binding) { + #if DEBUG_BINDING + Debug.WriteLine ("\tCompile Event Source => " + binding.ToString()); + #endif + Type sourceType = this.GetType(); #region Retrieve EventHandler parameter type @@ -1373,17 +1389,30 @@ namespace Crow addHandler.Invoke(this, new object[] {del}); binding.Resolved = true; - #if DEBUG_BINDING - Debug.WriteLine ("\tCompiled Event Source => " + binding.ToString()); - #endif } /// /// Remove dynamic delegates by ids from dataSource /// and delete ref of this in Shared interface refs /// public virtual void ClearBinding(){ + //dont clear binding if dataSource is not null, foreach (Binding b in Bindings) { try { + if (!b.Resolved) + continue; + //cancel compiled events + if (b.Source == null){ + continue; + #if DEBUG_BINDING + Debug.WriteLine("Clear binding canceled for => " + b.ToString()); + #endif + } + if (b.Source.Instance != DataSource){ + #if DEBUG_BINDING + Debug.WriteLine("Clear binding canceled for => " + b.ToString()); + #endif + continue; + } #if DEBUG_BINDING Debug.WriteLine("ClearBinding => " + b.ToString()); #endif