我在设计器中遇到了这个奇怪的错误。
Visual Studio 2013 WPF - 其他 View /窗口工作正常。
当我有设计时数据时会发生这种情况(如果我删除设计时数据,它可以正常工作)。
System.NullReferenceException Object reference not set to an instance of an object.
at System.Windows.Controls.GridViewHeaderRowPresenter.OnLayoutUpdated(Object sender, EventArgs e)
at System.Windows.ContextLayoutManager.fireLayoutUpdateEvent()
at System.Windows.ContextLayoutManager.UpdateLayout()
at System.Windows.UIElement.UpdateLayout()
at System.Windows.Interop.HwndSource.SetLayoutSize()
at System.Windows.Interop.HwndSource.set_RootVisualInternal(Visual value)
at System.Windows.Interop.HwndSource.set_RootVisual(Visual value)
at Microsoft.Expression.DesignHost.Isolation.Remoting.RemoteUIElement.<>c__DisplayClass10.b__f()
at Microsoft.Expression.DesignHost.Isolation.Remoting.ThreadMarshaler.<>c__DisplayClass2a`1.b__29()
at Microsoft.Expression.DesignHost.Isolation.Remoting.STAMarshaler.Call.InvokeWorker()
at Microsoft.Expression.DesignHost.Isolation.Remoting.STAMarshaler.Call.Invoke(Boolean waitingInExternalCall)
at< Microsoft.Expression.DesignHost.Isolation.Remoting.STAMarshaler.InvokeCall(Call call)
at Microsoft.Expression.DesignHost.Isolation.Remoting.STAMarshaler.ProcessQueue(CallQueue queue)
at Microsoft.Expression.DesignHost.Isolation.Remoting.STAMarshaler.ProcessInboundAsyncQueue(Int32 identity)
at Microsoft.Expression.DesignHost.Isolation.Remoting.STAMarshaler.ProcessMessage(Int32 msg, IntPtr wParam, IntPtr lParam, Boolean elevatedQuery, Boolean& handled)
at Microsoft.Expression.DesignHost.Isolation.Remoting.STAMarshaler.OnWindowMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at Microsoft.Expression.DesignHost.Isolation.Remoting.MessageOnlyHwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.Run()
at System.Windows.Application.RunDispatcher(Object ignore)
at System.Windows.Application.RunInternal(Window window)
at System.Windows.Application.Run(Window window)
at Microsoft.Expression.DesignHost.Isolation.DesignerProcess.RunApplication()
at Microsoft.Expression.DesignHost.Isolation.DesignerProcess.<>c__DisplayClass2.b__0()
at System.Threading.ThreadHelper.ThreadStart_Context(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 System.Threading.ThreadHelper.ThreadStart()
错误图像
最佳答案
这是由于某些转换器未检查 null 造成的: 我添加了对 value == null 的检查,一切都很好。
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null) { return "White"; }
var valueAsString = (string)value;
if ( String.IsNullOrWhiteSpace(valueAsString) )
{
return "White";
}
if (valueAsString.ToLower().Contains("Late".ToLower()))
{
return "Yellow";
}
return "White";
}
MSDN 还提供了一些关于设计时错误的指示: http://msdn.microsoft.com/query/dev12.query?appId=Dev12IDEF1&l=EN-US&k=k(VS.XamlDesigner.ExceptionUI);k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4.0)&rd=true
值转换器 您的自定义 IValueConverter 实现应检查 Convert 方法的第一个参数中是否为 null 以及预期类型。以下 XAML 显示了与 Application.Current 的绑定(bind),如果未正确实现值转换器,该绑定(bind)将在设计时失败。
关于c# - WPF Visual Studio 设计器中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21094549/