【问题标题】:Create SharePoint (2010) ToolPart usable for more than one WebPart创建可用于多个 WebPart 的 SharePoint (2010) ToolPart
【发布时间】:2012-01-08 10:21:17
【问题描述】:

我正在使用基本说明 (here) 创建由自定义 ToolPart 驱动的属性。

一切都很好,除了部分,为了访问 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 部件创建一个基类,但这很丑。

我也可能会绝望并使用反射打开 this.ParentToolPane.SelectedWebPart 引用并以这种方式调用任何名为“Text”的属性。

无论哪种方式,我都在盯着一个公平的桶,只是发现每个选项都是死胡同。

有没有人这样做并且可以推荐创建可重用工具部件的正确方法?

【问题讨论】:

  • 基础 webpart/toolpart 和必要时继承/覆盖有什么问题?
  • 直到我需要针对几个不同的工具部件运行几个不同的属性组合。
  • 这是真的 - 但也许你可以获得 80% 的通常的相关属性,比如这样?取决于您的确切用例。不得不说,没有冒犯,但我担心你可能会遇到 Architect Astraunatis 的案例;)(过度抽象)-joelonsoftware.com/items/2008/05/01.html
  • 我怀疑尝试应用依赖倒置原则会走得更远。
  • 很公平。我的直觉是,从长远来看,这是一个我将有足够多的努力得到可靠解决方案的问题。

标签: sharepoint sharepoint-2010 properties web-parts toolpart


【解决方案1】:

我使用的是界面而不是 Web 部件的特定实例。

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]);
}

但这并没有给出编译时警告工具部件需要父 Web 部件来实现 IMyProperty 接口。

对此的简单解决方案是在 toolpart 构造函数中添加 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 代码中有一些令人讨厌的东西可能会在以后绊倒我的感觉。

【讨论】:

    猜你喜欢
    • 2011-08-24
    • 1970-01-01
    • 2011-11-01
    • 2012-06-06
    • 2015-03-30
    • 2011-10-30
    • 2011-08-08
    • 2011-03-26
    • 2011-03-29
    相关资源
    最近更新 更多