Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/ReactiveUI/Bindings/Property/PropertyBinderImplementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -596,30 +596,30 @@ private bool EvalBindingHooks<TViewModel, TView>(TViewModel viewModel, TView vie
signalInitialUpdate.Select(_ => true),
signalObservable);

var changeWithValues = somethingChanged.Select(isVm =>
var changeWithValues = somethingChanged.Select<bool, (bool isValid, object view, bool isViewModel)>(isVm =>
{
if (!Reflection.TryGetValueForPropertyChain(out TVMProp vmValue, view.ViewModel, vmExpression.GetExpressionChain()) ||
!Reflection.TryGetValueForPropertyChain(out TVProp vValue, view, viewExpression.GetExpressionChain()))
{
return null;
return (false, null, false);
}

if (isVm)
{
if (!vmToViewConverter(vmValue, out TVProp vmAsView) || EqualityComparer<TVProp>.Default.Equals(vValue, vmAsView))
{
return null;
return (false, null, false);
}

return Tuple.Create((object)vmAsView, isVm);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just syntactic sugar to remove a Tuple instance for the more efficient ValueTuple.

return (true, (object)vmAsView, isVm);
}

if (!viewToVmConverter(vValue, out TVMProp vAsViewModel) || EqualityComparer<TVMProp>.Default.Equals(vmValue, vAsViewModel))
{
return null;
return (false, null, false);
}

return Tuple.Create((object)vAsViewModel, isVm);
return (true, (object)vAsViewModel, isVm);
});

var ret = EvalBindingHooks(viewModel, view, vmExpression, viewExpression, BindingDirection.TwoWay);
Expand All @@ -628,7 +628,10 @@ private bool EvalBindingHooks<TViewModel, TView>(TViewModel viewModel, TView vie
return null;
}

IObservable<(object view, bool isViewModel)> changes = changeWithValues.Where(value => value != null).Select(value => (value.Item1, value.Item2)).Publish().RefCount();
IObservable<(object view, bool isViewModel)> changes = changeWithValues
.Where(value => value.isValid)
.Select(value => (value.view, value.isViewModel))
.Publish().RefCount();

IDisposable disposable = changes.Subscribe(isVmWithLatestValue =>
{
Expand Down
10 changes: 7 additions & 3 deletions src/ReactiveUI/Platforms/android/AndroidObservableForWidgets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,14 @@ private static DispatchItem CreateFromWidget<TView, TEventArgs>(Expression<Func<
{
var view = (TView)x;

return Observable.FromEvent<EventHandler<TEventArgs>, TEventArgs>(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the cause of the error. The event handlers weren't being set properly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Essentially, there was a mismatch between the EventHandler and Rx would cause an exception.

Now we are explicitly specifying the event handler method which is more efficient since it avoids reflection anyway.

return Observable.FromEvent<EventHandler<TEventArgs>, ObservedChange<object, object>>(
eventHandler =>
{
void Handler(object sender, TEventArgs e) => eventHandler(new ObservedChange<object, object>(view, ex));
return Handler;
},
h => addHandler(view, h),
h => removeHandler(view, h))
.Select(_ => new ObservedChange<object, object>(view, ex));
h => removeHandler(view, h));
}
};
}
Expand Down