wpf 应用程序在调试模式下运行,但没有调试就无法运行

wpf 应用程序在调试模式下运行,但没有调试就无法运行

问题描述:

我的 WPF 应用程序在调试模式下在 VS2015 中成功运行.但是,在没有调试的情况下启动时,应用程序启动并立即完成.从 Debug/Release 文件夹启动 exe 文件时也会发生同样的情况.事件查看器显示以下 .Net 运行时错误:

My WPF application successfully runs in VS2015 in debug mode. However when starting without debugging application starts and immediately finishes. The same happens when starting exe file from the Debug/Release folder. Event Viewer shows the following .Net Runtime Error:

应用:Cisco.exe 框架版本:v4.0.30319 描述:由于未处理的异常,进程被终止.异常信息:System.InvalidOperationException 在MS.Internal.Data.PropertyPathWorker.CheckReadOnly(System.Object,系统对象)在MS.Internal.Data.PropertyPathWorker.ReplaceItem(Int32, System.Object,系统对象)在MS.Internal.Data.PropertyPathWorker.UpdateSourceValueState(Int32,System.ComponentModel.ICollectionView, System.Object, Boolean) atMS.Internal.Data.ClrBindingWorker.AttachDataItem() 在System.Windows.Data.BindingExpression.Activate(System.Object) 在System.Windows.Data.BindingExpression.AttachToContext(AttachAttempt)在System.Windows.Data.BindingExpression.MS.Internal.Data.IDataBindEngineClient.AttachToContext(Boolean)在 MS.Internal.Data.DataBindEngine+Task.Run(Boolean) 在MS.Internal.Data.DataBindEngine.Run(System.Object) 在System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate,System.Object, Int32) atSystem.Windows.Threading.ExceptionWrapper.TryCatchWhen(System.Object,System.Delegate, System.Object, Int32, System.Delegate) atSystem.Windows.Threading.DispatcherOperation.InvokeImpl() 在System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(System.Object)在MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(System.Object)在System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext,System.Threading.ContextCallback, System.Object, Boolean) atSystem.Threading.ExecutionContext.Run(System.Threading.ExecutionContext,System.Threading.ContextCallback, System.Object, Boolean) atSystem.Threading.ExecutionContext.Run(System.Threading.ExecutionContext,System.Threading.ContextCallback, System.Object) 在MS.Internal.CulturePreservingExecutionContext.Run(MS.Internal.CulturePreservingExecutionContext,System.Threading.ContextCallback, System.Object) 在System.Windows.Threading.DispatcherOperation.Invoke() 在System.Windows.Threading.Dispatcher.ProcessQueue() 在System.Windows.Threading.Dispatcher.WndProcHook(IntPtr, Int32, IntPtr,IntPtr, Boolean ByRef) 在 MS.Win32.HwndWrapper.WndProc(IntPtr, Int32,IntPtr, IntPtr, Boolean ByRef) atMS.Win32.HwndSubclass.DispatcherCallbackOperation(System.Object) 在System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate,System.Object, Int32) atSystem.Windows.Threading.ExceptionWrapper.TryCatchWhen(System.Object,System.Delegate, System.Object, Int32, System.Delegate) atSystem.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority,System.TimeSpan, System.Delegate, System.Object, Int32) atMS.Win32.HwndSubclass.SubclassWndProc(IntPtr, Int32, IntPtr, IntPtr)在MS.Win32.UnsafeNativeMethods.MessageBox(System.Runtime.InteropServices.HandleRef,System.String, System.String, Int32) 在System.Windows.MessageBox.ShowCore(IntPtr, System.String,System.String, System.Windows.MessageBoxButton,System.Windows.MessageBoxImage, System.Windows.MessageBoxResult,System.Windows.MessageBoxOptions) 在System.Windows.MessageBox.Show(System.String) 在Cisco.App.OnStartup(System.Windows.StartupEventArgs) 在System.Windows.Application.<.ctor>b__1_0(System.Object) atSystem.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate,System.Object, Int32) atSystem.Windows.Threading.ExceptionWrapper.TryCatchWhen(System.Object,System.Delegate, System.Object, Int32, System.Delegate) atSystem.Windows.Threading.DispatcherOperation.InvokeImpl() 在System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(System.Object)在MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(System.Object)在System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext,System.Threading.ContextCallback, System.Object, Boolean) atSystem.Threading.ExecutionContext.Run(System.Threading.ExecutionContext,System.Threading.ContextCallback, System.Object, Boolean) atSystem.Threading.ExecutionContext.Run(System.Threading.ExecutionContext,System.Threading.ContextCallback, System.Object) 在MS.Internal.CulturePreservingExecutionContext.Run(MS.Internal.CulturePreservingExecutionContext,System.Threading.ContextCallback, System.Object) 在System.Windows.Threading.DispatcherOperation.Invoke() 在System.Windows.Threading.Dispatcher.ProcessQueue() 在System.Windows.Threading.Dispatcher.WndProcHook(IntPtr, Int32, IntPtr,IntPtr, Boolean ByRef) 在 MS.Win32.HwndWrapper.WndProc(IntPtr, Int32,IntPtr, IntPtr, Boolean ByRef) atMS.Win32.HwndSubclass.DispatcherCallbackOperation(System.Object) 在System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate,System.Object, Int32) atSystem.Windows.Threading.ExceptionWrapper.TryCatchWhen(System.Object,System.Delegate, System.Object, Int32, System.Delegate) atSystem.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority,System.TimeSpan, System.Delegate, System.Object, Int32) atMS.Win32.HwndSubclass.SubclassWndProc(IntPtr, Int32, IntPtr, IntPtr)在MS.Win32.UnsafeNativeMethods.DispatchMessage(System.Windows.Interop.MSGByRef) 在System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame)在System.Windows.Threading.Dispatcher.PushFrame(System.Windows.Threading.DispatcherFrame)在 System.Windows.Application.RunDispatcher(System.Object) 在System.Windows.Application.RunInternal(System.Windows.Window) 在System.Windows.Application.Run(System.Windows.Window) 在Cisco.App.Main()

Application: Cisco.exe Framework Version: v4.0.30319 Description: The process was terminated due to an unhandled exception. Exception Info: System.InvalidOperationException at MS.Internal.Data.PropertyPathWorker.CheckReadOnly(System.Object, System.Object) at MS.Internal.Data.PropertyPathWorker.ReplaceItem(Int32, System.Object, System.Object) at MS.Internal.Data.PropertyPathWorker.UpdateSourceValueState(Int32, System.ComponentModel.ICollectionView, System.Object, Boolean) at MS.Internal.Data.ClrBindingWorker.AttachDataItem() at System.Windows.Data.BindingExpression.Activate(System.Object) at System.Windows.Data.BindingExpression.AttachToContext(AttachAttempt) at System.Windows.Data.BindingExpression.MS.Internal.Data.IDataBindEngineClient.AttachToContext(Boolean) at MS.Internal.Data.DataBindEngine+Task.Run(Boolean) at MS.Internal.Data.DataBindEngine.Run(System.Object) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate) at System.Windows.Threading.DispatcherOperation.InvokeImpl() at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(System.Object) at MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(System.Object) at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object) at MS.Internal.CulturePreservingExecutionContext.Run(MS.Internal.CulturePreservingExecutionContext, System.Threading.ContextCallback, System.Object) at System.Windows.Threading.DispatcherOperation.Invoke() at System.Windows.Threading.Dispatcher.ProcessQueue() at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef) at MS.Win32.HwndWrapper.WndProc(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef) at MS.Win32.HwndSubclass.DispatcherCallbackOperation(System.Object) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate) at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority, System.TimeSpan, System.Delegate, System.Object, Int32) at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr, Int32, IntPtr, IntPtr) at MS.Win32.UnsafeNativeMethods.MessageBox(System.Runtime.InteropServices.HandleRef, System.String, System.String, Int32) at System.Windows.MessageBox.ShowCore(IntPtr, System.String, System.String, System.Windows.MessageBoxButton, System.Windows.MessageBoxImage, System.Windows.MessageBoxResult, System.Windows.MessageBoxOptions) at System.Windows.MessageBox.Show(System.String) at Cisco.App.OnStartup(System.Windows.StartupEventArgs) at System.Windows.Application.<.ctor>b__1_0(System.Object) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate) at System.Windows.Threading.DispatcherOperation.InvokeImpl() at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(System.Object) at MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(System.Object) at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object) at MS.Internal.CulturePreservingExecutionContext.Run(MS.Internal.CulturePreservingExecutionContext, System.Threading.ContextCallback, System.Object) at System.Windows.Threading.DispatcherOperation.Invoke() at System.Windows.Threading.Dispatcher.ProcessQueue() at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef) at MS.Win32.HwndWrapper.WndProc(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef) at MS.Win32.HwndSubclass.DispatcherCallbackOperation(System.Object) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate) at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority, System.TimeSpan, System.Delegate, System.Object, Int32) at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr, Int32, IntPtr, IntPtr) at MS.Win32.UnsafeNativeMethods.DispatchMessage(System.Windows.Interop.MSG ByRef) at System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame) at System.Windows.Threading.Dispatcher.PushFrame(System.Windows.Threading.DispatcherFrame) at System.Windows.Application.RunDispatcher(System.Object) at System.Windows.Application.RunInternal(System.Windows.Window) at System.Windows.Application.Run(System.Windows.Window) at Cisco.App.Main()

我知道有很多关于同样问题的问题,但还没有解决方案解决我的问题.

I know that there are many questions with the same problem, but no solution has fixed my problem yet.

在运行期间 MainWindow 甚至不出现.因此,我将所有 OnStartup 方法移至 try catch 块,尝试使用 MessageBox 识别此异常,但 MessageBox 也没有出现.

During run MainWindow even doesn't appear. So I moved all OnStartup method to the try catch block trying to identify this exception with the MessageBox but MessageBox doesn't appear too.

    public partial class App : Application
{
    void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
    {
        e.Handled = true;
    }


    protected override void OnStartup(StartupEventArgs e)
    {
        try
        {
            base.OnStartup(e);

            Ioc.Setup();

            Current.MainWindow = new MainWindow();

            Current.MainWindow.Show();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}

我已将完整的解决方案上传到谷歌驱动器.也许它会更有用.

I've uploaded the full solution to the google drive. Maybe it would be more useful.

终于找到原因了.问题与使用私有 setter 绑定到公共变量的 twoWay 相关.使此 setter 公共应用程序成功运行后.

Finally I've found the cause. The problem was related to the twoWay binding to a public variable with a private setter. After making this setter public application runs successfully.