我们正在使用Xamarin Forms框架开发移动应用程序(目前仅针对Android)。我们构建了我们的应用程序,以便在MVVMLight工具包的帮助下利用MVVM模式。对于数据管理,我们使用BreezeSharp(因为我们已经为Web客户端实现了Breeze友好服务器)。
我们正在处理的问题是在输入绑定到Breeze实体属性的Xamarin Forms Entry控件时应用程序崩溃。
Xaml视图中的条目控制:
<Entry Text="{Binding SelectedCustomerAddress.AddressLine1}" />
ViewModel中的属性(Set()来自MVVMLight ViewModelBase):
public CustomerAddress SelectedCustomerAddress
{
get { return this.selectedCustomerAddress; }
set { this.Set(() => this.SelectedCustomerAddress, ref this.selectedCustomerAddress, value);}
}
Breeze CLR中的属性类型:
public string AddressLine1
{
get { return this.GetValue<string>(); }
set { this.SetValue(value); }
}
在输入字段中仅键入几个字符后,应用程序将因堆栈跟踪而崩溃:
11-10 11:51:02.896 F/ ( 4924): * Assertion at /Users/builder/data/lanes/monodroid-mlion-monodroid-4.18-series/3b7ef0a7/source/mono/mono/metadata/sgen-stw.c:68, condition `info->stack_start >= info->stack_start_limit && info->stack_start < info->stack_end' not met
11-10 11:51:02.900 E/mono-rt ( 4924): Stacktrace:
11-10 11:51:02.900 E/mono-rt ( 4924):
11-10 11:51:02.904 E/mono-rt ( 4924): at <unknown> <0xffffffff>
11-10 11:51:02.908 E/mono-rt ( 4924): at (wrapper managed-to-native) object.MemberwiseClone (object) <IL 0x0002d, 0xffffffff>
11-10 11:51:02.912 E/mono-rt ( 4924): at System.Delegate.Clone () <IL 0x00001, 0x00027>
11-10 11:51:02.916 E/mono-rt ( 4924): at System.MulticastDelegate.CombineImpl (System.Delegate) <IL 0x000be, 0x0037c>
11-10 11:51:02.916 E/mono-rt ( 4924): at System.Delegate.Combine (System.Delegate,System.Delegate) <IL 0x00062, 0x001f8>
11-10 11:51:02.920 E/mono-rt ( 4924): at Breeze.Sharp.EntityAspect.add_EntityPropertyChanged (System.ComponentModel.PropertyChangedEventHandler) <IL 0x0000b, 0x00057>
11-10 11:51:02.924 E/mono-rt ( 4924): at Breeze.Sharp.BaseEntity.System.ComponentModel.INotifyPropertyChanged.add_PropertyChanged (System.ComponentModel.PropertyChangedEventHandler) [0x00000] in c:\GitHub\breeze.sharp\Breeze.Sharp\BaseEntity.cs:74
11-10 11:51:02.928 E/mono-rt ( 4924): at Xamarin.Forms.BindingExpression.ApplyCore (object,Xamarin.Forms.BindableObject,Xamarin.Forms.BindableProperty,bool) <IL 0x00130, 0x0077f>
11-10 11:51:02.928 E/mono-rt ( 4924): at Xamarin.Forms.BindingExpression.Apply (bool) <IL 0x00041, 0x000f3>
11-10 11:51:02.932 E/mono-rt ( 4924): at Xamarin.Forms.BindingExpression/BindingExpressionPart.<PropertyChanged>b__12 () <IL 0x00007, 0x0002f>
11-10 11:51:02.932 E/mono-rt ( 4924): at Java.Lang.Thread/RunnableImplementor.Run () [0x0000b] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.18-series/3b7ef0a7/source/monodroid/src/Mono.Android/src/Java.Lang/Thread.cs:36
11-10 11:51:02.932 E/mono-rt ( 4924): at Java.Lang.IRunnableInvoker.n_Run (intptr,intptr) [0x00009] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.18-series/3b7ef0a7/source/monodroid/src/Mono.Android/platforms/android-18/src/generated/Java.Lang.IRunnable.cs:71
11-10 11:51:02.932 E/mono-rt ( 4924): at (wrapper dynamic-method) object.39beb2b5-65b4-431f-a21a-3e626049fa1f (intptr,intptr) <IL 0x00011, 0x0001b>
11-10 11:51:02.932 E/mono-rt ( 4924): at (wrapper native-to-managed) object.39beb2b5-65b4-431f-a21a-3e626049fa1f (intptr,intptr) <IL 0x00022, 0xffffffff>
11-10 11:51:02.936 E/mono-rt ( 4924): at <unknown> <0xffffffff>
11-10 11:51:02.936 E/mono-rt ( 4924): at (wrapper managed-to-native) object.wrapper_native_0xb60a0eb0 (intptr,intptr,intptr,Android.Runtime.JValue[]) <IL 0x00120, 0xffffffff>
11-10 11:51:02.936 E/mono-rt ( 4924): at Android.Runtime.JNIEnv.CallVoidMethod (intptr,intptr,Android.Runtime.JValue[]) [0x00040] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.18-series/3b7ef0a7/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:504
11-10 11:51:02.940 E/mono-rt ( 4924): at Android.App.Activity.RunOnUiThread (Java.Lang.IRunnable) [0x0002d] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.18-series/3b7ef0a7/source/monodroid/src/Mono.Android/platforms/android-18/src/generated/Android.App.Activity.cs:4130
11-10 11:51:02.940 E/mono-rt ( 4924): at Android.App.Activity.RunOnUiThread (System.Action) [0x00000] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.18-series/3b7ef0a7/source/monodroid/src/Mono.Android/src/Android.App/Activity.cs:23
11-10 11:51:02.940 E/mono-rt ( 4924): at Xamarin.Forms.Forms/AndroidPlatformServices.BeginInvokeOnMainThread (System.Action) <IL 0x0000b, 0x00053>
11-10 11:51:02.940 E/mono-rt ( 4924): at Xamarin.Forms.Device.BeginInvokeOnMainThread (System.Action) <IL 0x00006, 0x00038>
11-10 11:51:02.940 E/mono-rt ( 4924): at Xamarin.Forms.BindingExpression/BindingExpressionPart.PropertyChanged (object,System.ComponentModel.PropertyChangedEventArgs) <IL 0x00087, 0x00313>
11-10 11:51:02.944 E/mono-rt ( 4924): at (wrapper delegate-invoke) <Module>.invoke_void_object_PropertyChangedEventArgs (object,System.ComponentModel.PropertyChangedEventArgs) <IL 0x00027, 0x00042>
11-10 11:51:02.944 E/mono-rt ( 4924): at (wrapper delegate-invoke) <Module>.invoke_void_object_PropertyChangedEventArgs (object,System.ComponentModel.PropertyChangedEventArgs) <IL 0x00027, 0x00042>
11-10 11:51:02.944 E/mono-rt ( 4924): at (wrapper delegate-invoke) <Module>.invoke_void_object_PropertyChangedEventArgs (object,System.ComponentModel.PropertyChangedEventArgs) <IL 0x00027, 0x00042>
..... (2500 lines)
11-10 11:51:03.564 E/mono-rt ( 4924): at (wrapper delegate-invoke) <Module>.invoke_void_object_PropertyChangedEventArgs (object,System.ComponentModel.PropertyChangedEventArgs) <IL 0x00059, 0xffffffff>
11-10 11:51:03.564 E/mono-rt ( 4924): at Breeze.Sharp.EntityAspect.OnPropertyChangedCore (System.ComponentModel.PropertyChangedEventArgs) [0x0000b] in c:\GitHub\breeze.sharp\Breeze.Sharp\EntityAspect.cs:1315
11-10 11:51:03.564 E/mono-rt ( 4924): at Breeze.Sharp.EntityAspect/<>c__DisplayClass4b.<OnPropertyChanged>b__4a () [0x00000] in c:\GitHub\breeze.sharp\Breeze.Sharp\EntityAspect.cs:1306
11-10 11:51:03.564 E/mono-rt ( 4924): at Breeze.Sharp.EntityAspect.QueueEvent (System.Action) [0x0003e] in c:\GitHub\breeze.sharp\Breeze.Sharp\EntityAspect.cs:1362
11-10 11:51:03.564 E/mono-rt ( 4924): at Breeze.Sharp.EntityAspect.OnPropertyChanged (System.ComponentModel.PropertyChangedEventArgs) [0x0003f] in c:\GitHub\breeze.sharp\Breeze.Sharp\EntityAspect.cs:1305
11-10 11:51:03.564 E/mono-rt ( 4924): at Breeze.Sharp.EntityAspect.SetValueWithEvents<T> (T,object,System.Action`3<T, object, object>) [0x00042] in c:\GitHub\breeze.sharp\Breeze.Sharp\EntityAspect.cs:563
11-10 11:51:03.564 E/mono-rt ( 4924): at Breeze.Sharp.EntityAspect.SetDpValue (Breeze.Sharp.DataProperty,object) [0x00000] in c:\GitHub\breeze.sharp\Breeze.Sharp\EntityAspect.cs:529
11-10 11:51:03.564 E/mono-rt ( 4924): at Breeze.Sharp.EntityAspect.SetValue (Breeze.Sharp.StructuralProperty,object) [0x00052] in c:\GitHub\breeze.sharp\Breeze.Sharp\EntityAspect.cs:522
11-10 11:51:03.564 E/mono-rt ( 4924): at Breeze.Sharp.EntityAspect.SetValue (string,object) [0x00031] in c:\GitHub\breeze.sharp\Breeze.Sharp\EntityAspect.cs:507
11-10 11:51:03.564 E/mono-rt ( 4924): at Breeze.Sharp.BaseEntity.SetValue (object,string) [0x00000] in c:\GitHub\breeze.sharp\Breeze.Sharp\BaseEntity.cs:37
11-10 11:51:03.564 E/mono-rt ( 4924): at Client.Common.Model.Proxy.CustomerAddress.set_AddressLine2 (string) [0x00001] in c:\Projects\Client\source\Client.Common\Model\Proxy\CustomerAddress.cs:26
11-10 11:51:03.564 E/mono-rt ( 4924): at (wrapper runtime-invoke) <Module>.runtime_invoke_void__this___object (object,intptr,intptr,intptr) <IL 0x00052, 0xffffffff>
11-10 11:51:03.564 E/mono-rt ( 4924): at <unknown> <0xffffffff>
11-10 11:51:03.564 E/mono-rt ( 4924): at (wrapper managed-to-native) System.Reflection.MonoMethod.InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&) <IL 0x00030, 0xffffffff>
11-10 11:51:03.564 E/mono-rt ( 4924): at System.Reflection.MonoMethod.Invoke (object,System.Reflection.BindingFlags,System.Reflection.Binder,object[],System.Globalization.CultureInfo) <IL 0x0004a, 0x0016f>
11-10 11:51:03.564 E/mono-rt ( 4924): at System.Reflection.MethodBase.Invoke (object,object[]) <IL 0x00006, 0x0004b>
11-10 11:51:03.564 E/mono-rt ( 4924): at Xamarin.Forms.BindingExpression.ApplyCore (object,Xamarin.Forms.BindableObject,Xamarin.Forms.BindableProperty,bool) <IL 0x002f4, 0x00fd3>
11-10 11:51:03.564 E/mono-rt ( 4924): at Xamarin.Forms.BindingExpression.Apply (bool) <IL 0x00041, 0x000f3>
11-10 11:51:03.564 E/mono-rt ( 4924): at Xamarin.Forms.Binding.Apply (bool) <IL 0x00027, 0x000db>
11-10 11:51:03.564 E/mono-rt ( 4924): at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty,object,bool,bool,bool) <IL 0x00090, 0x00295>
11-10 11:51:03.564 E/mono-rt ( 4924): at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty,object,bool,bool,bool) <IL 0x00213, 0x007b7>
11-10 11:51:03.564 E/mono-rt ( 4924): at Xamarin.Forms.Platform.Android.EntryRenderer.Android.Text.ITextWatcher.OnTextChanged (Java.Lang.ICharSequence,int,int,int) <IL 0x00014, 0x00087>
11-10 11:51:03.564 E/mono-rt ( 4924): at Android.Text.ITextWatcherInvoker.n_OnTextChanged_Ljava_lang_CharSequence_III (intptr,intptr,intptr,int,int,int) [0x00011] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.18-series/3b7ef0a7/source/monodroid/src/Mono.Android/platforms/android-18/src/generated/Android.Text.ITextWatcher.cs:149
11-10 11:51:03.564 E/mono-rt ( 4924): at (wrapper dynamic-method) object.369fe85e-8bc0-4aec-88da-b1540ec93f9c (intptr,intptr,intptr,int,int,int) <IL 0x00029, 0x0003f>
11-10 11:51:03.564 E/mono-rt ( 4924): at (wrapper native-to-managed) object.369fe85e-8bc0-4aec-88da-b1540ec93f9c (intptr,intptr,intptr,int,int,int) <IL 0x00028, 0xffffffff>
11-10 11:51:03.564 E/mono-rt ( 4924):
11-10 11:51:03.564 E/mono-rt ( 4924): =================================================================
11-10 11:51:03.564 E/mono-rt ( 4924): Got a SIGSEGV while executing native code. This usually indicates
11-10 11:51:03.564 E/mono-rt ( 4924): a fatal error in the mono runtime or one of the native libraries
11-10 11:51:03.564 E/mono-rt ( 4924): used by your application.
11-10 11:51:03.564 E/mono-rt ( 4924): =================================================================
11-10 11:51:03.564 E/mono-rt ( 4924):
11-10 11:51:03.564 F/libc ( 4924): Fatal signal 11 (SIGSEGV) at 0xdeadbaad (code=1), thread 4924 (r.nimbus.mobile)
The program 'Mono' has exited with code 0 (0x0).
在我看来,很多事件被触发,导致事件堆栈溢出。
绑定到非Breeze属性时,条目正常工作。绑定到分离实体时,它也可以正常工作。
在深入了解Breeze EntityAspect.cs代码之后,我发现所有这些都来自这个函数:
internal void OnPropertyChanged(PropertyChangedEventArgs pcArgs)
{
if (this.IsDetached || !this.EntityGroup.ChangeNotificationEnabled)
return;
pcArgs = pcArgs ?? EntityAspect.AllPropertiesChangedEventArgs;
this.QueueEvent((Action) (() =>
{
this.OnPropertyChangedCore(pcArgs);
this.OnEntityChangedCore(EntityAction.PropertyChange, (EventArgs) pcArgs);
}));
}
所以我禁止在Breeze中使用以下方式举办活动:
this.EntityManager.ChangeNotificationEnabled = false;
这似乎解决了我们的条目绑定问题,但我们不确定这是否为其他问题打开了大门。
这种方法的后果是什么?
这些EntityPropertyChanged和PropertyChanged事件是由Breeze代码使用还是仅供我们的代码附加在它们上面?
或者您是否看到了解决此问题的其他方法?
更新: 后果是:如果我们在UI上有一个元素绑定到同一个属性,并且如果我们在一个地方更新该属性,则更改在其他地方不可见。
示例:从ListView中选择元素并将所选元素绑定到某些输入字段(ListView外部)。当我们通过该输入字段更新元素时,更改在ListView上不可见(因为ListView未被通知)。
启用Breeze PropertyChanged机制(EntityPropertyChanged和ForceEntityPropertyChanged),这将开箱即用,但正如我上面提到的那样,应用程序崩溃存在问题。
我们最终在我们的基础实体(继承自Breeze BaseEntity的实体)中实现了OnPropertyChanged和RaisePropertyChanged方法。此技术类似于ObservableObject的MVVMLight实现。
此代码工作正常(没有应用程序崩溃):
public bool IsImportant
{
get { return this.GetValue<bool>(); }
set
{
this.SetValue(value);
this.OnPropertyChanged();
}
}
我们也可以在需要时调用RaisePropertyChanged:
this.SelectedNote.RaisePropertyChanged("IsImportant");
答案 0 :(得分:0)
我猜测只有在用户完成文本编辑后才能修改绑定,而不是每次更改字符。这对于您尝试使用您的应用程序完成的任务是否足够?在WPF / Silverlight XAML中,绑定上存在一个名为UpdateSourceTrigger的属性,您可以设置该属性来指示在何时推送更改。我正在联系Xamarin以了解他们是否拥有同等财产。
答案 1 :(得分:0)
没有测试,但您可以在EntityManager
上停用属性通知,这样您就可以根据需要切换此属性。