WebPart.AfterDeserialize 方法

从 SharePoint 数据库或 Web 部件描述文件 (.dwp) SharePoint Web 部件的属性反序列化之后调用。SharePoint Web 部件升级代码的的建议的位置。

命名空间:  Microsoft.SharePoint.WebPartPages
程序集:  Microsoft.SharePoint(位于 Microsoft.SharePoint.dll 中)

语法

声明
Public Overridable Sub AfterDeserialize
用法
Dim instance As WebPart

instance.AfterDeserialize()
public virtual void AfterDeserialize()

备注

AfterDeserialize方法支持 Web 部件的新版本需要适应对已保存的以前版本的 Web 部件的属性的更改的升级方案。如果 Web 部件开发人员只需将添加一个或多个新属性,不所需的任何升级的逻辑。但是,如果属性被删除或重命名,或保存的值发生了变化,然后重写AfterDeserialize方法提供一种方法,用于处理这些更改时 SharePoint Web 部件升级。

示例

下面的代码示例演示如何从 Web 部件的更高版本中删除了一个或多个属性时处理情况。由于该程序集无法识别的元素将添加到UnknownXmlElementCollection集合,下面的代码示例显示循环访问该集合并删除未知的元素。

有关在升级 Web 部件,使用AfterDeserialize方法的详细信息,请参阅升级 Web 部件程序集

' Override the AfterDeserialize method to remove any items in 
' the UnknownXmlElementCollection within the MyWebPartNamespace
Public Overrides Sub AfterDeserialize()
    Try
       Dim itemsRemoved As Boolean = False
       If UnknownXmlElements.Count > 0 Then
          Dim element As XmlElement
          Dim index As Integer = UnknownXmlElements.Count - 1

          ' Loop from the end of the collection to eliminate the need to
          ' update the index based on whether or not an item was deleted. 
          While index >= 0
             element = UnknownXmlElements(index)

            ' Check if the element is within the namespace.
            ' Do a case-insensitive match to be consistent with the
            ' XmlSerializer.
            If element.NamespaceURI = "MyWebPartNamespace" Then
               ' If the element is in the namespace, remove it 
               ' from the collection.
               UnknownXmlElements.RemoveAt(index)
               itemsRemoved = True
            End If
            index -= 1
          End While
      End If
      ' If your assembly is installed in the application's bin directory, which defaults to minimal trust, 
      ' make sure that your Web Part assembly has UnsafeSaveOnGet permission or the following 
      ' attempt to save changes will return an error.  
      If itemsRemoved Then
         SaveProperties = True
      End If
   Catch Else
       ' Handle or ignore errors.
   End Try
End Sub
// Override the AfterDeserialize method to remove any items in 
// the UnknownXmlElementCollection within the MyWebPartNamespace
public override void AfterDeserialize()
{
    try
    {
        bool itemsRemoved = false;
        if (UnknownXmlElements.Count>0)
        {
            XmlElement element;
            int index = UnknownXmlElements.Count - 1;

            // Loop from the end of the collection to eliminate the need to
            // update the index based on whether or not an item was deleted. 
            while (index >= 0)
            {
                element = UnknownXmlElements[index];

                // Check if the element is within the namespace.
                // Do a case-insensitive match to be consistent with the
                // XmlSerializer.
                if (element.NamespaceURI == "MyWebPartNamespace")
                {
                    // If the element is in the namespace, remove it from the collection.
                    UnknownXmlElements.RemoveAt(index);
                    itemsRemoved = true;
                }
                --index;
            }
        }
        // If your assembly is installed in the application's bin directory, which defaults to minimal trust, 
        // make sure that your Web Part assembly has UnsafeSaveOnGet permission or the following 
        // attempt to save changes will return an error.  
        if (itemsRemoved)
        {
            SaveProperties = true;
        }
    }
    catch
    {
         // Handle or ignore errors.
    }
}

另请参阅

引用

WebPart 类

WebPart 成员

Microsoft.SharePoint.WebPartPages 命名空间