Skip to content

Commit

Permalink
Optimize dictionary usage and improve code readability
Browse files Browse the repository at this point in the history
Updated MessageBinder.cs:
- Modified SpecialValues dictionary initialization for consistent formatting and alignment.
- Optimized custom converter checks using TryGetValue for better performance and readability.
- Adjusted conditional compilation blocks for enum type checks for improved readability and consistency across platforms.

Updated FrameAdapter.cs:
- Simplified ResumeStateAsync method by using TryGetValue for dictionary lookups, enhancing performance.
- Streamlined CurrentParameter assignment with TryGetValue for efficiency.
  • Loading branch information
vb2ae committed Dec 17, 2024
1 parent 50a84b0 commit 1b07084
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
11 changes: 6 additions & 5 deletions src/Caliburn.Micro.Platform/MessageBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public static class MessageBinder
{
["$eventargs"] = c => c.EventArgs,
#if XFORMS || MAUI
["$datacontext"] = c => c.Source.BindingContext,
["$bindingcontext"] = c => c.Source.BindingContext,
["$datacontext"] = c => c.Source.BindingContext,
["$bindingcontext"] = c => c.Source.BindingContext,
#else
["$datacontext"] = c => c.Source.DataContext,
#endif
Expand Down Expand Up @@ -120,9 +120,9 @@ public static object CoerceValue(Type destinationType, object providedValue, obj
return providedValue;
}

if (CustomConverters.ContainsKey(destinationType))
if (CustomConverters.TryGetValue(destinationType, out Func<object, object, object> value))
{
return CustomConverters[destinationType](providedValue, context);
return value;
}
try
{
Expand All @@ -142,7 +142,8 @@ public static object CoerceValue(Type destinationType, object providedValue, obj
}
#endif
#if WINDOWS_UWP || XFORMS || MAUI
if (destinationType.GetTypeInfo().IsEnum) {
if (destinationType.GetTypeInfo().IsEnum)
{
#else
if (destinationType.IsEnum)
{
Expand Down
9 changes: 4 additions & 5 deletions src/Caliburn.Micro.Platform/Platforms/uap/FrameAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -368,14 +368,13 @@ public virtual async Task<bool> ResumeStateAsync()
{
var container = GetSettingsContainer();

if (!container.Values.ContainsKey(FrameStateKey))
if (!container.Values.TryGetValue(FrameStateKey, out object stateKeyValue))
return false;

var frameState = (string)container.Values[FrameStateKey];
var frameState = (string)stateKeyValue;

CurrentParameter = container.Values.ContainsKey(ParameterKey) ?
container.Values[ParameterKey] :
null;
CurrentParameter = container.Values.TryGetValue(ParameterKey, out object parameterKeyValue) ?
parameterKeyValue : null;

if (String.IsNullOrEmpty(frameState))
return false;
Expand Down

0 comments on commit 1b07084

Please sign in to comment.