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

Update NativeControlComponentBase to implement IComponent directly #84

Merged
merged 1 commit into from
Nov 23, 2022
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
57 changes: 49 additions & 8 deletions src/BlazorBindings.Core/NativeControlComponentBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,33 @@

namespace BlazorBindings.Core
{
public abstract class NativeControlComponentBase : ComponentBase
public abstract class NativeControlComponentBase : IComponent
{
private readonly RenderFragment _renderFragment;
private bool _hasPendingQueuedRender;
private RenderHandle _renderHandle;
private Exception _eventCallbackException;

public IElementHandler ElementHandler { get; private set; }
protected IElementHandler ElementHandler { get; private set; }

public void SetElementReference(IElementHandler elementHandler)
public NativeControlComponentBase()
{
ElementHandler = elementHandler ?? throw new ArgumentNullException(nameof(elementHandler));
_renderFragment = builder =>
{
_hasPendingQueuedRender = false;
BuildRenderTree(builder);
};
}

protected override void BuildRenderTree(RenderTreeBuilder builder)
public virtual Task SetParametersAsync(ParameterView parameters)
{
ArgumentNullException.ThrowIfNull(builder);
parameters.SetParameterProperties(this);
StateHasChanged();
return Task.CompletedTask;
}

protected virtual void BuildRenderTree(RenderTreeBuilder builder)
{
if (_eventCallbackException != null)
{
var oldException = _eventCallbackException;
Expand Down Expand Up @@ -54,9 +66,28 @@ protected virtual void RenderAdditionalElementContent(RenderTreeBuilder builder,
{
}

protected void StateHasChanged()
{
if (_hasPendingQueuedRender)
return;

try
{
_renderHandle.Render(_renderFragment);
}
finally
{
_hasPendingQueuedRender = false;
}
}

protected Task InvokeAsync(Action workItem) => _renderHandle.Dispatcher.InvokeAsync(workItem);

protected Task InvokeAsync(Func<Task> workItem) => _renderHandle.Dispatcher.InvokeAsync(workItem);

protected Task InvokeEventCallback<T>(EventCallback<T> eventCallback, T value)
{
return InvokeAsync(async () =>
return _renderHandle.Dispatcher.InvokeAsync(async () =>
{
try
{
Expand All @@ -74,7 +105,7 @@ protected Task InvokeEventCallback<T>(EventCallback<T> eventCallback, T value)

protected Task InvokeEventCallback(EventCallback eventCallback)
{
return InvokeAsync(async () =>
return _renderHandle.Dispatcher.InvokeAsync(async () =>
{
try
{
Expand All @@ -91,5 +122,15 @@ protected Task InvokeEventCallback(EventCallback eventCallback)
}

protected virtual RenderFragment GetChildContent() => null;

internal void SetElementReference(IElementHandler elementHandler)
{
ElementHandler = elementHandler ?? throw new ArgumentNullException(nameof(elementHandler));
}

void IComponent.Attach(RenderHandle renderHandle)
{
_renderHandle = renderHandle;
}
}
}
4 changes: 2 additions & 2 deletions src/BlazorBindings.Maui/Elements/BindableObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public override Task SetParametersAsync(ParameterView parameters)
{
HandleParameter(parameterValue.Name, parameterValue.Value);
}

return base.SetParametersAsync(ParameterView.Empty);
StateHasChanged();
return Task.CompletedTask;
}

protected sealed override void RenderAttributes(AttributesBuilder builder)
Expand Down
9 changes: 4 additions & 5 deletions src/BlazorBindings.Maui/Elements/Shell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,10 @@ static partial void RegisterAdditionalHandlers()

[Parameter] public RenderFragment ChildContent { get; set; }

// <summary>
// Defines the background color in the Shell chrome. The color will not fill in behind the Shell content.
// </summary>
// Cannot add 'new' properties.
//[Parameter] public Color BackgroundColor { get; set; }
/// <summary>
/// Defines the background color in the Shell chrome. The color will not fill in behind the Shell content.
/// </summary>
[Parameter] public new Color BackgroundColor { get; set; }

/// <summary>
/// Defines the color to shade text and icons that are disabled.
Expand Down