How do I create a ViewModel [ReactiveUI] property in AvaloniaUI with custom getter/setter?

41 views Asked by At

I have a AvaloniaUI WPF application (which uses ReactiveUI). I'm having difficulty create a [Reactive] property with a custom getter/setter.

Consider the following code snippet:

class SomeValueClass : IValue {}

private IValue? ValueStubFoo;
private IValue? ValueStub;
[Reactive] public IValue? CustomValue 
{ 
    get
    {
        if (ValueStub is null)
        {
            if (ValueStubFoo is SomeValueClass valueStub)
            {
                // Clone() is a method on SomeValueClass
                ValueStub = valueStub.Clone();
            }
        }
        return ValueStub;
    }
    set { ValueStub = value; } 
}

At compile time I get the following exception:

Error Fody: An unhandled exception occurred: Exception: Failed to execute weaver C:..\weaver\ReactiveUI.Fody.dll Type: System.Exception StackTrace: at InnerWeaver.ExecuteWeavers() in C:\projects\fody\FodyIsolated\InnerWeaver.cs:line 222 at InnerWeaver.Execute() in C:\projects\fody\FodyIsolated\InnerWeaver.cs:line 113 Source: FodyIsolated TargetSite: Void ExecuteWeavers() Sequence contains more than one matching element Type: System.InvalidOperationException StackTrace: at System.Linq.Enumerable.Single[TSource](IEnumerable1 source, Func2 predicate) at ReactiveUI.Fody.ReactiveUIPropertyWeaver.Execute() at ReactiveUI.Fody.ModuleWeaver.Execute() at InnerWeaver.ExecuteWeavers() in C:\projects\fody\FodyIsolated\InnerWeaver.cs:line 186 Source: System.Core TargetSite: TSource Single[TSource](System.Collections.Generic.IEnumerable1[TSource], System.Func2[TSource,System.Boolean])

I've isolated to the [Reactive] tag on the CustomValue property. If I remove the tag, it builds correctly. It looks like there's an issue with [Reactive] properties with custom accessors.

How do I get this to work with custom getters/setters?

0

There are 0 answers