【发布时间】:2015-11-28 06:59:56
【问题描述】:
我创建了一个使用 Catel、Entity Framework 6 和 Syncfusion Community Edition 的项目。具体来说,我使用的是 Syncfusion 的 sfDataGrid。
我遇到了一个让我完全难过的问题,也许有人可以为我指出正确的方向。
我有一个带有 sfDataGrid 的 Catel UserControl。 ViewModel 具有我用作 sfDataGrid 的 ItemsSource 的数据的 ObservableCollection(Catel 属性)。我在 sfDataGrid 上启用了过滤。这是 sfDataGrid 的代码:
<sync:SfDataGrid
x:Name="ItemsDataGrid"
ItemsSource="{Binding MyItems, Mode=OneWay}"
SelectedItem="{Binding MyItem, Mode=TwoWay}"
AutoGenerateColumns="False"
AllowFiltering="True"
AllowResizingColumns="True"
GridCopyOption="IncludeHeaders"
GridPasteOption="None"
ShowGroupDropArea="True"
ShowRowHeader="True"
DataFetchSize="50">
<!-- Columns -->
</sync:SfDataGrid>
我在不同视图上向集合中添加一个新项目,然后将其保存到数据上下文并刷新列表:
private async void OnNewMyItemExecute()
{
MyItem= new MyItem();
MyItemsRepository.Add(MyItem);
var myItemsViewModel = TypeFactory
.Default
.CreateInstanceWithParametersAndAutoCompletion<myItemsViewModel >(MyItem); //the construction has parameters for IUIVisualizerService, IPleaseWaitService, etc.
if (await _UIVisualizerService.ShowDialogAsync(myItemsViewModel) ?? true)
{
var myItem= myItemsViewModel.MyItem;
await InitializeAsync();
MyItem = myItem;
}
else
{
_UnitOfWorkService.Rollback(); //just undo some changes
MyItem = null;
}
UnitOfWork.SaveChanges();
}
创建一个新项目根本不是问题,奇怪的是,在我创建了一个新项目之后,然后尝试在 sfDataGrid 的任何列上过滤网格(单击列标题中的过滤按钮,一个过滤器弹出窗口打开,在条件文本框中输入一个值,然后单击“确定”),我明白了:
MyProject.vshost.exe 警告:0 : 17:57:49:075 => [警告] [Catel.Services.UIVisualizerService] [8] 发生错误,返回 null 因为我们不知道结果 | [目标调用异常] System.Reflection.TargetInvocationException:已引发异常 通过调用的目标。 ---> System.Reflection.TargetInvocationException:已引发异常 通过调用的目标。 ---> System.InvalidCastException: 无法将“MyProject.Domain.MyItem”类型的对象转换为类型 'System.Data.Entity.DynamicProxies.MyItem_4CA6F59AB53174892ED9EFBC1D3CC07B862579FC3C5733C6CF4DE857907CFF9C'。 在 lambda_method(Closure , MyItem) --- 内部异常结束 堆栈跟踪 --- 在 System.RuntimeMethodHandle.InvokeMethod(Object 目标、Object[] 参数、签名 sig、布尔构造函数)在 System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(对象 obj, Object[] 参数,Object[] 参数)在 System.Delegate.DynamicInvokeImpl(Object[] args) 在 System.Delegate.DynamicInvoke(Object[] args) 在 Syncfusion.Data.QueryableCollectionView.c__DisplayClass3.b__1(Object o) 在 Syncfusion.Data.QueryableCollectionView.FilterRecord(Object 记录)在 Syncfusion.Data.EnumerableRecordsWrapper..ctor(IEnumerable 源, CollectionViewAdv 视图)在 Syncfusion.Data.EnumerableRecordsWrapper.CreateNew(IEnumerable 源, CollectionViewAdv 视图)在 Syncfusion.Data.QueryableCollectionView.CreateRecords() 在 Syncfusion.Data.CollectionViewAdv.EnsureInitialized() 在 Syncfusion.Data.CollectionViewAdv.get_Records() 在 Syncfusion.Data.CollectionViewAdv.EnsureRecordsInitialized() 在 Syncfusion.Data.CollectionViewAdv.Refresh() 在 Syncfusion.Data.QueryableCollectionView.RefreshFilter() 在 Syncfusion.Data.CollectionViewAdv.ApplyFilters() 在 Syncfusion.Data.CollectionViewAdv.set_FilterPredicates(ObservableCollection
1 value) at Syncfusion.UI.Xaml.Grid.GridModel.FilterColumn(GridColumn column, List1 filterPredicates) 在 Syncfusion.UI.Xaml.Grid.GridFilterControl.RefreshFilter() 在 Syncfusion.UI.Xaml.Grid.GridFilterControl.b__10(对象 s, RunWorkerCompletedEventArgs e) 在 System.ComponentModel.BackgroundWorker.OnRunWorkerCompleted(RunWorkerCompletedEventArgs 吃 System.ComponentModel.BackgroundWorker.AsyncOperationCompleted(对象 arg)在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托 回调,对象参数,Int32 numArgs)在 System.Windows.Threading.ExceptionWrapper.TryCatchWhen(对象源, 委托回调、对象 args、Int32 numArgs、委托 catchHandler) 在 System.Windows.Threading.DispatcherOperation.InvokeImpl() 在 System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(对象 州)在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext、ContextCallback 回调、对象状态、布尔值 preserveSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext、ContextCallback 回调、对象状态、布尔值 preserveSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext、ContextCallback 回调、对象状态)在 System.Windows.Threading.DispatcherOperation.Invoke() 在 System.Windows.Threading.Dispatcher.ProcessQueue() 在 System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd,Int32 味精,IntPtr wParam,IntPtr lParam,布尔值&处理)在 MS.Win32.HwndWrapper.WndProc(IntPtr hwnd,Int32 msg,IntPtr wParam, IntPtr lParam,布尔值&处理)在 MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) 在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托 回调,对象参数,Int32 numArgs)在 System.Windows.Threading.ExceptionWrapper.TryCatchWhen(对象源, 委托回调、对象 args、Int32 numArgs、委托 catchHandler) 在 System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority 优先级、TimeSpan 超时、委托方法、对象参数、Int32 numArgs) 在 MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 消息,IntPtr wParam,IntPtr lParam)在 MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) 在 System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame 帧)在 System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame 框架)
在 System.Windows.Window.ShowHelper(Object booleanBox) 在 System.Windows.Window.Show() 在 System.Windows.Window.ShowDialog() --- 内部异常堆栈跟踪结束 --- 在 System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] 参数,签名 sig,布尔构造函数)在 System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(对象 obj, Object[] 参数,Object[] 参数)在 System.Reflection.RuntimeMethodInfo.Invoke(对象 obj,BindingFlags invokeAttr、Binder binder、Object[] 参数、CultureInfo 文化)
在 System.Reflection.MethodBase.Invoke(对象 obj,对象 [] 参数)在 Catel.Services.UIVisualizerService.c__DisplayClass23_0.b__0() 在 C:\CI_WS\Ws\86058\Source\Catel\src\Catel.MVVM\Catel.MVVM.Shared\Services\UIVisualizerService.cs:line 499
这是 UIVisualizerService 第 499 行的方法:
protected virtual bool? ShowWindow(FrameworkElement window, bool showModal)
{
if (showModal)
{
var showDialogMethodInfo = window.GetType().GetMethodEx("ShowDialog");
if (showDialogMethodInfo != null)
{
// Child window does not have a ShowDialog, so not null is allowed
bool? result = null;
window.Dispatcher.InvokeIfRequired(() =>
{
// Safety net to prevent crashes when this is the main window
try
{
result = showDialogMethodInfo.Invoke(window, null) as bool?; //Line 499
}
catch (Exception ex)
{
Log.Warning(ex, "An error occurred, returning null since we don't know the result");
}
});
return result;
}
Log.Warning("Method 'ShowDialog' not found on '{0}', falling back to 'Show'", window.GetType().Name);
}
我曾尝试使用 .ShowAsync()、.ShowDialogAsync() 打开 MyItemsView,在升级到 Catel 4.4 之前,我还使用了 .ShowDialog(),但没有任何区别。我可以在 EF 中关闭代理创建,但这会导致我必须在我的项目中进行很多更改(我不再需要延迟加载,这不是一个选项)。我确实相信问题是我添加的新项目不是动态代理,并且让它成为动态代理可能会解决问题,但我已经尝试再次从 DbContext 加载所有数据,但它也没有解决问题。
任何建议将不胜感激。
【问题讨论】:
标签: entity-framework-6 catel syncfusion