// Handle attached property changed to automatically target and untarget UIElements and Brushes. private static void OnIsTargetChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) { var isAdding = (bool)e.NewValue; if (isAdding) { if (obj is UIElement) { XamlLight.AddTargetElement(GetIdStatic(), obj as UIElement); } else if (obj is Brush) { XamlLight.AddTargetBrush(GetIdStatic(), obj as Brush); } } else { if (obj is UIElement) { XamlLight.RemoveTargetElement(GetIdStatic(), obj as UIElement); } else if (obj is Brush) { XamlLight.RemoveTargetBrush(GetIdStatic(), obj as Brush); } } }
protected override void OnDisconnected(UIElement oldElement) { // 这一句是对应的,Add了之后就要Remove XamlLight.RemoveTargetElement(GetId(), oldElement); // 释放资源 CompositionLight.Dispose(); CompositionLight = null; }
// Handle attached property changed to automatically target and untarget UIElements and Brushes. private static void OnTargetIdPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) { string oldId = e.OldValue as string; string newId = e.NewValue as string; string[] oldIds = null; if (!string.IsNullOrEmpty(oldId)) { oldIds = oldId.Split(','); } else { oldIds = new string[0]; } string[] newIds = null; if (!string.IsNullOrEmpty(newId)) { newIds = newId.Split(','); } else { newIds = new string[0]; } List <string> added = new List <string>(); List <string> removed = new List <string>(); foreach (var id in newIds) { if (!oldIds.Contains(id)) { added.Add(id); } } foreach (var id in oldIds) { if (!newIds.Contains(id)) { removed.Add(id); } } foreach (var id in added) { if (obj is UIElement) { XamlLight.AddTargetElement(GetIdStatic(id), obj as UIElement); } else if (obj is Brush) { XamlLight.AddTargetBrush(GetIdStatic(id), obj as Brush); } } foreach (var id in removed) { if (obj is UIElement) { XamlLight.RemoveTargetElement(GetIdStatic(id), obj as UIElement); } else if (obj is Brush) { XamlLight.RemoveTargetBrush(GetIdStatic(id), obj as Brush); } } }