在我的应用程序中,用户使用像书一样的枢轴,他翻转枢轴项目直到他击中最后一个,然后我删除现有的枢轴项目并加载新的。这一切都是通过简单地将Pivot.ItemsSource
属性指向一个新的项目集合来完成的。
我注意到超时内存消耗会增加并且永远不会降低。Pivot 的 VisualTree 似乎没有被垃圾收集。
我创建了一个示例应用程序来演示该问题(这是一个 WP8 应用程序):
重现步骤:
- 启动应用程序
- 导航到第 1 页
单击加载更多按钮几次。
(一次它加载 100 个项目,这当然不是我在实际应用程序中所做的,但它清楚地展示了每次内存消耗如何越来越高,即使导航回来也不会被清除)
我将不胜感激任何降低内存的提示或建议,因为如果以这种方式使用足够长的时间,应用程序不可避免地会崩溃。
MainPage.xaml:
...
<HyperlinkButton NavigateUri="/Page1.xaml">
Page1
</HyperlinkButton>
...
MainPage.xaml.cs:
//...
public MainPage()
{
InitializeComponent();
var timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(2);
timer.Tick += delegate
{
Debug.WriteLine("{0:f} MB, {1:f} MB",
DeviceStatus.ApplicationCurrentMemoryUsage/(1024.0*1024),
DeviceStatus.ApplicationPeakMemoryUsage/(1024.0*1024));
};
timer.Start();
}
//...
Page1.xaml:
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel Grid.Row="0" Margin="12,17,0,28">
<TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}" />
<TextBlock Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}" />
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<StackPanel>
<phone:Pivot x:Name="pivot1" ItemsSource="{Binding Items}">
<!--Pivot item one-->
<phone:Pivot.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" />
</DataTemplate>
</phone:Pivot.ItemTemplate>
</phone:Pivot>
<Button Click="Load_More">Load More</Button>
</StackPanel>
</Grid>
</Grid>
Page1.xaml.cs:
public partial class Page1 : PhoneApplicationPage, INotifyPropertyChanged
{
private List<int> _items;
public List<int> Items
{
get
{
if (_items == null)
{
_items = new List<int>();
}
return _items;
}
set
{
_items = value;
RaisePropertyChanged("Items");
}
}
public Page1()
{
InitializeComponent();
DataContext = this;
}
private void Load_More(object sender, RoutedEventArgs e)
{
var lst = new List<int>();
//100 new items just for demonstration, in reality i won't have more than 6 new items
for (int i = 0; i < 100; i++)
{
lst.Add(i);
}
pivot1.SelectedIndex = 0;
Items=lst;
}
public event PropertyChangedEventHandler PropertyChanged;
void RaisePropertyChanged(string property)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(property));
}
}
~Page1()
{
Debug.WriteLine("Page1 GC");
}
}