2011-07-12 44 views
0

我知道沒有DESIGN,DESIGN_MODE,DESIGN_TIME等預處理器指令值。但是,我需要一些可以做到這一點的技巧。我不能使用正常的If語句,因爲在我的情況下,我需要更改繼承的類,以便控件在設計時正確呈現。如果沒有,我會收到一個異常,因爲繼承的類是一個抽象類。設計模式預處理器指令解決方法

以下是我正在尋找的東西來完成:

Partial Class MyCustomControl 
#If DesignMode Then 
     Inherits UserControl 
#Else 
    Inherits WidgetControl 
#End If 

有什麼建議?

+1

你不能使這項工作。你必須讓你的基類成爲一個非抽象類來支持設計時間。 –

+0

但是我確信有一種解決方法,無需非抽象類。我甚至嘗試創建一個繼承自WidgetControl的中間類,但是當我調試和/或釋放時,我想使用WidgetControl。必須有一種使用預處理器指令的方法。 –

回答

0

嘗試使用:

if (this.DesignMode == true) 
{ } 
else 
{ } 
+0

'this'是指從UserControl繼承的當前類。 – Ryno

+0

我如何使用這個繼承?我很確定我不能。 –

0

在過去,我已經創建了一個虛擬的類之間一展身手。有時VS仍然會弄清楚你正在做什麼並感到沮喪,但通常重新啓動IDE將解決這個問題。

Partial Class MyCustomControl : MyAbstractClass_FAKE_IMPL 
{ 
    //your normal class 
} 

Partial Class MyAbstractClass_FAKE_IMPL : MyAbstractClass 
{ 

    //let IDE autogenerate implementation code that you are always going to override in reality. 

}