adospace / reactorui-maui

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

Need some help with Scaffold 3rd party control error. #81

Closed dstanchev closed 1 year ago

dstanchev commented 1 year ago

Hello, I have the following issue when using the QR/Barcode scanning library Camera.MAUI The CameraView control is used for the actual scanning. I use the following code:

[Scaffold(typeof(Camera.MAUI.CameraView))]
public partial class CameraView { }

and the error on build is: "CameraViewExtensions.Camera(T, CameraInfo)' is a method, which is not valid in the given context"

if I add this two objects too

[Scaffold(typeof(Camera.MAUI.CameraInfo))]
public partial class CameraInfo { }

[Scaffold(typeof(Camera.MAUI.MicrophoneInfo))]
public partial class MicrophoneInfo { }

Build fail with error: The type or namespace name 'IObject' could not be found (are you missing a using directive or an assembly reference?)

I would appreciate any advice on how to fix this.

adospace commented 1 year ago

Working on the fix...

adospace commented 1 year ago

Hi, this bug has been fixed in version 1.0.131. This is the code I used to verify it:

[Scaffold(typeof(Camera.MAUI.CameraView))]
public partial class CameraView { }

public class TestPageState
{
    public ObservableCollection<CameraInfo> Cameras { get; set; } = new();

    public ObservableCollection<MicrophoneInfo> Microphones { get; set; } = new();

    public CameraInfo SelectedCamera { get; set; }

    public MicrophoneInfo SelectedMicrophone { get; set; }

    public bool IsStarted { get; set; }
}

public class TestPage : Component<TestPageState>
{
    public override VisualNode Render()
    {
        return new ContentPage()
        {
            new Grid("Auto,Auto, Auto,*", "*")
            {
                new Picker()
                    .ItemsSource(State.Cameras.Select(_=>_.Name).ToList())
                    .OnSelectedIndexChanged(index => SetState(s => s.SelectedCamera = index >= 0 ? s.Cameras[index] : null))
                    ,
                new Picker()
                    .ItemsSource(State.Microphones.Select(_=>_.Name).ToList())
                    .OnSelectedIndexChanged(index => SetState(s => s.SelectedMicrophone  = index >= 0 ? s.Microphones[index] : null))
                    .GridRow(1)
                    ,
                new HStack
                {
                    new Button("Start")
                        .OnClicked(()=>SetState(s => s.IsStarted = true)),
                    new Button("Stop")
                        .OnClicked(()=>SetState(s => s.IsStarted = false)),
                }
                .GridRow(2),

                new CameraView()
                    .OnLoaded(OnCameraViewLoaded)
                    .Camera(State.SelectedCamera)
                    .Microphone(State.SelectedMicrophone)
                    .AutoStartPreview(State.IsStarted)
                    .GridRow(3)
            }
        };
    }

    private void OnCameraViewLoaded(object sender, EventArgs args)
    {
        var cameraView = (Camera.MAUI.CameraView)sender;

        SetState(s =>
        {
            s.Cameras = cameraView.Cameras;
            s.Microphones = cameraView.Microphones;
            s.SelectedCamera = cameraView.Cameras.FirstOrDefault();
            s.SelectedMicrophone = cameraView.Microphones.FirstOrDefault();
        });
    }
}

You only need to scaffold MAUI View-derived objects. CameraInfo and MicrophoneInfo are just DTO, so there is no need to scaffold them.

dstanchev commented 1 year ago

It's work like a charm! I would also like to thank you for your work on this project!