2011-11-29 38 views
1

我正在使用基本指令(here)創建由自定義ToolPart驅動的屬性。創建SharePoint(2010)ToolPart可用於多個Web部件

除了爲了訪問ApplyChanges方法中的webpart屬性,我必須將「this.ParentToolPane.SelectedWebPart」轉換回具體的「SimpleWebPart」類的部分除外。

public override void ApplyChanges() 
{ 
    SimpleWebPart wp1 = (SimpleWebPart)this.ParentToolPane.SelectedWebPart; 

// Send the custom text to the Web Part. 
    wp1.Text = Page.Request.Form[inputname]; 
} 

這樣做意味着我必須將每個工具部件與特定的Web部件配對。有沒有更好的辦法? 我無法創建接口,因爲無法在其中指定屬性。

我無法在工具部件創建期間傳遞事件/事件處理程序,但在調用時沒有更新webpart屬性。

我可以爲所有具有公共「文本」屬性的web部件創建一個基類,但這很糟糕。

我也可以絕望,並打開這個.ParentToolPane.SelectedWebPart引用與反射,並調用任何屬性名稱「文本」的方式。無論哪種方式,我正在盯着每一個選項都是死衚衕的公平比特。

有沒有人這樣做,並可以推薦創建可重用工具部件的正確方法?

+0

基本webpart/toolpart和根據需要繼承/覆蓋有什麼問題? – Ryan

+0

什麼也沒有,直到我需要運行幾個不同的屬性組合對幾個不同的工具部件。 – Nat

+0

這是真的 - 但也許你可以得到像這樣的通常的相關屬性的80%?取決於你的確切用例。不得不說,沒有冒犯,但我擔心你可能會得到一個建築師Astraunatis的案件;)(在抽象) - http://www.joelonsoftware.com/items/2008/05/01.html – Ryan

回答

0

我已經使用了一個接口,而不是webpart的特定實例。

private class IMyProperty 
{ 
    void SetMyProperty(string value); 
} 

public override void ApplyChanges() 
{ 
    IMyProperty wp1 = (IMyProperty)this.ParentToolPane.SelectedWebPart; 

    // Send the custom text to the Web Part. 
    wp1.SetMyProperty(Page.Request.Form[inputname]); 
} 

但是,這並沒有給一個編譯時警告說,toolpart需要家長的WebPart實現IMyProperty接口。

簡單的解決方案是在工具構造函數中添加IMyProperty接口的屬性,並調用此引用而不是this.ParentToolPane.SelectedWebPart屬性。

public ToolPart1(IContentUrl webPart) 
{ 
    // Set default properties    
    this.Init += new EventHandler(ToolPart1_Init); 
    parentWebPart = webPart; 
} 

public override void ApplyChanges() 
{ 
    // Send the custom text to the Web Part. 
    parentWebPart.SetMyProperty(Page.Request.Form[inputname]); 
} 

public override ToolPart[] GetToolParts() 
{ 
    // This is the custom ToolPart. 
    toolparts[2] = new ToolPart1(this); 
    return toolparts; 
} 

這工作得很好,但我不能克服的感覺,有什麼東西在底層的SharePoint代碼討厭以後可能我絆倒。

相關問題