diff options
-rw-r--r-- | Xamarin.PropertyEditing.Mac/Controls/Custom/HistoryLayer.cs | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/Xamarin.PropertyEditing.Mac/Controls/Custom/HistoryLayer.cs b/Xamarin.PropertyEditing.Mac/Controls/Custom/HistoryLayer.cs index 9f90d61..d4ec241 100644 --- a/Xamarin.PropertyEditing.Mac/Controls/Custom/HistoryLayer.cs +++ b/Xamarin.PropertyEditing.Mac/Controls/Custom/HistoryLayer.cs @@ -3,6 +3,7 @@ using AppKit; using CoreAnimation; using CoreGraphics; using ObjCRuntime; +using Xamarin.PropertyEditing.Drawing; namespace Xamarin.PropertyEditing.Mac { @@ -77,9 +78,20 @@ namespace Xamarin.PropertyEditing.Mac public override void UpdateFromModel (EditorInteraction interaction) { LayoutIfNeeded (); - current.BackgroundColor = interaction.Color.ToCGColor (); - previous.BackgroundColor = interaction.ViewModel.InitialColor.ToCGColor (); - last.BackgroundColor = interaction.LastColor.ToCGColor (); + SetCALayerBackgroundColor (current, interaction.Color); + //there are some scenarios where viewmodel could be null + if (interaction.ViewModel != null) + SetCALayerBackgroundColor (previous, interaction.ViewModel.InitialColor); + SetCALayerBackgroundColor (last, interaction.LastColor); + } + + static void SetCALayerBackgroundColor(CALayer layer, CommonColor color) + { + // We need to use GC.KeepAlive to ensure that the CGColor doesn't get garbage collected (and thus the + // native object Released) before the BackgroundColor assignment is complete + CGColor cgColor = color.ToCGColor (); + layer.BackgroundColor = cgColor; + GC.KeepAlive (cgColor); } public override void UpdateFromLocation (EditorInteraction interaction, CGPoint location) |