Skip to content
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

Use ParameterView for setting parameters #453

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
36 changes: 2 additions & 34 deletions src/Microsoft.MobileBlazorBindings.Core/NativeComponentRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,8 @@ public async Task<IComponent> AddComponent(Type componentType, IElementHandler p

_componentIdToAdapter[componentId] = rootAdapter;

SetParameterArguments(component, parameters);

await RenderRootComponentAsync(componentId).ConfigureAwait(false);
var parameterView = parameters?.Count > 0 ? ParameterView.FromDictionary(parameters) : ParameterView.Empty;
await RenderRootComponentAsync(componentId, parameterView).ConfigureAwait(false);
return component;
}).ConfigureAwait(false);
}
Expand Down Expand Up @@ -154,36 +153,5 @@ internal NativeComponentAdapter CreateAdapterForChildComponent(IElementHandler p
_componentIdToAdapter[componentId] = result;
return result;
}

internal static void SetParameterArguments(IComponent component, Dictionary<string, object> arguments)
{
if (component == null)
{
throw new ArgumentNullException(nameof(component));
}
if (arguments == null || arguments.Count == 0)
{
//parameters will often be null. e.g. if you navigate with no parameters or when creating a root component.
return;
}

foreach (var parameter in arguments)
{
var prop = component.GetType().GetProperty(parameter.Key);

if (prop == null)
{
throw new InvalidOperationException($"Object of type '{component.GetType()}' does not have a property matching the name '{parameter.Key}'.");
}

var parameterAttribute = prop.GetCustomAttribute(typeof(ParameterAttribute));
if (parameterAttribute == null)
{
throw new InvalidOperationException($"Object of type '{component.GetType()}' has a property matching the name '{parameter.Key}', but it does not have [ParameterAttribute] or [CascadingParameterAttribute] applied.");
}

prop.SetValue(component, parameter.Value);
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.MobileBlazorBindings.ShellNavigation;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
Expand Down Expand Up @@ -183,7 +184,7 @@ internal static bool TryParse(Type type, string s, out object result)
}
else if (type == typeof(int))
{
success = int.TryParse(s, out var parsed);
success = int.TryParse(s, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out var parsed);
result = parsed;
}
else if (type == typeof(Guid))
Expand All @@ -198,27 +199,27 @@ internal static bool TryParse(Type type, string s, out object result)
}
else if (type == typeof(DateTime))
{
success = DateTime.TryParse(s, out var parsed);
success = DateTime.TryParse(s,DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out var parsed);
result = parsed;
}
else if (type == typeof(decimal))
{
success = decimal.TryParse(s, out var parsed);
success = decimal.TryParse(s, NumberStyles.Number, NumberFormatInfo.InvariantInfo, out var parsed);
result = parsed;
}
else if (type == typeof(double))
{
success = double.TryParse(s, out var parsed);
success = double.TryParse(s, NumberStyles.Float, NumberFormatInfo.InvariantInfo, out var parsed);
result = parsed;
}
else if (type == typeof(float))
{
success = float.TryParse(s, out var parsed);
success = float.TryParse(s, NumberStyles.Float, NumberFormatInfo.InvariantInfo, out var parsed);
result = parsed;
}
else if (type == typeof(long))
{
success = long.TryParse(s, out var parsed);
success = long.TryParse(s, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out var parsed);
result = parsed;
}
else
Expand Down