我正在努力使用枢轴项目内容来按需添加内容,但所有项目都会立即添加。 (数据库标题的加载)
我在MainViewModel下面使用。
public class ArabicTextTranslationViewModel : INotifyPropertyChanged
{
ObservableCollection<ArabicTextTranslationHeader> _arabicTextTranslationHeader;
public ArabicTextTranslationViewModel()
{
ArabicTextTranslationHeaders = new ObservableCollection<ArabicTextTranslationHeader>();
DataSource ds = new DataSource();
List<Chapter> chapters = ds.getChaptersIndex(); // has 114 pivot headers
List<ArabicTextWithTranslationContent> content = null;
foreach (Chapter chapter in chapters)
{
content = ds.getArabicTextTranslationContent(chapter);
ArabicTextTranslationHeaders.Add(new ArabicTextTranslationHeader() { Header = chapter.TName, ArabicTextWithTranslationContent = content }); //loading contents
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public ObservableCollection<ArabicTextTranslationHeader> ArabicTextTranslationHeaders
{
get { return _arabicTextTranslationHeader; }
set
{
if (_arabicTextTranslationHeader != value)
{
_arabicTextTranslationHeader = value;
OnPropertyChanged("ArabicTextTranslationHeaders");
}
}
}
public class ArabicTextTranslationHeader : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
string _header;
public string Header
{
get { return _header; }
set
{
if (_header != value)
{
_header = value;
OnPropertyChanged("Header");
}
}
}
List<ArabicTextWithTranslationContent> _arabicTextTranslationContent;
public List<ArabicTextWithTranslationContent> ArabicTextWithTranslationContent
{
get { return _arabicTextTranslationContent; }
set
{
if (_arabicTextTranslationContent != value)
{
_arabicTextTranslationContent = value;
OnPropertyChanged("ArabicTextWithTranslationContent");
}
}
}
}
public class ArabicTextWithTranslationContent : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
List<ArabicTextWithTranslation> _arabicTextTranslation;
public List<ArabicTextWithTranslation> ArabicTextWithTranslation
{
get { return _arabicTextTranslation; }
set
{
if (_arabicTextTranslation != value)
{
_arabicTextTranslation = value;
OnPropertyChanged("ArabicTextWithTranslation");
}
}
}
}
}
和XAML
<phone:Pivot Title="{Binding Path=LocalizedResources.ApplicationTitle, Source={StaticResource LocalizedStrings}}"
ItemsSource="{Binding ArabicTextTranslationHeaders}"
DataContext="{Binding Source={StaticResource ArabicTextTranslationDataSource}}">
<phone:Pivot.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Header}"></TextBlock>
</DataTemplate>
</phone:Pivot.HeaderTemplate>
<phone:Pivot.ItemTemplate>
<DataTemplate>
<ListBox ItemsSource="{Binding ArabicTextWithTranslationContent}"
FontFamily="./Fonts/ScheherazadeRegOT.ttf#Scheherazade"
FlowDirection="RightToLeft" Foreground="Black" >
<ListBox.ItemTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding ArabicTextWithTranslation}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock TextWrapping="Wrap"
Text="{Binding ArabicText.Aya}" FontSize="30"
Foreground="Black"></TextBlock>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DataTemplate>
</phone:Pivot.ItemTemplate>
</phone:Pivot>
问题是,我遇到内存不足异常,我发现所有内容都是立即加载的。
如何按需加载每个透视图项目的内容?