2009-09-27 120 views
0

作爲學習wpf的一部分,我正在通過一些MS'實驗室'示例應用程序工作;完成的練習看起來是正確的,但我遇到了「源代碼不可用」問題(下面的例外細節)的NullReferenceException。它顯示主窗口後顯示。wpf調試xaml

我懷疑問題出在XAML上,但調試器並沒有像我想的那樣幫助我。我已經在代碼中調用NavigationService的地方放置了斷點,並且在異常發生之前沒有任何命中。作爲練習的一部分,頁面函數返回的類型從字符串更改爲對象。

在一個真實的項目。有可能會有足夠的單元測試和檢查來達到這種狀態,但只要我是學習WPF的一部分,它肯定不會傷害到知道如何更好地進行調試!

我該如何更好地調試這樣的東西?

< ---編輯過的註釋

@electronherder

最近想我可以做的是,有在窗口1框架16,其不掛接正確。這個想法是框架在ListBox中顯示關於當前所選聯繫人的聯繫信息;這在代碼中完成如下:

void ListItemSelected(object sender, SelectionChangedEventArgs args) { 
     // show first page function on the right hand side frame 
     var pageFunction = new ContactDetailPage1(false, lstBoxAllContacts.SelectedIndex); 
     pageFunction.Return += pageFunction0_Return; 
     Frame_RightPane.Navigate(pageFunction); 

     //update status bar 
     if (lstBoxAllContacts.SelectedItem != null) { 
      var contact = lstBoxAllContacts.SelectedItem as Contact; 
      statusBar.Text = contact.FirstName + " " + contact.LastName; 
     } 
     else { 
      statusBar.Text = "No contacts on file."; 
     } 
    } 

在Window1退出對Show()的調用之前,此方法執行時不會崩潰。它確實有這樣的呼叫做一些導航,我不完全理解,但是從實驗練習筆記複製。導航與另一個名爲ContactDetailPage1的窗口有關,以下是實驗室說明的解釋:

「我們希望能夠以兩種模式顯示此頁面:讀取和編輯。這樣我們可以重新使用讀取模式在聯繫人詳細信息框架中,Frame_RightPane「。

事件連接(pageFunction.Return + = pageFunction0_Return;)也有點懷疑,因爲它故意不讓編譯器感到滿意。我把斷言裏面來,因爲你可以在下面的代碼中看到:

// This is the ContactDetailPage1 page function's return handler; merely a placeholder 
    // since the page is displayed in read mode and doesn't return anything. 
    private static void pageFunction0_Return(object sender, ReturnEventArgs<object> e) { 
     Debug.Assert(false, "We should never actually be here!"); 
    } 

最後,這裏是從窗口1覆蓋框架的XAML:

<!-- RHS Panel --> 
    <Frame Name="Frame_RightPane" Grid.Column="0" Grid.ColumnSpan="1" Grid.Row="0" Grid.RowSpan="1" 
     Margin="201,65,0,0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="Auto"/> 

這是我能做到的最好在這裏,但這裏是任何真正想了解它的人的實驗室鏈接(http://windowsclient.net/downloads/folders/hands-on-labs/entry3719.aspx)。

< --- END EDIT --->的

乾杯,
Berryl

< - 例外 - >

System.NullReferenceException was unhandled 
Message="Object reference not set to an instance of an object." Source="mscorlib" 
StackTrace: 
    at System.Object.GetType() 
    at MS.Internal.AppModel.ReturnEventSaver._Detach(PageFunctionBase pf) 
    at System.Windows.Navigation.NavigationService.HookupNewTree(Object newTree, NavigateInfo navInfo, Uri newUri) 
    at System.Windows.Navigation.NavigationService.OnBeforeSwitchContent(Object newBP, NavigateInfo navInfo, Uri newUri) 
    at System.Windows.Navigation.NavigationService.MS.Internal.AppModel.IContentContainer.OnContentReady(ContentType contentType, Object bp, Uri bpu, Object navState) 
    at System.Windows.Navigation.NavigationService.DoNavigate(Object bp, NavigationMode navFlags, Object navState) 
    at System.Windows.Navigation.NavigateQueueItem.Dispatch(Object obj) 
    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.DispatcherOperation.InvokeImpl() 
    at System.Threading.ExecutionContext.runTryCode(Object userData) 
    at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData) 
    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.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter) 
    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.TranslateAndDispatchMessage(MSG& msg) 
    at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame) 
    at System.Windows.Application.RunInternal(Window window) 
    at AddressBook.MyApp.Main() in C:\Users\Berryl\Documents\Visual Studio 2008\Projects\UI related\DEV007 Building WPF Applications\Source\CS\Ex1\Task1\AddressBook\obj\Debug\MyApp.g.cs:line 0 
    at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) 
    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
    at System.Threading.ThreadHelper.ThreadStart() 

的InnerException:

+0

該示例相當陳舊 - 2007年5月9日 - 如果您運行的是VS 2008 SP1,則可能會在.NET 3.5 SP1中進行一些更改,這些更改可能會改變某些庫的行爲。像WinForms一樣,WPF在單獨的線程上運行,因此主瀏覽器的STA線程不會被阻塞,所以可能很難逐步完成失敗,特別是使用像XAML這樣的聲明式語言。我會建議評論你認爲是有問題的XAML,並看看它是否構建並在沒有它的情況下運行。 如果這樣,那麼嘗試添加功能。 HTH – jnoss 2009-09-28 06:24:01

回答

0

你有沒有重命名任何類 - 比如Window1.xaml?當我重命名窗口類並錯過了一些參考時,我在WPF項目中看到過類似的失敗。他們是生成代碼中的項目中的一些人。如果您在整個項目中搜索舊名稱,則應該能夠找到可能導致null參考的孤立參考。

+0

這聽起來像我正在尋找的東西,但在這裏似乎並不是這種情況。 – Berryl 2009-09-27 20:36:56

+0

編譯時WPF會自動生成大量的C# - 上述調用堆棧中的MyApp.g.cs代碼全部由WPF生成。 MS.Win32類是本機(C++)代碼,根據我的經驗,不會產生非常好的錯誤消息。 如果您提供了有關您正在嘗試執行的操作的更多信息,可能會有所幫助。 – jnoss 2009-09-27 21:34:12