adospace / reactorui-maui

MauiReactor is a MVU UI framework built on top of .NET MAUI
MIT License
588 stars 49 forks source link

CommunityToolkit Popup does not contain a definition for OnClosed #200

Closed cris-m closed 8 months ago

cris-m commented 8 months ago

I was testing CommunityToolkit Popup example and in PopupHost class:

public override VisualNode Render()
{
    var children = Children();

    return _isShown ? 
        new Popup(r => 
        {
            _popup = r;
            _nativePopupCreateAction?.Invoke(r);
        })
        {
            children[0]
        }
        .OnClosed(OnClosed) // error
        : null!;
 }
private void OnClosed(object? sender, PopupClosedEventArgs args)
{
   _onCloseAction?.Invoke(args.Result);
}

The OnClosed action on Popup create the following statement error: 'Popup' does not contain a definition for 'OnClosed' and no accessible extension method 'OnClosed' accepting a first argument of type 'Popup' could be found (are you missing a using directive or an assembly reference?)

adospace commented 8 months ago

OnClosed event is auto-generated from the source generator, if it doesn't build it probably means that dotnet build doesn't correctly reference the source generator. Try installing the latest version of MauiReactor that doesn't require the external MauiReactor scaffold generator project:

Use this:

  <ItemGroup>
    <PackageReference Include="Reactor.Maui" Version="2.0.19" />
    <PackageReference Include="Reactor.Maui.Canvas" Version="2.0.19" />
  </ItemGroup>

instead of:

  <ItemGroup>
    <PackageReference Include="Reactor.Maui" Version="2.0.X-beta" />
    <PackageReference Include="Reactor.Maui.Canvas" Version="2.0.X-beta" />
    <PackageReference Include="Reactor.Maui.ScaffoldGenerator" Version="2.0.X-beta" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  </ItemGroup>
cris-m commented 8 months ago

Thanks I will try to upgrade the package

cris-m commented 8 months ago

I update to the last version but I still get the same problem.

1. Popup control

namespace  ExampleApp.Controls.Native;
[Scaffold(typeof(CommunityToolkit.Maui.Views.Popup))]
partial class Popup
{
    protected override void OnAddChild(VisualNode widget, BindableObject childNativeControl)
    {
        if(childNativeControl is MauiControls.View content)
        {
            Validate.EnsureNotNull(NativeControl);
            NativeControl.Content = content;
        }

        base.OnAddChild(widget, childNativeControl);
    }

    protected override void OnRemoveChild(VisualNode widget, BindableObject childNativeControl)
    {
        Validate.EnsureNotNull(NativeControl);

        if(childNativeControl is MauiControls.View content && 
        NativeControl.Content == content)
        {
            NativeControl.Content = null;
        }

        base.OnRemoveChild(widget, childNativeControl);
    }
}

2. PopupHost Components

namespace ExampleApp.Components;

#nullable enable
public class PopupHost : Component
{
    private Popup? _popup;
    private bool _isShown;
    private Action<object?>? _onCloseAction;
    private readonly Action<Popup?>? _nativePopupCreateAction;

    public PopupHost(Action<Popup?>? nativePopupCreateAction = null)
    {
        _nativePopupCreateAction = nativePopupCreateAction;
    }
    public PopupHost IsShown(bool isShown)
    {
        _isShown = isShown;
        return this;
    }
    public PopupHost OnClosed(Action<Object?> action)
    {
        _onCloseAction = action;
        return this;
    }
    protected override void OnMounted()
    {
        InitializePopup();
        base.OnMounted();
    }
    protected override void OnPropsChanged()
    {
        InitializePopup();
        base.OnPropsChanged();
    }
    private void InitializePopup()
    {
        if(_isShown && MauiControls.Application.Current != null)
        {
            MauiControls.Application.Current?.Dispatcher.Dispatch(() => {
                if(ContainerPage == null || _popup == null)
                    return;

                ContainerPage.ShowPopup(_popup);
            });
        }
    }

    public override VisualNode Render()
    {
        var children = Children();

        return _isShown ? 
            new Controls.Native.Popup(r => 
            {
                _popup = r;
                _nativePopupCreateAction?.Invoke(r);
            })
            {
                children[0]
            }
            .OnClosed(OnClosed)
            : null!;
    }
    public void OnClosed(object? sender, PopupClosedEventArgs args)
    {
        _onCloseAction?.Invoke(args.Result);
    }
}
adospace commented 8 months ago

sorry, but I'm not sure what exactly is the problem: you can't build the project for the error: 'Popup' does not contain a definition for 'OnClosed'..., right?

can you confirm that you can't build the following project? PopupIssue.zip

cris-m commented 8 months ago

I have identified the problem. Thanks for the support.