After upgrading to Xamarin.Forms 4.3 from the previous stable (4.2...) Loading up some of my pages and setting the binding context I get a crash with the following error:
System.Exception: 'Element not found.
Element not found.
'
at Windows.ApplicationModel.Core.CoreApplication.GetCurrentView()
at Xamarin.Forms.Platform.UWP.Dispatcher..ctor()
at Xamarin.Forms.Platform.UWP.DispatcherProvider.GetDispatcher(Object context)
at Xamarin.Forms.DispatcherExtensions.GetDispatcher(BindableObject bindableObject)
at Xamarin.Forms.BindableObject.get_Dispatcher()
at Xamarin.Forms.BindingExpression.BindingExpressionPart.PropertyChanged(Object sender, PropertyChangedEventArgs args)
at Xamarin.Forms.BindingExpression.WeakPropertyChangedProxy.OnPropertyChanged(Object sender, PropertyChangedEventArgs e)
at Xamarin.Forms.BindingExpression.WeakPropertyChangedProxy.OnBCChanged(Object sender, EventArgs e)
at System.EventHandler.Invoke(Object sender, EventArgs e)
at Xamarin.Forms.BindableObject.OnBindingContextChanged()
at Xamarin.Forms.Element.OnBindingContextChanged()
at Xamarin.Forms.View.OnBindingContextChanged()
at Xamarin.Forms.Grid.OnBindingContextChanged()
at Xamarin.Forms.BindableObject.SetInheritedBindingContext(BindableObject bindable, Object value)
at Xamarin.Forms.Element.SetChildInheritedBindingContext(Element child, Object context)
at Xamarin.Forms.Element.b__82_0(BindableObject child, Object bc)
at Xamarin.Forms.BindableObjectExtensions.PropagateBindingContextT
at Xamarin.Forms.Element.OnBindingContextChanged()
at Xamarin.Forms.View.OnBindingContextChanged()
at Xamarin.Forms.BindableObject.SetInheritedBindingContext(BindableObject bindable, Object value)
at Xamarin.Forms.TemplatedView.SetChildInheritedBindingContext(Element child, Object context)
at Xamarin.Forms.Element.b__82_0(BindableObject child, Object bc)
at Xamarin.Forms.BindableObjectExtensions.PropagateBindingContextT
at Xamarin.Forms.Element.OnBindingContextChanged()
at Xamarin.Forms.View.OnBindingContextChanged()
at Xamarin.Forms.ContentView.OnBindingContextChanged()
at Xamarin.Forms.BindableObject.SetInheritedBindingContext(BindableObject bindable, Object value)
at Xamarin.Forms.TemplatedView.SetChildInheritedBindingContext(Element child, Object context)
at Xamarin.Forms.Element.b__82_0(BindableObject child, Object bc)
at Xamarin.Forms.BindableObjectExtensions.PropagateBindingContextT
at Xamarin.Forms.Element.OnBindingContextChanged()
at Xamarin.Forms.View.OnBindingContextChanged()
at Xamarin.Forms.ContentView.OnBindingContextChanged()
at Xamarin.Forms.BindableObject.BindingContextPropertyChanged(BindableObject bindable, Object oldvalue, Object newvalue)
at Xamarin.Forms.BindableObject.SetValueActual(BindableProperty property, BindablePropertyContext context, Object value, Boolean currentlyApplying, SetValueFlags attributes, Boolean silent)
at Xamarin.Forms.BindableObject.SetValueCore(BindableProperty property, Object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes)
at Xamarin.Forms.BindableObject.SetValue(BindableProperty property, Object value, Boolean fromStyle, Boolean checkAccess)
at Xamarin.Forms.BindableObject.set_BindingContext(Object value)
The only time I've been able to replicate this is in my sample app if you turn on "break when thrown - common language runtime exceptions" but in my production app it does actually throw a real exception
notice this doesnt actually cause any crashes in the application but in my production app it does
Looking at this class:
https://github.com/xamarin/Xamarin.Forms/blob/7a9b54dae9605169f0417c168af2cd81f931b2a9/Xamarin.Forms.Platform.UAP/Dispatcher.cs
I came to find this SO which is probably linked
https://stackoverflow.com/questions/52648176/how-to-get-the-current-view-of-my-uwp-app-from-background-thread
Wrapping the line in InvokeOnMainThread
fixes the error. But running my production app this error is thrown constantly and crashes the application. any RaisePropertyChanged which is raised on a background thread seems to be throwing this exception.
I don't particularly want to change all raisepropertychanged to run on a mainthread as this will bottleneck my application. The UI and VM should be decoupled
I'm experiencing this same issue with RaisePropertyChanged. Invoking on main thread does fix it, but at a performance cost.
Same issue, it's a regression and blocks the update of XF 4.3 on UWP
Spot on! I spent an hour before googling the issue and coming across this bug report!
My solution is to run the code on UI thread as well but of course at performance cost.
@amirvenus the performance cost is so big. I wouldn't even call it a "Solution". I would just say UWP is unusable with 4.3.0.
Wrapping RaisePropertyChanged
in a RunOnUIThread
would completely bottleneck the whole application
Wrapping the line in InvokeOnMainThread fixes the error.
Wrapping which line?
@hartez The line in my production app.
I think this bug can be replicated with:
Run the following on UWP:
Task.Run(() => RaisePropertyChanged("PropertyThatIsBoundToTheView"));
a real world example would be something like:
Data = await Task.Run<Data>(() => GetData()).ConfigureAwait(false); //configure await causes the await to resume on any thread
RaisePropertyChanged("Data"); //crash on this line
Same issue here. I was trying to upgrade Xamarin.Forms to v4.3 to fix the bug regarding the wrong label height but actually it has breaking changes. I don't want to change each assignment to force it to return UI thread.
Most helpful comment
Wrapping the line in
InvokeOnMainThread
fixes the error. But running my production app this error is thrown constantly and crashes the application. any RaisePropertyChanged which is raised on a background thread seems to be throwing this exception.I don't particularly want to change all raisepropertychanged to run on a mainthread as this will bottleneck my application. The UI and VM should be decoupled