kekyo / FlashCap

Independent video frame capture library on .NET/.NET Core and .NET Framework.
Apache License 2.0
203 stars 29 forks source link

Avalonia 11: MVVM Community Toolkit: ObservableCollection<CaptureDeviceDescriptor> is null #111

Closed ManuelKetisch closed 1 year ago

ManuelKetisch commented 1 year ago

Hello,

i am having a little trouble using FlashCap with Avalonia 11 and the MVVM Community Toolkit.

I mostly used the Avalonia code sample to get FlashCap up and running, but ran into a problem.

The original sample code looks like this: ` public Command? Opened { get; } public SKBitmap? Image { get; private set; } public bool IsEnbaled { get; private set; }

public ObservableCollection<CaptureDeviceDescriptor?> DeviceList { get; } = new();
public CaptureDeviceDescriptor? Device { get; set; }

public ObservableCollection<VideoCharacteristics> CharacteristicsList { get; } = new();
public VideoCharacteristics? Characteristics { get; set; }

public string? Statistics1 { get; private set; }
public string? Statistics2 { get; private set; }
public string? Statistics3 { get; private set; }

public MainWindowViewModel()
{
    // Window shown:
    this.Opened = Command.Factory.CreateSync(() =>
    {
        ////////////////////////////////////////////////
        // Initialize and start capture device

        // Enumerate capture devices:
        var devices = new CaptureDevices();

        // Store device list into the combo box.
        this.DeviceList.Clear();
        this.DeviceList.Add(null);

        foreach (var descriptor in devices.EnumerateDescriptors().
            // You could filter by device type and characteristics.
            //Where(d => d.DeviceType == DeviceTypes.DirectShow).  // Only DirectShow device.
            Where(d => d.Characteristics.Length >= 1))             // One or more valid video characteristics.
        {
            this.DeviceList.Add(descriptor);
        }

        this.IsEnbaled = true;
    });
}

`

The code i am currently using looks like this: ` private long frameCount; private ImageCaptureWindow? _currentWindow; private CaptureDevice? _captureDevice;

    [ObservableProperty] private string? _statistics1;
    [ObservableProperty] private string? _statistics2;
    [ObservableProperty] private string? _statistics3;

    [ObservableProperty] private bool? _isEnabled;
    [ObservableProperty] private bool? _windowIsOpen;

    [ObservableProperty] [NotifyPropertyChangedFor(nameof(Device))] private ObservableCollection<CaptureDeviceDescriptor>? _devices;
    [ObservableProperty] [NotifyPropertyChangedFor(nameof(Characteristic))] private ObservableCollection<VideoCharacteristics>? _characteristics;

    [ObservableProperty] private CaptureDeviceDescriptor? _device;
    [ObservableProperty] private VideoCharacteristics? _characteristic;
    [ObservableProperty] private SKBitmap? _image;

    public ImageCaptureWindowViewModel()
    {
        // Enumerate capture devices:
        var devices = new CaptureDevices();

        // Store device list into combobox:
        Devices?.Clear();
        Devices = null;

        // Get device descriptor
        foreach (var descriptor in devices.EnumerateDescriptors().
                     Where(d => d.DeviceType == DeviceTypes.DirectShow))
        {
            Devices?.Add(descriptor);
        }

        IsEnabled = true;
    }

`

My particular problem is that... Devices?.Add(descriptor); ...does not add the descriptor to... [ObservableProperty] [NotifyPropertyChangedFor(nameof(Device))] private ObservableCollection<CaptureDeviceDescriptor>? _devices;

The descriptor is NOT null, but it is not being added to the collection.

To me it is not obvious why that is the case. Does anyone have any tips what is causing this?

Just in case someone does not know, MVVM Community Toolkit is a similar MVVM framework than Epoxy. It auto-generates the public property 'Devices' from the private observable property '_devices'.

Any help would be appreciated.

ManuelKetisch commented 1 year ago

Never mind... my bad... I was not initializing the Collection, so there was no collection to store the descriptor. Also i wrote Devices = null ... because Devices.Add(null) from the orginal code did result in an error... Even if i would have initialized the collection... it would have returned to null.

Sorry for posting the question in the first place without thoroughly inspecting my code.

kekyo commented 1 year ago

No problem and thanks reached out FlashCap!