Pivot
raises various events to indicate when a PivotItem
is being brought into view and when it is being moved out of view.
Best Practice
To improve the performance of your app, use the LoadingPivotItem
event to load content on demand. One approach is to convert the content of each PivotItem
into a UserControl
. You can then subscribe to the LoadingPivotItem
event on the Pivot
, and then when the event is raised instantiate the UserControl
and set it as the PivotItem
’s content.
The UnloadedPivotItem
event also provides the opportunity to clean up any unneeded resources.
When the user switches from one PivotItem
to another, events are raised in the following order:
1. The UnloadingPivotItem
event is raised for the first PivotItem
.
2. The LoadingPivotItem
event is raised for the second PivotItem
.
3. The UnloadedPivotItem
event is raised for the first PivotItem
.
4. The LoadedPivotItem
event is raised for the second PivotItem
.
These events are not cancelable. That is, they are not designed to allow for preventing or returning the Pivot
to the previously active PivotItem
. It is possible, however, to queue a change back to the previously active PivotItem
. Queuing an index change in one of the load event handlers has to be performed after the current PivotItem
change has finished; otherwise, the Pivot
will get out of sync. This can be done by using the Dispatcher
to invoke the change, which means it occurs after the current UI thread task completes. The following example demonstrates how to use the Dispatcher
to lock the Pivot
to the first PivotItem
:
void Pivot_LoadingPivotItem(object sender, PivotItemEventArgs e)
{
Dispatcher.BeginInvoke(delegate { pivot.SelectedIndex = 0; });
}
18.223.107.85