我的MainPage.xaml是一個包含3個透視項的透視頁。目前它正在爲MainPage構造函數中的每個PivotItems加載所有必需的東西。這很糟糕,因爲它加載了很多不必要的東西。將PivotItem轉換爲Usercontrol以提高WP7中的加載性能
閱讀here和here建議我只加載第一個PivotItem,並在加載後加載剩下的項目。具體來說:
通過按需加載Pivot控件內容來提高數據透視表的性能,而不是在啓動時加載所有內容。一種解決方案是從每個PivotItem控件獲取內容並將其轉換爲UserControls。然後,您可以訂閱根樞軸控件上的LoadingPivotItem事件。接下來,在事件處理程序中,實例化適當的UserControl並將其設置爲PivotItem內容。
如果我按照建議:
private void OnLoadingPivotItem(object sender, PivotItemEventArgs e)
{
if (e.Item.Content != null)
{
// Content loaded already
return;
}
Pivot pivot = (Pivot)sender;
if (e.Item == pivot.Items[0])
{
e.Item.Content = new Page1Control();
}
else if (e.Item == pivot.Items[1])
{
e.Item.Content = new Page2Control();
}
else if (e.Item == pivot.Items[2])
{
e.Item.Content = new Page3Control();
}
}
我應該使用創建類PageXControl?它應該從主頁類繼承嗎?
如何從每個PivotItem控件獲取內容並將其轉換爲UserControls?
感謝
謝謝。我注意到你使用'PivotItem'作爲樞軸頁面代碼而不是'UserControl'。有什麼區別嗎? – Michael 2012-04-29 16:07:01
我認爲如果您將多個控件放在一起形成另一個控件,您將使用UserControl,但是由於您只對PivotItem感興趣,因此從中派生出來是有意義的。 – 2012-04-29 16:25:31