2

My MainPage.xaml is a pivot page with 3 PivotItems. Currently it is loading all of the necessary stuff for each of the PivotItems on the MainPage constructor. This is bad because it loads a lot of stuff that are not necessary.

Reading here and here suggests that I only load the first PivotItem and after it loads, load the rest of the items. Specifically :

Improve the performance of the pivot application by loading Pivot control content on-demand as opposed to loading everything at startup. One solution is to take the content from each PivotItem control and convert into UserControls. You can then subscribe to the LoadingPivotItem event on the root pivot control. Next, in the event handler, instantiate the proper UserControl and set it as the PivotItem content.

If I follow the suggestion:

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

I should use create class PageXControl ? Should it inherit somehow from main-page class ?

How do i take the content from each PivotItem control and convert into UserControls ?

Thanks

4

2 回答 2

10

将 PivotItems 的内容提取到 UserControls 中实际上非常简单。首先,为每个 PivotItems 创建一个新的 UserControl。然后将 PivotItems 的内容从 PivotItem 移动到 UserControls 中。OnLoadingPivotItem然后按照您指定的方法在方法中创建控件。我在 GitHub 上创建了一个小项目,向您展示如何做到这一点。见:https ://github.com/ErikSchierboom/pivotcontentdemo

如您所见,我从基类派生了 UserControl,因为它们在语义上是相同的。但是,这绝不是必要的,只需从 UserControl 继承即可。

与将 PivotItems 本身提取到自定义控件中的方法相比,我更喜欢这种方法。

于 2012-12-12T10:33:00.743 回答
6

您可以创建自己的 Pivot 项,该项将从 PivotItem 继承。我已经根据 VS 中的默认 Pivot 项目整理了一个示例,它将两个 Pivot 项目拆分为它们自己的类:-

http://www.smartmobiledevice.co.uk/projects/PivotItemUserControlSample.zip

于 2012-04-29T14:55:41.273 回答