我目前完全被我遇到的問題困惑不解。我正在爲另一個提供公共.NET API的應用程序編寫一個插件。我創建了一個名爲Room
的類,我正在使用PropertyGrid來允許用戶查看和編輯Room
實例的屬性。一些屬性僅限於一組標準值。因此,我使用GetStandardValues()
覆蓋的自定義TypeDescriptors來獲取屬性網格,以顯示這些屬性的下拉列表。InvalidCastException將對象轉換爲它自己的類型
這一切都工作得很好。我得到的下降,我可以編輯值沒有問題。然而,現在由於某種原因,當我選擇Room
時,PropertyGrid將類型描述符的屬性顯示爲黑盒子。
如果我點擊框會變成白色,我得到一個閃爍的光標,但我無法輸入任何內容。如果我再選擇另一間我的程序有以下例外崩潰:
System.InvalidCastException was caught
Message=Unable to cast object of type 'DVAMC.Room' to type 'DVAMC.Room'.
Source=DVAMC
StackTrace:
at DVAMC.BuildingTypeConverter.GetStandardValuesSupported(ITypeDescriptorContext context) in C:\Documents and Settings\eric.anastas\My Documents\_SVN WC\DVAMC Working\BuildingTypeConverter.cs:line 14
at System.Windows.Forms.PropertyGridInternal.GridEntry.get_Flags()
at System.Windows.Forms.PropertyGridInternal.GridEntry.get_NeedsDropDownButton()
at System.Windows.Forms.PropertyGridInternal.PropertyDescriptorGridEntry.get_NeedsDropDownButton()
at System.Windows.Forms.PropertyGridInternal.PropertyGridView.SelectRow(Int32 row)
at System.Windows.Forms.PropertyGridInternal.PropertyGridView.SelectGridEntry(GridEntry gridEntry, Boolean fPageIn)
at System.Windows.Forms.PropertyGridInternal.PropertyGridView.GridPositionData.Restore(PropertyGridView gridView)
at System.Windows.Forms.PropertyGridInternal.PropertyGridView.Refresh(Boolean fullRefresh, Int32 rowStart, Int32 rowEnd)
at System.Windows.Forms.PropertyGridInternal.PropertyGridView.Refresh()
at System.Windows.Forms.PropertyGrid.Refresh(Boolean clearCached)
at System.Windows.Forms.PropertyGrid.set_SelectedObjects(Object[] value)
at System.Windows.Forms.PropertyGrid.set_SelectedObject(Object value)
at DVAMC.RoomDetailsForm.set_RoomDetailsSelectedRoom(Room value) in C:\Documents and Settings\eric.anastas\My Documents\_SVN WC\DVAMC Working\RoomDetailsForm.cs:line 115
at DVAMC.RoomDetailsForm.roomListTreeView_SelectionChanged(Object sender, EventArgs e) in C:\Documents and Settings\eric.anastas\My Documents\_SVN WC\DVAMC Working\RoomDetailsForm.cs:line 159
at BrightIdeasSoftware.ObjectListView.OnSelectionChanged(EventArgs e)
at BrightIdeasSoftware.ObjectListView.HandleApplicationIdle(Object sender, EventArgs e)
at System.Windows.Forms.Application.ThreadContext.System.Windows.Forms.UnsafeNativeMethods.IMsoComponent.FDoIdle(Int32 grfidlef)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.RunDialog(Form form)
at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
at System.Windows.Forms.Form.ShowDialog()
at DVAMC.RoomDetailsCmd.Execute(ExternalCommandData commandData, String& message, ElementSet elements) in C:\Documents and Settings\eric.anastas\My Documents\_SVN WC\DVAMC Working\RoomDetailsCmd.cs:line 44
InnerException:
在堆棧跟蹤點我BuildingTypeConverter.GetStandardValuesSupported()方法(如下所示)中的最後一項。
GetStandardValuesSupported(System.ComponentModel.ITypeDescriptorContext context)
{
Room r = (Room)context.Instance; //this is line 14 referenced by the InvalidCastException's stack trace
if (r.IsLinked)
{
return true;
}
else
{
return false;
}
}
現在,如果我在設置上面一行14斷點並嘗試調試調試器在斷點處不破。另外,如果在強制轉換之前添加任意代碼,則InvalidCastException中的堆棧跟蹤總是似乎引用GetStandardValues()的第一行,而不管它是什麼。例如,我嘗試了以下。
public override bool GetStandardValuesSupported(System.ComponentModel.ITypeDescriptorContext context)
{
string s = "hello world"; //FIRST LINE
int i = 0;
Room r = (Room)context.Instance;
.....
我還是得到了InvalidCastException。然而,它是堆棧跟蹤參考上面的第一行,我初始化string s
。另外,如果我試圖在第一行設置斷點,它也不會被觸發。
就像我之前說過的,這個是工作在一天前很好。我甚至嘗試回滾到SVN存儲庫中的以前版本。我已經回到第一次修訂版了,我創建了自定義類型描述符類,但仍遇到InvalidCastExceptions
的問題。有誰知道發生了什麼事?
我最近被困在一個類似的方式,事實證明,這與設計師生成的代碼相關,當我命名一個屬性時,這些代碼碰到了 – 2010-10-09 00:33:01
Humm有趣的東西。然而即使我回滾(包括設計器代碼)到一個正在工作的版本,它仍然不起作用。儘管如此,我確實有一些與我的課程名稱相同的屬性。 – 2010-10-09 00:47:17