From 98ffc2efe58dee80c600b60ab102a4a7d8220f7d Mon Sep 17 00:00:00 2001 From: Ken Tucker Date: Wed, 18 Jun 2025 17:47:30 -0400 Subject: [PATCH 1/2] Refactor color parsing and improve FrameAdapter structure - Cleaned up comments and refactored hex color parsing in AppManifestHelper.cs to handle alpha values more efficiently. - Introduced a new `navigationManager` field in FrameAdapter.cs, initialized for UWP, and created an `AddEventHandlers` method to better organize event subscriptions and prevent memory leaks. --- .../Platforms/uap/AppManifestHelper.cs | 26 +++++++------------ .../Platforms/uap/FrameAdapter.cs | 15 ++++++----- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/src/Caliburn.Micro.Platform/Platforms/uap/AppManifestHelper.cs b/src/Caliburn.Micro.Platform/Platforms/uap/AppManifestHelper.cs index 0546bdabd..ef42d80bf 100644 --- a/src/Caliburn.Micro.Platform/Platforms/uap/AppManifestHelper.cs +++ b/src/Caliburn.Micro.Platform/Platforms/uap/AppManifestHelper.cs @@ -1,4 +1,4 @@ -// +// // Copyright (c) 2012 Tim Heuer // // Licensed under the Microsoft Public License (Ms-PL) (the "License"); @@ -49,8 +49,8 @@ public async static Task GetManifestVisualElementsAsync() SmallLogoUri = new Uri(string.Format("ms-appx:///{0}", vel.Attribute("Square30x30Logo").Value.Replace(@"\", @"/"))), BackgroundColorAsString = vel.Attribute("BackgroundColor").Value }).FirstOrDefault(); - - if (visualElementNode == null) + + if (visualElementNode == null) throw new ArgumentNullException("Could not parse the VisualElements from the app manifest."); return visualElementNode; @@ -94,23 +94,17 @@ private static Windows.UI.Color ToColor(string hexValue) throw new ArgumentException("This does not appear to be a proper hex color number"); } - byte a = 255; - byte r = 255; - byte g = 255; - byte b = 255; + bool isEightCharactersLong = hexValue.Length == 8; - int startPosition = 0; + int startPosition = isEightCharactersLong ? 2 : 0; // the case where alpha is provided - if (hexValue.Length == 8) - { - a = byte.Parse(hexValue.Substring(0, 2), NumberStyles.HexNumber); - startPosition = 2; - } - r = byte.Parse(hexValue.Substring(startPosition, 2), NumberStyles.HexNumber); - g = byte.Parse(hexValue.Substring(startPosition + 2, 2), NumberStyles.HexNumber); - b = byte.Parse(hexValue.Substring(startPosition + 4, 2), NumberStyles.HexNumber); + byte a = isEightCharactersLong ? byte.Parse(hexValue.Substring(0, 2), NumberStyles.HexNumber) : (byte)255; + + byte r = byte.Parse(hexValue.Substring(startPosition, 2), NumberStyles.HexNumber); + byte g = byte.Parse(hexValue.Substring(startPosition + 2, 2), NumberStyles.HexNumber); + byte b = byte.Parse(hexValue.Substring(startPosition + 4, 2), NumberStyles.HexNumber); return Windows.UI.Color.FromArgb(a, r, g, b); } diff --git a/src/Caliburn.Micro.Platform/Platforms/uap/FrameAdapter.cs b/src/Caliburn.Micro.Platform/Platforms/uap/FrameAdapter.cs index 6d16a42dd..08894af94 100644 --- a/src/Caliburn.Micro.Platform/Platforms/uap/FrameAdapter.cs +++ b/src/Caliburn.Micro.Platform/Platforms/uap/FrameAdapter.cs @@ -32,7 +32,6 @@ public class FrameAdapter : INavigationService, IDisposable private static readonly ILog Log = LogManager.GetLog(typeof(FrameAdapter)); private const string FrameStateKey = "FrameState"; private const string ParameterKey = "ParameterKey"; - private readonly Frame frame; private readonly bool treatViewAsLoaded; private event NavigatingCancelEventHandler ExternalNavigatingHandler = delegate { }; @@ -50,16 +49,18 @@ public FrameAdapter(Frame frame, bool treatViewAsLoaded = false) this.frame = frame; this.treatViewAsLoaded = treatViewAsLoaded; +#if WINDOWS_UWP + navigationManager = SystemNavigationManager.GetForCurrentView(); +#endif + AddEventHandlers(); + } + + private void AddEventHandlers() + { this.frame.Navigating += OnNavigating; this.frame.Navigated += OnNavigated; #if WINDOWS_UWP - - // This could leak memory if we're creating and destorying navigation services regularly. - // Another unlikely scenario though - - navigationManager = SystemNavigationManager.GetForCurrentView(); - navigationManager.BackRequested += OnBackRequested; #endif } From dbb55dfecca01ccd6f809adc7ef0d27819a07302 Mon Sep 17 00:00:00 2001 From: Ken Tucker Date: Wed, 18 Jun 2025 18:22:30 -0400 Subject: [PATCH 2/2] Add comments --- src/Caliburn.Micro.Platform/Platforms/uap/AppManifestHelper.cs | 2 +- src/Caliburn.Micro.Platform/Platforms/uap/FrameAdapter.cs | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Caliburn.Micro.Platform/Platforms/uap/AppManifestHelper.cs b/src/Caliburn.Micro.Platform/Platforms/uap/AppManifestHelper.cs index ef42d80bf..e87eaf4b5 100644 --- a/src/Caliburn.Micro.Platform/Platforms/uap/AppManifestHelper.cs +++ b/src/Caliburn.Micro.Platform/Platforms/uap/AppManifestHelper.cs @@ -95,7 +95,7 @@ private static Windows.UI.Color ToColor(string hexValue) } bool isEightCharactersLong = hexValue.Length == 8; - + // if the string is 8 characters it includes the a part int startPosition = isEightCharactersLong ? 2 : 0; // the case where alpha is provided diff --git a/src/Caliburn.Micro.Platform/Platforms/uap/FrameAdapter.cs b/src/Caliburn.Micro.Platform/Platforms/uap/FrameAdapter.cs index 08894af94..44022a03c 100644 --- a/src/Caliburn.Micro.Platform/Platforms/uap/FrameAdapter.cs +++ b/src/Caliburn.Micro.Platform/Platforms/uap/FrameAdapter.cs @@ -55,6 +55,7 @@ public FrameAdapter(Frame frame, bool treatViewAsLoaded = false) AddEventHandlers(); } + // add evenhandlers for navigating and navigated events private void AddEventHandlers() { this.frame.Navigating += OnNavigating;