2

我正在學習C#2010中使用的書「Microsoft VS C#2010 Step by Step」,其第27章介紹了Task Parallel Library。當我運行提供的'GraphDemo'項目時,我得到一個XamlParseException錯誤。 我在同一個異常上檢查了本網站上的多個線程,並設法深入瞭解內核異常,以便加載PerformanceCounter失敗。 由於擔心繫統的.NET Framework 4安裝出現問題,我將其修復爲原始狀態,但錯誤仍然存​​在。 奇怪的是,本書的其他讀者都沒有抱怨過這個問題;在我寫信給發佈商之後,作者讓我給他發送一個項目的zip文件,他聲稱他的機器運行良好。第27章還有7個其他項目,他們都在我的系統上拋出同樣的錯誤。XamlParseException停止項目

產生異常的全文:

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被初始化。

我會感激所有我能得到的幫助。

回答

0

您在面向4.0框架時很有可能提到.Net 2.0應用程序。嘗試添加useLegacyV2RuntimeActivationPolicy =「真」到配置文件:

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