I am using 17.2 and have following code,
try { string error = null; this.DataGridControl.LoadCustomizations(fs, true, out error, CustomizationType.None); } catch(Exception ex) { //ignore exception caused by loading xml file, like invalid element etc }
I have manually changed xml element (<fieldLayouts> to <fieldLayouts1>) in saved xml file created by SaveCustomizations(fs, true) and then loaded with above code. As expected I can see error message as below, and no exception catch from above.
The 'fieldLayouts1' start tag on line 3 position 4 does not match the end tag of 'fieldLayouts'. Line 71, position 5.
Issue is after exit from above code and keep running there is an unhandled exception as below, and I have no way to catch it.
System.Reflection.TargetInvocationException HResult=0x80131604 Message=Exception has been thrown by the target of an invocation. Source=mscorlib StackTrace: at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) at System.Delegate.DynamicInvokeImpl(Object[] args) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler) at System.Windows.Threading.DispatcherOperation.InvokeImpl() at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext executionContext, ContextCallback callback, Object state) at System.Windows.Threading.DispatcherOperation.Invoke() at System.Windows.Threading.Dispatcher.ProcessQueue() at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler) at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs) at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) Inner Exception 1: InvalidOperationException: Invalid root element in LoadCustomizations.
Can anyone tell me although I have returned error message from LoadCustomizations why exception isn't handled internally by XamDataGrid control? Thanks!
Hello Jiyang,
I have been investigating into the behavior you are seeing, and I believe I am able to see the exception that you are receiving, but this exception appears to be a first-chance exception, and will not happen if you do not have a checkmark placed to break on it in your Common Language Runtime Exceptions option of the Debug => Exception Settings in Visual Studio. This exception can be continued through, and you will not see it if running without the debugger.
Just to ensure that we are indeed seeing the same behavior, I am attaching the sample project I used to test this. It will be helpful if you can run this sample on your end and verify that it is the same behavior. I created this sample project against version 17.2.20172.2143 of Infragistics for WPF 2017.2. Is this the same version you are using?
Please let me know if you have any other questions or concerns on this matter.
XDGSaveLoadExceptionCase.zip
My version is 17.2.20172.1000. I don't have any exception in following code, but once my application is out of that block I got exception immediately and my application got crashed.
private void OnFieldLayoutInitialized(object sender, Infragistics.Windows.DataPresenter.Events.FieldLayoutInitializedEventArgs e) { if (File.Exists(gridLayoutFile)) { using (FileStream fs = new FileStream(gridLayoutFile, FileMode.Open, FileAccess.Read)) { if (fs.Length > 0) { try { string error = null; this.DataGridControl.LoadCustomizations(fs, true, out error, CustomizationType.None); } catch { //ignore exception caused by loading xml file, like invalid element etc } } } } }
OnFieldLayoutInitialized is added as below,
protected override void OnInitialized(EventArgs e) { base.OnInitialized(e); this.DataGridControl.FieldLayoutInitialized += new EventHandler<Infragistics.Windows.DataPresenter.Events.FieldLayoutInitializedEventArgs>(OnFieldLayoutInitialized); }
When it crashed stack is as below,
System.InvalidOperationException HResult=0x80131509 Message=Invalid root element in LoadCustomizations. Source=InfragisticsWPF.DataPresenter StackTrace: at Infragistics.Windows.DataPresenter.CustomizationsManager.ApplyCustomizations() at Infragistics.Windows.DataPresenter.FieldLayout.InitializeFields(Object listObject, IEnumerable containingCollection, RecordCollectionBase parentRecordCollection, PropertyDescriptorProvider propertyDescriptorProvider) at Infragistics.Windows.DataPresenter.DataRecord.Create(RecordCollectionBase parentCollection, Object listObject, Boolean isAddRecord, Int32 listIndex, Boolean& newRecordCreated) at Infragistics.Windows.DataPresenter.RecordManager.OnSourceCollectionReset() at Infragistics.Windows.DataPresenter.RecordManager.PostDelayedReset() at Infragistics.Windows.DataPresenter.RecordManager.SetDataSource(IEnumerable currentValue, Boolean postReset) at Infragistics.Windows.DataPresenter.RecordManager.set_DataSource(IEnumerable value) at Infragistics.Windows.DataPresenter.DataPresenterBase.VerifyRecordManagerDataSource() at Infragistics.Windows.DataPresenter.DataPresenterBase.InitializeRecordManagerDataSource() at Infragistics.Windows.DataPresenter.DataPresenterBase.get_CurrentRecordListControl() at Infragistics.Windows.DataPresenter.DataPresenterBase.VerifyContentSiteGrid() at Infragistics.Windows.DataPresenter.DataPresenterBase.OnApplyTemplate() at System.Windows.FrameworkElement.ApplyTemplate() at System.Windows.FrameworkElement.MeasureCore(Size availableSize) at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Controls.Grid.MeasureOverride(Size constraint) at System.Windows.FrameworkElement.MeasureCore(Size availableSize) at System.Windows.UIElement.Measure(Size availableSize) at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint) at System.Windows.Controls.ContentPresenter.MeasureOverride(Size constraint) at System.Windows.FrameworkElement.MeasureCore(Size availableSize) at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Controls.DockPanel.MeasureOverride(Size constraint) at System.Windows.FrameworkElement.MeasureCore(Size availableSize) at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Controls.Border.MeasureOverride(Size constraint) at System.Windows.FrameworkElement.MeasureCore(Size availableSize) at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Controls.Control.MeasureOverride(Size constraint) at System.Windows.FrameworkElement.MeasureCore(Size availableSize) at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Controls.Grid.MeasureOverride(Size constraint) at System.Windows.FrameworkElement.MeasureCore(Size availableSize) at System.Windows.UIElement.Measure(Size availableSize) at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint) at System.Windows.Controls.ContentPresenter.MeasureOverride(Size constraint) at System.Windows.FrameworkElement.MeasureCore(Size availableSize) at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Controls.Border.MeasureOverride(Size constraint) at System.Windows.FrameworkElement.MeasureCore(Size availableSize) at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Controls.Control.MeasureOverride(Size constraint) at System.Windows.FrameworkElement.MeasureCore(Size availableSize) at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV) at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV, Boolean& hasDesiredSizeUChanged) at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV) at System.Windows.Controls.Grid.MeasureOverride(Size constraint) at System.Windows.FrameworkElement.MeasureCore(Size availableSize) at System.Windows.UIElement.Measure(Size availableSize) at System.Windows.ContextLayoutManager.UpdateLayout() at System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg) at System.Windows.Media.MediaContext.InvokeOnRenderCallback.DoWork() at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks() at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget) at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler) at System.Windows.Threading.DispatcherOperation.InvokeImpl() at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext executionContext, ContextCallback callback, Object state) at System.Windows.Threading.DispatcherOperation.Invoke() at System.Windows.Threading.Dispatcher.ProcessQueue() at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler) at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs) at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
After i modified your application with adding the same event and I got the exactly same crash and stack trace. Any idea? Thanks!
I have tested against both 17.2.20172.1000 and the latest 17.2.20172.2143, and I am able to reproduce this exception that you are seeing with both. It appears that this is mainly due to the LoadCustomizations being called in the FieldLayoutInitialized event handler. I have found that you can work around this issue by placing your code in a Dispatcher.BeginInvoke action. The code for this would look like the following:
Dispatcher.BeginInvoke(new Action(() => { //your code here…});
Doing this results in the original exception, which is a first chance, internally handled exception.
The InvalidOperationException you are seeing is unexpected behavior, and as such, I have asked our development teams to investigate it further. To ensure this receives attention, I have logged it in our internal tracking systems with a development ID of 255992. I have also created you a private support case, CAS-197225-X0H8J1, which I will link to the development issue so you can be notified when a fix becomes available. You can access the case here.
As suggested I modified code as below, but I don't think it is gonna work since error message shows "Cannot access closed stream". That means xml file isn't loaded at all to get rid of unhandled exception. It isn't a workaround.
private void OnFieldLayoutInitialized(object sender, Infragistics.Windows.DataPresenter.Events.FieldLayoutInitializedEventArgs e) { if (File.Exists(fileName)) { try { using (FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { Dispatcher.BeginInvoke(new Action(() => { string error = null; xdg1.LoadCustomizations(fs, true, out error, Infragistics.Windows.DataPresenter.CustomizationType.None); })); } } catch (Exception ex) { } } }
The Action inside Dispatcher.BeginInvoke should include the entire try catch block and probably even the if statement.
The reason that you are getting the can not access a close stream message is because the using block is completed and the stream is closed before the method that is invoked by BeginInvoke is called. As such the using statement needs to be within the method for the BeginInvoke call so that the FileStream is created and available when you load the customizations.