gpt4 book ai didi

c# - MvvmCross - 在 UISlider 中处理绑定(bind)时获取 TargetInvocationException 和 SIGABRT

转载 作者:太空宇宙 更新时间:2023-11-03 20:56:46 25 4
gpt4 key购买 nike

我正在使用 Xamarin.iOS 和 MvvmCross (v5.6.3) 开发一个项目,我在一个 View Controller 上有一个非常典型的 UISlider,其值绑定(bind)到其 View 上的 float 属性-模型。

set.Bind(MySlider).For(x => x.Value).To(vm => vm.FloatProperty).TwoWay();

当 View 卸载并尝试处理现有的绑定(bind)时(比如导航到另一个 View 模型时),我收到以下未处理的异常和 SIGABRT:

Unhandled Exception:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> UIKit.UIKitThreadAccessException: UIKit Consistency error: you are calling a UIKit method that can only be invoked from the UI thread.
at UIKit.UIApplication.EnsureUIThread () [0x00023] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.9.1.24/src/Xamarin.iOS/UIKit/UIApplication.cs:88
at UIKit.UIControl.RemoveTarget (Foundation.NSObject target, System.IntPtr sel, UIKit.UIControlEvent events) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.9.1.24/src/Xamarin.iOS/UIKit/UIControl.g.cs:235
at UIKit.UIControl.RemoveTarget (System.EventHandler notification, UIKit.UIControlEvent events) [0x00057] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.9.1.24/src/Xamarin.iOS/UIKit/UIControl.cs:116
at UIKit.UIControl.remove_ValueChanged (System.EventHandler value) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.9.1.24/src/Xamarin.iOS/UIKit/UIControl.cs:209
at (wrapper managed-to-native) System.Reflection.MonoMethod.InternalInvoke(System.Reflection.MonoMethod,object,object[],System.Exception&)
at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00032] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.9.1.24/src/Xamarin.iOS/mcs/class/corlib/System.Reflection/MonoMethod.cs:305
--- End of inner exception stack trace ---
at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00046] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.9.1.24/src/Xamarin.iOS/mcs/class/corlib/System.Reflection/MonoMethod.cs:313
at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.9.1.24/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/reflection/methodbase.cs:229
at MvvmCross.Platform.WeakSubscription.MvxWeakEventSubscription`1[TSource].RemoveEventHandler () [0x00024] in <6adc0d5857264558a9d45778a78ae02a>:0
at MvvmCross.Platform.WeakSubscription.MvxWeakEventSubscription`1[TSource].Dispose (System.Boolean disposing) [0x00003] in <6adc0d5857264558a9d45778a78ae02a>:0
at MvvmCross.Platform.WeakSubscription.MvxWeakEventSubscription`1[TSource].Dispose () [0x00000] in <6adc0d5857264558a9d45778a78ae02a>:0
at MvvmCross.Binding.iOS.Target.MvxUISliderValueTargetBinding.Dispose (System.Boolean isDisposing) [0x0001b] in <614c9ef828c14ba687a40ec2656f480f>:0
at MvvmCross.Binding.Bindings.MvxBinding.Finalize () [0x00000] in <866b1e46764b48aab0d408952a6f006f>:0
2018-04-09 23:15:20.210 MyProject.iOS[73770:3302608] Unhandled managed exception:
Exception has been thrown by the target of an invocation. (System.Reflection.TargetInvocationException)
at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00046] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.9.1.24/src/Xamarin.iOS/mcs/class/corlib/System.Reflection/MonoMethod.cs:313
at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.9.1.24/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/reflection/methodbase.cs:229
at MvvmCross.Platform.WeakSubscription.MvxWeakEventSubscription`1[TSource].RemoveEventHandler () [0x00024] in <6adc0d5857264558a9d45778a78ae02a>:0
at MvvmCross.Platform.WeakSubscription.MvxWeakEventSubscription`1[TSource].Dispose (System.Boolean disposing) [0x00003] in <6adc0d5857264558a9d45778a78ae02a>:0
at MvvmCross.Platform.WeakSubscription.MvxWeakEventSubscription`1[TSource].Dispose () [0x00000] in <6adc0d5857264558a9d45778a78ae02a>:0
at MvvmCross.Binding.iOS.Target.MvxUISliderValueTargetBinding.Dispose (System.Boolean isDisposing) [0x0001b] in <614c9ef828c14ba687a40ec2656f480f>:0
at MvvmCross.Binding.Bindings.MvxBinding.Finalize () [0x00000] in <866b1e46764b48aab0d408952a6f006f>:0
--- inner exception ---
UIKit Consistency error: you are calling a UIKit method that can only be invoked from the UI thread. (UIKit.UIKitThreadAccessException)
at UIKit.UIApplication.EnsureUIThread () [0x00023] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.9.1.24/src/Xamarin.iOS/UIKit/UIApplication.cs:88
at UIKit.UIControl.RemoveTarget (Foundation.NSObject target, System.IntPtr sel, UIKit.UIControlEvent events) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.9.1.24/src/Xamarin.iOS/UIKit/UIControl.g.cs:235
at UIKit.UIControl.RemoveTarget (System.EventHandler notification, UIKit.UIControlEvent events) [0x00057] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.9.1.24/src/Xamarin.iOS/UIKit/UIControl.cs:116
at UIKit.UIControl.remove_ValueChanged (System.EventHandler value) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.9.1.24/src/Xamarin.iOS/UIKit/UIControl.cs:209
at (wrapper managed-to-native) System.Reflection.MonoMethod.InternalInvoke(System.Reflection.MonoMethod,object,object[],System.Exception&)
at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00032] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.9.1.24/src/Xamarin.iOS/mcs/class/corlib/System.Reflection/MonoMethod.cs:305
2018-04-09 23:15:20.210 MyProject.iOS[73770:3302608] critical: Stacktrace:

2018-04-09 23:15:20.211 MyProject.iOS[73770:3302608] critical:
Native stacktrace:

2018-04-09 23:15:20.212 MyProject.iOS[73770:3302608] critical: 0 MyProject.iOS 0x00000001038b4574 mono_handle_native_crash + 244
2018-04-09 23:15:20.212 MyProject.iOS[73770:3302608] critical: 1 libsystem_platform.dylib 0x000000010bbecf5a _sigtramp + 26
2018-04-09 23:15:20.213 MyProject.iOS[73770:3302608] critical: 2 ??? 0x0000000103dd308a 0x0 + 4359794826
2018-04-09 23:15:20.213 MyProject.iOS[73770:3302608] critical: 3 libsystem_c.dylib 0x000000010b8820eb abort + 127
2018-04-09 23:15:20.213 MyProject.iOS[73770:3302608] critical: 4 MyProject.iOS 0x0000000103a5b2df xamarin_unhandled_exception_handler + 47
2018-04-09 23:15:20.214 MyProject.iOS[73770:3302608] critical: 5 MyProject.iOS 0x000000010391b854 mono_invoke_unhandled_exception_hook + 148
2018-04-09 23:15:20.214 MyProject.iOS[73770:3302608] critical: 6 MyProject.iOS 0x00000001039c2a3e mono_thread_internal_unhandled_exception + 110
2018-04-09 23:15:20.215 MyProject.iOS[73770:3302608] critical: 7 MyProject.iOS 0x000000010391d15a mono_gc_run_finalize + 842
2018-04-09 23:15:20.215 MyProject.iOS[73770:3302608] critical: 8 MyProject.iOS 0x00000001039fc9ba sgen_gc_invoke_finalizers + 234
2018-04-09 23:15:20.215 MyProject.iOS[73770:3302608] critical: 9 MyProject.iOS 0x000000010391eae4 finalizer_thread + 756
2018-04-09 23:15:20.215 MyProject.iOS[73770:3302608] critical: 10 MyProject.iOS 0x00000001039c3600 start_wrapper + 704
2018-04-09 23:15:20.215 MyProject.iOS[73770:3302608] critical: 11 libsystem_pthread.dylib 0x000000010bbfe6c1 _pthread_body + 340
2018-04-09 23:15:20.216 MyProject.iOS[73770:3302608] critical: 12 libsystem_pthread.dylib 0x000000010bbfe56d _pthread_body + 0
2018-04-09 23:15:20.216 MyProject.iOS[73770:3302608] critical: 13 libsystem_pthread.dylib 0x000000010bbfdc5d thread_start + 13
2018-04-09 23:15:20.216 MyProject.iOS[73770:3302608] critical:
=================================================================
Got a SIGABRT while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries
used by your application.
=================================================================

绑定(bind)工作正常,只有当我尝试导航到另一个 View 模型 正在卸载 View 时才会发生这种情况。

我错过了什么吗?或者这可能是 MvvmCross 上的错误?

编辑:最后检查了卸载 View 时是否会出现此问题,无论我是否导航到另一个 View 模型。我删除了指向导航问题的引用。

最佳答案

目前 MvvmCross 5.x.x 中存在一个关于 UISlider 的错误。 PR 2750 已解决此问题它将在 6.0.0 版本中发布

与此同时,您应该能够覆盖绑定(bind)以通过使用修复创建自定义绑定(bind)来为您的项目修复它。问题在 Dispose .

public class FixedMvxUISliderValueTargetBinding : MvxPropertyInfoTargetBinding<UISlider>
{
private IDisposable _subscription;

public FixedMvxUISliderValueTargetBinding(object target, PropertyInfo targetPropertyInfo)
: base(target, targetPropertyInfo) { }

protected override void SetValueImpl(object target, object value)
{
var view = target as UISlider;
if (view == null) return;
view.Value = (float)value;
}

private void HandleSliderValueChanged(object sender, EventArgs e)
{
var view = View;
if (view == null) return;
FireValueChanged(view.Value);
}

public override MvxBindingMode DefaultMode => MvxBindingMode.TwoWay;

public override void SubscribeToEvents()
{
var slider = View;
if (slider == null)
{
MvxBindingTrace.Trace(
MvxTraceLevel.Error,
"Error - UISlider is null in MvxUISliderValueTargetBinding");
return;
}
_subscription = slider.WeakSubscribe(
nameof(slider.ValueChanged),
HandleSliderValueChanged);
}

protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
if (isDisposing)
{
_subscription?.Dispose();
_subscription = null;
}
}
}

然后在您的 Setup.cs 中注册绑定(bind)。请注意,确保先调用 base.FillTargetFactories

protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry)
{
base.FillTargetFactories(registry);
registry.RegisterPropertyInfoBindingFactory(
typeof(FixedMvxUISliderValueTargetBinding),
typeof(UISlider),
nameof(UISlider.Value));
}

关于c# - MvvmCross - 在 UISlider 中处理绑定(bind)时获取 TargetInvocationException 和 SIGABRT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49742011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com