XAMLParseException 让我抓狂!
这个 XAMLParseException 让我抓狂!!我在 Visual Studio 1020 中使用 .NET 3.5 这是我的 xaml:
<Window x:Class="WinformsHost.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Advanced Notepad" Height="350" Width="525" Loaded="Window_Loaded">
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="File">
<MenuItem Header="_New" />
<MenuItem Header="_Open..." />
<MenuItem Header="_Save" />
<MenuItem Header="S_ave As..." />
<Separator />
<MenuItem Header="Page Setup" />
<MenuItem Header="Print" />
<Separator />
<MenuItem Header="Exit" />
</MenuItem>
<MenuItem Header="Edit">
<MenuItem Header="Undo" />
<MenuItem Header="Redo" />
<Separator />
<MenuItem Header="Cut" />
<MenuItem Header="Copy" />
<MenuItem Header="Paste" />
<MenuItem Header="Delete" />
<Separator />
<MenuItem Header="Time/Date" />
<Separator />
<MenuItem Header="Preferences" />
</MenuItem>
<MenuItem Header="Format">
<MenuItem Header="Word Wrap" IsCheckable="True" />
<MenuItem Header="Font..." />
</MenuItem>
<MenuItem Header="View">
<MenuItem Header="Status Bar" IsCheckable="True" />
</MenuItem>
<MenuItem Header="Language">
<RadioButton x:Name="rbtCmake" Checked="rbtCmake_Checked" Content="Cmake" />
<RadioButton x:Name="rbtCSharp" Content="C#" />
<RadioButton x:Name="rbtHTML" Content="HTML" />
<RadioButton x:Name="rbtPython" Content="Python" />
<Separator />
<RadioButton x:Name="rbtText" IsChecked="True" Content="Text" />
</MenuItem>
<MenuItem Header="Help">
<MenuItem Header="View Help" />
<Separator />
<MenuItem Header="About Notepad" />
</MenuItem>
</Menu>
<StatusBar x:Name="stbStatus" DockPanel.Dock="Bottom" Visibility="Collapsed">
<TextBlock x:Name="txtStatus" Text="Status" />
</StatusBar>
</DockPanel>
</Window>
那么它又可能是我的代码?这是例外情况:System.Windows.Markup.XamlParseException 未处理 消息=无法创建程序集“记事本,版本=1.0.0.0,文化=中性,PublicKeyToken=null”中定义的“MainWindow”实例。调用目标已引发异常。标记文件“MainWindow.xaml”第 1 行第 9 行出错。 来源=PresentationFramework 行号=1 线位置=9 堆栈跟踪: 在System.Windows.Markup.XamlParseException.ThrowException(字符串消息,异常innerException,Int32 lineNumber,Int32 linePosition,Uri baseUri,XamlObjectIds currentXamlObjectIds,XamlObjectIds contextXamlObjectIds,类型objectType) 在System.Windows.Markup.XamlParseException.ThrowException(ParserContext parserContext,Int32 lineNumber,Int32 linePosition,字符串消息,异常innerException) 在System.Windows.Markup.BamlRecordReader.ThrowExceptionWithLine(字符串消息,异常innerException) 在 System.Windows.Markup.BamlRecordReader.CreateInstanceFromType(类型类型,Int16 typeId,布尔值 throwOnFail) 在 System.Windows.Markup.BamlRecordReader.GetElementAndFlags(BamlElementStartRecord bamlElementStartRecord、Object&元素、ReaderFlags&标志、Type&delayCreatedType、Int16&delayCreatedTypeId) 在 System.Windows.Markup.BamlRecordReader.BaseReadElementStartRecord(BamlElementStartRecord bamlElementRecord) 在 System.Windows.Markup.BamlRecordReader.ReadElementStartRecord(BamlElementStartRecord bamlElementRecord) 在 System.Windows.Markup.BamlRecordReader.ReadRecord(BamlRecord bamlRecord) 在 System.Windows.Markup.BamlRecordReader.Read(布尔 singleRecord) 在 System.Windows.Markup.TreeBuilderBamlTranslator.ParseFragment() 在 System.Windows.Markup.TreeBuilder.Parse() 在 System.Windows.Markup.XamlReader.LoadBaml(流流,ParserContext parserContext,对象父级,布尔 closeStream) 在 System.Windows.Application.LoadBamlStreamWithSyncInfo(流流,ParserContext pc) 在System.Windows.Application.LoadComponent(Uri资源定位器,布尔bSkipJournaledProperties) 在 System.Windows.Application.DoStartup() 在 System.Windows.Application.<.ctor>b__0(对象未使用) 在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托回调,对象参数,布尔值 isSingleParameter) 在 System.Windows.Threading.ExceptionWrapper.TryCatchWhen(对象源,委托回调,对象参数,布尔 isSingleParameter,委托 catchHandler) 在 System.Windows.Threading.Dispatcher.WrappedInvoke(委托回调、对象参数、布尔 isSingleParameter、委托 catchHandler) 在 System.Windows.Threading.DispatcherOperation.InvokeImpl() 在 System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(对象状态) 在 System.Threading.ExecutionContext.runTryCode(对象 userData) 在System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode代码,CleanupCode backoutCode,对象userData) 在 System.Threading.ExecutionContext.RunInternal(ExecutionContextexecutionContext,ContextCallback 回调,对象状态) 在 System.Threading.ExecutionContext.Run(ExecutionContextexecutionContext,ContextCallback 回调,对象状态) 在 System.Windows.Threading.DispatcherOperation.Invoke() 在 System.Windows.Threading.Dispatcher.ProcessQueue() 在 System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd、Int32 msg、IntPtr wParam、IntPtr lParam、Boolean& 处理) 在MS.Win32.HwndWrapper.WndProc(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam,布尔值和处理) 在 MS.Win32.HwndSubclass.DispatcherCallbackOperation(对象 o) 在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托回调,对象参数,布尔值 isSingleParameter) 在 System.Windows.Threading.ExceptionWrapper.TryCatchWhen(对象源,委托回调,对象参数,布尔 isSingleParameter,委托 catchHandler) 在 System.Windows.Threading.Dispatcher.WrappedInvoke(委托回调、对象参数、布尔 isSingleParameter、委托 catchHandler) 在 System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority 优先级、TimeSpan 超时、委托方法、对象参数、布尔值 isSingleParameter) 在System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority优先级,委托方法,对象arg) 在MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam) 在 MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) 在System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame框架) 在System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame框架) 在 System.Windows.Threading.Dispatcher.Run() 在 System.Windows.Application.RunDispatcher(对象忽略) 在 System.Windows.Application.RunInternal(窗口窗口) 在 System.Windows.Application.Run(窗口窗口) 在 System.Windows.Application.Run() 在 C:\Users\Mohit\Documents\Visual Studio 2010\Projects\Notepad\Notepad\obj\x86\Debug\App.g.cs 中的 WinformsHost.App.Main() 处:第 0 行 在 System.AppDomain._nExecuteAssembly(程序集,String[] args) 在 System.AppDomain.ExecuteAssembly(字符串 assemblyFile,证据 assemblySecurity,字符串 [] args) 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在 System.Threading.ThreadHelper.ThreadStart_Context(对象状态) 在 System.Threading.ExecutionContext.Run(ExecutionContextexecutionContext,ContextCallback 回调,对象状态) 在 System.Threading.ThreadHelper.ThreadStart() 内部异常:System.Reflection.TargetInitationException Message=调用目标已引发异常。 来源=mscorlib 堆栈跟踪: 在 System.RuntimeTypeHandle.CreateInstance(RuntimeType 类型,布尔 publicOnly,布尔 noCheck,布尔& canBeCached,RuntimeMethodHandle& ctor,布尔& bNeedSecurityCheck) 在 System.RuntimeType.CreateInstanceSlow(布尔 publicOnly,布尔 fillCache) 在System.RuntimeType.CreateInstanceImpl(布尔publicOnly,布尔skipVisibilityChecks,布尔fillCache) 在 System.Activator.CreateInstance(类型类型,布尔非公共) 在 System.Windows.Markup.BamlRecordReader.CreateInstanceFromType(类型类型,Int16 typeId,布尔值 throwOnFail) 内部异常:NLog.Config.NLogConfigurationException 消息 = 从“C:\Users\Mohit\Documents\Visual Studio 2010\Projects\Notepad\Notepad\bin\Debug\NLog.config”加载配置时发生异常 来源=NLog 堆栈跟踪: 在 NLog.Config.XmlLoggingConfiguration..ctor(字符串文件名,布尔忽略错误) 在 NLog.Config.XmlLoggingConfiguration..ctor(String fileName) 在 NLog.LogFactory.get_Configuration() 在 NLog.LogFactory.GetLogger(LoggerCacheKey cacheKey) 在NLog.LogFactory.GetLogger(字符串名称) 在 NLog.LogManager.GetCurrentClassLogger() 在 C:\Users\Mohit\Documents\Visual Studio 2010\Projects\Notepad\Notepad\MainWindow.xaml.cs 中的 WinformsHost.MainWindow..ctor() 处:第 15 行 内部异常:NLog.Config.NLogConfigurationException 消息=未找到目标控制台。 来源=NLog 堆栈跟踪: 在 NLog.Config.XmlLoggingConfiguration.ConfigureRulesFromElement(LoggingConfiguration 配置、LoggingRuleCollection 规则、XmlElement 元素) 在NLog.Config.XmlLoggingConfiguration.ConfigureFromXmlElement(XmlElement configElement,字符串baseDirectory) 在 NLog.Config.XmlLoggingConfiguration.ConfigureFromFile(字符串文件名) 在 NLog.Config.XmlLoggingConfiguration..ctor(字符串文件名,布尔忽略错误) 内部异常:
THIS XAMLParseException IS DRIVING ME CRAZY!! I am using .NET 3.5 in Visual Studio 1020 This is my xaml:
<Window x:Class="WinformsHost.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Advanced Notepad" Height="350" Width="525" Loaded="Window_Loaded">
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="File">
<MenuItem Header="_New" />
<MenuItem Header="_Open..." />
<MenuItem Header="_Save" />
<MenuItem Header="S_ave As..." />
<Separator />
<MenuItem Header="Page Setup" />
<MenuItem Header="Print" />
<Separator />
<MenuItem Header="Exit" />
</MenuItem>
<MenuItem Header="Edit">
<MenuItem Header="Undo" />
<MenuItem Header="Redo" />
<Separator />
<MenuItem Header="Cut" />
<MenuItem Header="Copy" />
<MenuItem Header="Paste" />
<MenuItem Header="Delete" />
<Separator />
<MenuItem Header="Time/Date" />
<Separator />
<MenuItem Header="Preferences" />
</MenuItem>
<MenuItem Header="Format">
<MenuItem Header="Word Wrap" IsCheckable="True" />
<MenuItem Header="Font..." />
</MenuItem>
<MenuItem Header="View">
<MenuItem Header="Status Bar" IsCheckable="True" />
</MenuItem>
<MenuItem Header="Language">
<RadioButton x:Name="rbtCmake" Checked="rbtCmake_Checked" Content="Cmake" />
<RadioButton x:Name="rbtCSharp" Content="C#" />
<RadioButton x:Name="rbtHTML" Content="HTML" />
<RadioButton x:Name="rbtPython" Content="Python" />
<Separator />
<RadioButton x:Name="rbtText" IsChecked="True" Content="Text" />
</MenuItem>
<MenuItem Header="Help">
<MenuItem Header="View Help" />
<Separator />
<MenuItem Header="About Notepad" />
</MenuItem>
</Menu>
<StatusBar x:Name="stbStatus" DockPanel.Dock="Bottom" Visibility="Collapsed">
<TextBlock x:Name="txtStatus" Text="Status" />
</StatusBar>
</DockPanel>
</Window>
Then again it might be my code? Here is the exception:System.Windows.Markup.XamlParseException was unhandled
Message=Cannot create instance of 'MainWindow' defined in assembly 'Notepad, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Exception has been thrown by the target of an invocation. Error in markup file 'MainWindow.xaml' Line 1 Position 9.
Source=PresentationFramework
LineNumber=1
LinePosition=9
StackTrace:
at System.Windows.Markup.XamlParseException.ThrowException(String message, Exception innerException, Int32 lineNumber, Int32 linePosition, Uri baseUri, XamlObjectIds currentXamlObjectIds, XamlObjectIds contextXamlObjectIds, Type objectType)
at System.Windows.Markup.XamlParseException.ThrowException(ParserContext parserContext, Int32 lineNumber, Int32 linePosition, String message, Exception innerException)
at System.Windows.Markup.BamlRecordReader.ThrowExceptionWithLine(String message, Exception innerException)
at System.Windows.Markup.BamlRecordReader.CreateInstanceFromType(Type type, Int16 typeId, Boolean throwOnFail)
at System.Windows.Markup.BamlRecordReader.GetElementAndFlags(BamlElementStartRecord bamlElementStartRecord, Object& element, ReaderFlags& flags, Type& delayCreatedType, Int16& delayCreatedTypeId)
at System.Windows.Markup.BamlRecordReader.BaseReadElementStartRecord(BamlElementStartRecord bamlElementRecord)
at System.Windows.Markup.BamlRecordReader.ReadElementStartRecord(BamlElementStartRecord bamlElementRecord)
at System.Windows.Markup.BamlRecordReader.ReadRecord(BamlRecord bamlRecord)
at System.Windows.Markup.BamlRecordReader.Read(Boolean singleRecord)
at System.Windows.Markup.TreeBuilderBamlTranslator.ParseFragment()
at System.Windows.Markup.TreeBuilder.Parse()
at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
at System.Windows.Application.LoadBamlStreamWithSyncInfo(Stream stream, ParserContext pc)
at System.Windows.Application.LoadComponent(Uri resourceLocator, Boolean bSkipJournaledProperties)
at System.Windows.Application.DoStartup()
at System.Windows.Application.<.ctor>b__0(Object unused)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
at System.Windows.Threading.DispatcherOperation.InvokeImpl()
at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
at System.Threading.ExecutionContext.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext 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, Boolean isSingleParameter)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter)
at System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg)
at MS.Win32.HwndSubclass.SubclassWndProc(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 System.Windows.Application.Run()
at WinformsHost.App.Main() in C:\Users\Mohit\Documents\Visual Studio 2010\Projects\Notepad\Notepad\obj\x86\Debug\App.g.cs:line 0
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: System.Reflection.TargetInvocationException
Message=Exception has been thrown by the target of an invocation.
Source=mscorlib
StackTrace:
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandle& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean fillCache)
at System.RuntimeType.CreateInstanceImpl(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean fillCache)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.Windows.Markup.BamlRecordReader.CreateInstanceFromType(Type type, Int16 typeId, Boolean throwOnFail)
InnerException: NLog.Config.NLogConfigurationException
Message=Exception occured when loading configuration from 'C:\Users\Mohit\Documents\Visual Studio 2010\Projects\Notepad\Notepad\bin\Debug\NLog.config'
Source=NLog
StackTrace:
at NLog.Config.XmlLoggingConfiguration..ctor(String fileName, Boolean ignoreErrors)
at NLog.Config.XmlLoggingConfiguration..ctor(String fileName)
at NLog.LogFactory.get_Configuration()
at NLog.LogFactory.GetLogger(LoggerCacheKey cacheKey)
at NLog.LogFactory.GetLogger(String name)
at NLog.LogManager.GetCurrentClassLogger()
at WinformsHost.MainWindow..ctor() in C:\Users\Mohit\Documents\Visual Studio 2010\Projects\Notepad\Notepad\MainWindow.xaml.cs:line 15
InnerException: NLog.Config.NLogConfigurationException
Message=Target console not found.
Source=NLog
StackTrace:
at NLog.Config.XmlLoggingConfiguration.ConfigureRulesFromElement(LoggingConfiguration config, LoggingRuleCollection rules, XmlElement element)
at NLog.Config.XmlLoggingConfiguration.ConfigureFromXmlElement(XmlElement configElement, String baseDirectory)
at NLog.Config.XmlLoggingConfiguration.ConfigureFromFile(String fileName)
at NLog.Config.XmlLoggingConfiguration..ctor(String fileName, Boolean ignoreErrors)
InnerException:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不认为这实际上是一个 XamlParseException:我认为它是一个包含在 XamlParseException 中的 TargetInitationException。看看最终的内部异常:
看起来您有一个无效的配置文件,可能是由于无效的 XML 或可能由于某些更具体的原因。
当您收到神秘的 XamlParseException 时,总是值得检查 InnerException 中是否存在 TargetInitationException(如果存在,则从那里深入研究)。是的,这是非常令人困惑和误导的!
I don't think this is really a XamlParseException: I think it's a TargetInvocationException which has gotten wrapped in a XamlParseException. Take a look at the ultimate inner exception:
It looks like you have a borked configuration file, possibly due to invalid XML or possibly for some more specific reason.
When you get a mysterious XamlParseException, it's always worth checking the InnerException for a TargetInvocationException (and drilling in from there if there is one). Yes, it is very confusing and misleading!
我知道这是一个老问题,但希望这会对某人有所帮助。
我的主窗口的一个属性是业务对象...所以当窗口初始化时,业务对象尝试初始化...很好,但是数据访问层尚未设置,因此它引发了错误..所以
c# 称其为 XML 解析错误,因为它发生得太早了……实际上它是在代码中抛出的。
I know that this is an old question but hopefully this will help someone.
I had a property of the main window that was a Business Object... so when the window initialized, the Business Object attempted to initialize... great, but the Data access layer had not been setup, therefore it threw an error...
So c# called that an XML parse error because it happened so early... really it was thrown in code.
通过将引用的 dll 和配置文件包含在与 exe 文件相同的文件夹中解决了相同的问题。尝试单独运行 exe 将导致 Windows 级别异常。
Solved the same issue by including the referenced dll and config files in the same folder as the exe file. Trying to run the exe alone will cause a windows level exception.