共用方式為


WebPart.AfterDeserialize method

從 SharePoint 資料庫或網頁組件描述檔案 (.dwp) 還原序列化的 SharePoint 網頁組件的屬性之後呼叫。建議的 SharePoint 網頁組件升級的程式碼的位置。

Namespace:  Microsoft.SharePoint.WebPartPages
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

'宣告
Public Overridable Sub AfterDeserialize
'用途
Dim instance As WebPart

instance.AfterDeserialize()
public virtual void AfterDeserialize()

備註

AfterDeserialize方法支援升級的案例,其中網頁組件的新版本需要配合變更已儲存為舊版的網頁組件的屬性。如果網頁組件開發人員只是新增一或多個屬性,未升級的邏輯,則需要。不過,如果刪除或重新命名,屬性或儲存的值已變更,則覆寫AfterDeserialize方法提供一種方式,用來處理這些變更時 SharePoint 網頁組件已升級。

Examples

下列程式碼範例會示範如何處理這種狀況時的網頁組件更新的版本中已移除一或多個屬性。組件無法辨識的項目會新增至UnknownXmlElementCollection集合,因為下列程式碼範例會顯示逐一查看集合,並移除未知的項目。

如需升級網頁組件時使用AfterDeserialize方法的詳細資訊,請參閱 <升級網頁組件

' 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 class

WebPart members

Microsoft.SharePoint.WebPartPages namespace