XamlParseException 停止项目

发布于 2024-12-19 11:36:02 字数 6807 浏览 2 评论 0 原文

我正在使用《Microsoft VS C# 2010 Step by Step》一书学习 C# 2010,该书的第 27 章介绍了任务并行库。当我运行提供的“GraphDemo”项目时,出现 XamlParseException 错误。 我查看了该网站上关于同一异常的多个线程,并设法深入了解内部异常,发现无法加载 PerformanceCounter。 由于担心系统的 .NET Framework 4 安装损坏,我将其修复到原始状态,但错误仍然存​​在。 奇怪的是,本书的其他读者都没有抱怨过这个问题;在我写信给出版商后,作者要求我向他发送该项目的 zip 文件,他声称该项目在他的机器上运行良好。第 27 章中还有其他七个项目,它们都在我的系统上引发了相同的错误。

这是生成的异常的全文:

System.Windows.Markup.XamlParseException was unhandled
  Message='The invocation of the constructor on type 'GraphDemo.GraphWindow' that matches the specified binding constraints threw an exception.' Line number '3' and line position '5'.
  Source=PresentationFramework
  LineNumber=3
  LinePosition=5
  StackTrace:
       at System.Windows.Markup.XamlReader.RewrapException(Exception e, IXamlLineInfo lineInfo, Uri baseUri)
       at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
       at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
       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__1(Object unused)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, 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, Boolean ignoreSyncCtx)
       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, Int32 numArgs)
       at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
       at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
       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.Application.RunDispatcher(Object ignore)
       at System.Windows.Application.RunInternal(Window window)
       at System.Windows.Application.Run(Window window)
       at System.Windows.Application.Run()
       at GraphDemo.App.Main() in E:\IT Books\Source Code Projects\Microsoft Visual C# 2010 Step By Step\Chapter 27\GraphDemo\GraphDemo\obj\x86\Debug\App.g.cs:line 0
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly 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, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.InvalidOperationException
       Message=Cannot load Counter Name data because an invalid index '' was read from the registry.
       Source=System
       StackTrace:
            at System.Diagnostics.PerformanceCounterLib.GetStringTable(Boolean isHelp)
            at System.Diagnostics.PerformanceCounterLib.get_NameTable()
            at System.Diagnostics.PerformanceCounterLib.get_CategoryTable()
            at System.Diagnostics.PerformanceCounterLib.CounterExists(String category, String counter, Boolean& categoryExists)
            at System.Diagnostics.PerformanceCounterLib.CounterExists(String machine, String category, String counter)
            at System.Diagnostics.PerformanceCounter.InitializeImpl()
            at System.Diagnostics.PerformanceCounter..ctor(String categoryName, String counterName, String instanceName, Boolean readOnly)
            at System.Diagnostics.PerformanceCounter..ctor(String categoryName, String counterName)
            at GraphDemo.GraphWindow..ctor() in E:\IT Books\Source Code Projects\Microsoft Visual C# 2010 Step By Step\Chapter 27\GraphDemo\GraphDemo\GraphWindow.xaml.cs:line 25
       InnerException: 

And this is the code pinpointed by the inner exception:

    public GraphWindow()
    {
        InitializeComponent();
        PerformanceCounter memCounter = new PerformanceCounter("Memory", "Available Bytes");
        availableMemorySize = Convert.ToUInt64(memCounter.NextValue());
        this.pixelWidth = (int)availableMemorySize / 20000;
        if (this.pixelWidth < 0 || this.pixelWidth > 15000)
        this.pixelWidth = 15000;
        this.pixelHeight = (int)availableMemorySize / 40000;
        if (this.pixelHeight < 0 || this.pixelHeight > 7500)
        this.pixelHeight = 7500;
    }

第 25 行是初始化 memCounter 的行。

我将感谢我能得到的所有帮助。

I am learning C# 2010 using the book 'Microsoft VS C# 2010 Step by Step' whose Chapter 27 introduces the Task Parallel Library. When I run the provided 'GraphDemo' project, I get an XamlParseException error.
I went over several of the threads on this site on the same exception and managed to drill down the inner exception to a failure to load the PerformanceCounter.
Fearing corruption of my system's .NET Framework 4 installation, I repaired it to original state but the error persists.
The strange thing is that, none of the other readers of the book have complained about this issue; after I wrote to the publishers, the author asked me to send him a zipfile of the project and he claims it ran fine on his machine. There are seven other projects in Chapter 27 and all of them throw the same error on my system.

This the full text of the exception generated:

System.Windows.Markup.XamlParseException was unhandled
  Message='The invocation of the constructor on type 'GraphDemo.GraphWindow' that matches the specified binding constraints threw an exception.' Line number '3' and line position '5'.
  Source=PresentationFramework
  LineNumber=3
  LinePosition=5
  StackTrace:
       at System.Windows.Markup.XamlReader.RewrapException(Exception e, IXamlLineInfo lineInfo, Uri baseUri)
       at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
       at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
       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__1(Object unused)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, 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, Boolean ignoreSyncCtx)
       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, Int32 numArgs)
       at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
       at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
       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.Application.RunDispatcher(Object ignore)
       at System.Windows.Application.RunInternal(Window window)
       at System.Windows.Application.Run(Window window)
       at System.Windows.Application.Run()
       at GraphDemo.App.Main() in E:\IT Books\Source Code Projects\Microsoft Visual C# 2010 Step By Step\Chapter 27\GraphDemo\GraphDemo\obj\x86\Debug\App.g.cs:line 0
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly 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, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.InvalidOperationException
       Message=Cannot load Counter Name data because an invalid index '' was read from the registry.
       Source=System
       StackTrace:
            at System.Diagnostics.PerformanceCounterLib.GetStringTable(Boolean isHelp)
            at System.Diagnostics.PerformanceCounterLib.get_NameTable()
            at System.Diagnostics.PerformanceCounterLib.get_CategoryTable()
            at System.Diagnostics.PerformanceCounterLib.CounterExists(String category, String counter, Boolean& categoryExists)
            at System.Diagnostics.PerformanceCounterLib.CounterExists(String machine, String category, String counter)
            at System.Diagnostics.PerformanceCounter.InitializeImpl()
            at System.Diagnostics.PerformanceCounter..ctor(String categoryName, String counterName, String instanceName, Boolean readOnly)
            at System.Diagnostics.PerformanceCounter..ctor(String categoryName, String counterName)
            at GraphDemo.GraphWindow..ctor() in E:\IT Books\Source Code Projects\Microsoft Visual C# 2010 Step By Step\Chapter 27\GraphDemo\GraphDemo\GraphWindow.xaml.cs:line 25
       InnerException: 

And this is the code pinpointed by the inner exception:

    public GraphWindow()
    {
        InitializeComponent();
        PerformanceCounter memCounter = new PerformanceCounter("Memory", "Available Bytes");
        availableMemorySize = Convert.ToUInt64(memCounter.NextValue());
        this.pixelWidth = (int)availableMemorySize / 20000;
        if (this.pixelWidth < 0 || this.pixelWidth > 15000)
        this.pixelWidth = 15000;
        this.pixelHeight = (int)availableMemorySize / 40000;
        if (this.pixelHeight < 0 || this.pixelHeight > 7500)
        this.pixelHeight = 7500;
    }

Line 25 is the one where memCounter is initialized.

I shall appreciate all the help I can get.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扎心 2024-12-26 11:36:02

您很有可能在针对 4.0 框架时引用 .Net 2.0 应用程序。尝试将 useLegacyV2RuntimeActivationPolicy="true" 添加到配置文件中:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0"/>
  </startup>
</configuration>

There is a good chance you are referring to a .Net 2.0 application while targeting the 4.0 framework. Try adding useLegacyV2RuntimeActivationPolicy="true" to the config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0"/>
  </startup>
</configuration>
温柔戏命师 2024-12-26 11:36:02

我遇到了同样的问题,这就是我解决的方法。
基本上,您的性能计数器列表已损坏。
http://blogs.technet.com/b/yongrhee/archive/2009/10/06/how-to-rebuild-performance-counters-on-windows-vista-server2008-7-server2008r2。 :

要解决这个问题
以管理员身份运行 CMD,并在命令提示符下输入:

C:\Windows\System32\lodctr /r

性能计数器将从头开始重建。

I had the same issue, and here is how I solved.
Basically, your Performance Counter list is corrupted.
http://blogs.technet.com/b/yongrhee/archive/2009/10/06/how-to-rebuild-performance-counters-on-windows-vista-server2008-7-server2008r2.aspx

To solve this:
Run CMD as Administrator, and on the command prompt enter:

C:\Windows\System32\lodctr /r

The performance counters will be rebuild from scratch.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文