gpt4 book ai didi

c# - 将 PivotItem 转换为 Usercontrol 以提高 WP7 中的加载性能

转载 作者:行者123 更新时间:2023-11-30 18:55:06 25 4
gpt4 key购买 nike

我的 MainPage.xaml 是一个包含 3 个数据透视项的数据透视页。目前,它正在 MainPage 构造函数上为每个 PivotItems 加载所有必要的东西。这很糟糕,因为它加载了很多不必要的东西。

阅读 herehere建议我只加载第一个 PivotItem 并在加载后加载其余项目。具体来说:

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.

如果我听从建议:

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?

谢谢

最佳答案

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

如您所见,我从基类派生了 UserControl,因为它们在语义上是相同的。不过,这绝不是必须的,继承UserControl就可以了。

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

关于c# - 将 PivotItem 转换为 Usercontrol 以提高 WP7 中的加载性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10373012/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com