Skip to content

Refactor color parsing and improve FrameAdapter structure #977

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 11 additions & 17 deletions src/Caliburn.Micro.Platform/Platforms/uap/AppManifestHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//
//
// Copyright (c) 2012 Tim Heuer
//
// Licensed under the Microsoft Public License (Ms-PL) (the "License");
Expand Down Expand Up @@ -49,8 +49,8 @@ public async static Task<VisualElement> 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;
Expand Down Expand Up @@ -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;

int startPosition = 0;
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
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);
}
Expand Down
16 changes: 9 additions & 7 deletions src/Caliburn.Micro.Platform/Platforms/uap/FrameAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 { };
Expand All @@ -50,16 +49,19 @@ public FrameAdapter(Frame frame, bool treatViewAsLoaded = false)
this.frame = frame;
this.treatViewAsLoaded = treatViewAsLoaded;

#if WINDOWS_UWP
navigationManager = SystemNavigationManager.GetForCurrentView();
#endif
AddEventHandlers();
}

// add evenhandlers for navigating and navigated events
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
}
Expand Down
Loading