AvaloniaUI / Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
https://avaloniaui.net
MIT License
26.13k stars 2.26k forks source link

Unhandled exception. System.InvalidOperationException: Operation is not valid due to the current state of the object. #9481

Closed lanyusan closed 1 year ago

lanyusan commented 2 years ago

Describe the bug When run reactiveui sample that bind control to model in this.WhenActivated(disposables => { , below error reported.

If bind in axmal Text="{Binding Greeting}, it works fine

Unhandled exception. System.InvalidOperationException: Operation is not valid due to the current state of the object.
   at ReactiveUI.Reflection.<>c__DisplayClass4_0.<GetValueFetcherForProperty>b__0(Object obj, Object[] _) in D:\a\ReactiveUI\ReactiveUI\src\ReactiveUI\Expression\Reflection.cs:line 131
   at ReactiveUI.Reflection.TryGetAllValuesForPropertyChain(IObservedChange`2[]& changeValues, Object current, IEnumerable`1 expressionChain) in D:\a\ReactiveUI\ReactiveUI\src\ReactiveUI\Expression\Reflection.cs:line 288
   at ReactiveUI.PropertyBinderImplementation.<>c__DisplayClass12_0`2.<EvalBindingHooks>b__2() in D:\a\ReactiveUI\ReactiveUI\src\ReactiveUI\Bindings\Property\PropertyBinderImplementation.cs:line 397
   at Avalonia.ReactiveUI.AutoDataTemplateBindingHook.ExecuteHook(Object source, Object target, Func`1 getCurrentViewModelProperties, Func`1 getCurrentViewProperties, BindingDirection direction) in /_/src/Avalonia.ReactiveUI/AutoDataTemplateBindingHook.cs:line 37
   at ReactiveUI.PropertyBinderImplementation.<>c__DisplayClass12_0`2.<EvalBindingHooks>b__3(Boolean acc, IPropertyBindingHook x) in D:\a\ReactiveUI\ReactiveUI\src\ReactiveUI\Bindings\Property\PropertyBinderImplementation.cs:line 402
   at System.Linq.Enumerable.Aggregate[TSource,TAccumulate](IEnumerable`1 source, TAccumulate seed, Func`3 func)
   at ReactiveUI.PropertyBinderImplementation.EvalBindingHooks[TViewModel,TView](TViewModel viewModel, TView view, Expression vmExpression, Expression viewExpression, BindingDirection direction) in D:\a\ReactiveUI\ReactiveUI\src\ReactiveUI\Bindings\Property\PropertyBinderImplementation.cs:line 401
   at ReactiveUI.PropertyBinderImplementation.OneWayBind[TViewModel,TView,TVMProp,TVProp](TViewModel viewModel, TView view, Expression`1 vmProperty, Expression`1 viewProperty, Object conversionHint, IBindingTypeConverter vmToViewConverterOverride) in D:\a\ReactiveUI\ReactiveUI\src\ReactiveUI\Bindings\Property\PropertyBinderImplementation.cs:line 182
   at ReactiveUI.PropertyBindingMixins.OneWayBind[TViewModel,TView,TVMProp,TVProp](TView view, TViewModel viewModel, Expression`1 vmProperty, Expression`1 viewProperty, Object conversionHint, IBindingTypeConverter vmToViewConverterOverride) in D:\a\ReactiveUI\ReactiveUI\src\ReactiveUI\Bindings\Property\PropertyBindingMixins.cs:line 276
   at Reative.Views.MainWindow.<.ctor>b__0_0(CompositeDisposable disposables) in /home/fqye/projects/projects/study/Reative/Views/MainWindow.axaml.cs:line 48
   at ReactiveUI.ViewForMixins.<>c__DisplayClass9_0.<WhenActivated>b__0() in D:\a\ReactiveUI\ReactiveUI\src\ReactiveUI\Activation\ViewForMixins.cs:line 230
   at ReactiveUI.ViewForMixins.<>c__DisplayClass10_0.<HandleViewActivation>b__0(Boolean activated) in D:\a\ReactiveUI\ReactiveUI\src\ReactiveUI\Activation\ViewForMixins.cs:line 247
   at System.Reactive.AnonymousSafeObserver`1.OnNext(T value) in /_/Rx.NET/Source/src/System.Reactive/AnonymousSafeObserver.cs:line 43
   at System.Reactive.Sink`1.ForwardOnNext(TTarget value) in /_/Rx.NET/Source/src/System.Reactive/Internal/Sink.cs:line 49
   at System.Reactive.Linq.ObservableImpl.DistinctUntilChanged`2._.OnNext(TSource value) in /_/Rx.NET/Source/src/System.Reactive/Linq/Observable/DistinctUntilChanged.cs:line 72
   at System.Reactive.Sink`1.ForwardOnNext(TTarget value) in /_/Rx.NET/Source/src/System.Reactive/Internal/Sink.cs:line 49
   at System.Reactive.Linq.ObservableImpl.Merge`1.Observables._.InnerObserver.OnNext(TSource value) in /_/Rx.NET/Source/src/System.Reactive/Linq/Observable/Merge.cs:line 238
   at System.Reactive.Sink`1.ForwardOnNext(TTarget value) in /_/Rx.NET/Source/src/System.Reactive/Internal/Sink.cs:line 49
   at System.Reactive.Linq.ObservableImpl.Select`2.Selector._.OnNext(TSource value) in /_/Rx.NET/Source/src/System.Reactive/Linq/Observable/Select.cs:line 47
   at System.Reactive.Subjects.Subject`1.OnNext(T value) in /_/Rx.NET/Source/src/System.Reactive/Subjects/Subject.cs:line 145
   at System.Reactive.Linq.ObservableImpl.FromEventPattern.Impl`2.<>c__DisplayClass3_0.<GetHandler>b__0(Object sender, TEventArgs eventArgs) in /_/Rx.NET/Source/src/System.Reactive/Linq/Observable/FromEventPattern.cs:line 44
   at Avalonia.Controls.TopLevel.OnOpened(EventArgs e) in /_/src/Avalonia.Controls/TopLevel.cs:line 472
   at Avalonia.Controls.Window.ShowCore(Window parent) in /_/src/Avalonia.Controls/Window.cs:line 707
   at Avalonia.Controls.Window.Show() in /_/src/Avalonia.Controls/Window.cs:line 631
   at Avalonia.Controls.ApplicationLifetimes.ClassicDesktopStyleApplicationLifetime.ShowMainWindow() in /_/src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs:line 129
   at Avalonia.Controls.ApplicationLifetimes.ClassicDesktopStyleApplicationLifetime.Start(String[] args) in /_/src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs:line 118
   at Avalonia.ClassicDesktopStyleApplicationLifetimeExtensions.StartWithClassicDesktopLifetime[T](T builder, String[] args, ShutdownMode shutdownMode) in /_/src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs:line 209

To Reproduce

Clone this repo:

https://github.com/lanyusan/avalonia-bug-1

run: dotnet run

Expected behavior

Program launched successfully.

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information): Linux Ubuntun 22.04

timunie commented 2 years ago

Hey @lanyusan

I think this ReactiveUI bindings don't work in Avalonia. If I make an Avalonia-binding it works for me, see https://docs.avaloniaui.net/docs/data-binding/binding-from-code#using-xaml-bindings-from-code for docs about it.

I send you a PR, so you can try it on your own: https://github.com/lanyusan/avalonia-bug-1/pull/1

Happy coding Tim

lanyusan commented 2 years ago

@timunie

Thanks. It works but it is pretty clumsy.

I would suggest the TEAM to fix the problem of binding by name specified in xamal or make it clear in documentation that it has to use this.FindControl to locate control.

Personally I would much appreciate a fix as it makes much more senses.

timunie commented 2 years ago

this.FindControl is needed because the mame was not resolved in the constructor. I agree that this maybe is a bug with the names generator. The other thing is the binding, which is not supported the way you did it I guess. I'm not sure if that will ever work tbh.