我想添加存储在我的隔离数据存储中的数据,当我到达列表框的最后一个索引但我不知道我是如何做到这一点,我找到了一个解决方案,即.. {{3}我把它转换成我的要求,这是我做的代码,但问题是当我添加新项目存储在我的数据存储中。实际上我在这里添加相同的项目,我的数据存储,但项目不添加.. ..当我到达最后一个索引时... ...
ObservableCollection<Discrip> list = new ObservableCollection<Discrip>();
static ObservableCollection<Discrip> lst = new ObservableCollection<Discrip>();
ScrollViewer scrollViewer;
public MainPage()
{
InitializeComponent();
ListBoxDemo.Loaded += new RoutedEventHandler(ListBoxDemo_Loaded);
AddDummyItems();
// save();
}
void ListBoxDemo_Loaded(object sender, RoutedEventArgs e)
{
FrameworkElement element = (FrameworkElement)sender;
element.Loaded -= ListBoxDemo_Loaded;
scrollViewer = FindChildOfType<ScrollViewer>(element);
if (scrollViewer == null)
{
throw new InvalidOperationException("ScrollViewer not found.");
}
Binding binding = new Binding();
binding.Source = scrollViewer;
binding.Path = new PropertyPath("VerticalOffset");
binding.Mode = BindingMode.OneWay;
this.SetBinding(ListVerticalOffsetProperty, binding);
}
private static void OnListVerticalOffsetChanged(DependencyObject obj,
DependencyPropertyChangedEventArgs e)
{
MainPage page = obj as MainPage;
ScrollViewer viewer = page.scrollViewer;
//Checks if the Scroll has reached the last item based on the ScrollableHeight
bool atBottom = viewer.VerticalOffset >= viewer.ScrollableHeight;
if (atBottom)
{
//ViewModel will be better........
//int Count = page.ListBoxDemo.Items.Count;
//int end = Count + 10;
//for (int i = Count; i < end; i++)
//{
// page.ListBoxDemo.Items.Add("Dummy Item " + i);
//}
try
{
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("People.xml", FileMode.Open))
{
XmlSerializer serializer = new XmlSerializer(typeof(ObservableCollection<Discrip>));
ObservableCollection<Discrip> data = (ObservableCollection<Discrip>)serializer.Deserialize(stream);
lst =data;
if (data.Count() < 50)
{
for (int i = 0; i < data.Count(); i++)
{
page.save(lst);
lst.Add(new Discrip { date = "" + i + data.ElementAt(i).date, Imagee = data.ElementAt(i).Imagee, ImageSource = data.ElementAt(i).ImageSource, link = data.ElementAt(i).link, Detail = data.ElementAt(i).Detail, header = data.ElementAt(i).header, Title = "" + lst.Count() + data.ElementAt(i).Title });
}
// page. ListBoxDemo.ItemsSource = lst;
// MainPage p = new MainPage();
}
}
}
}
catch
{
//add some code here
}
}
}
void save(ObservableCollection<Discrip> lst)
{
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
xmlWriterSettings.Indent = true;
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("People.xml", FileMode.Create))
{
XmlSerializer serializer = new XmlSerializer(typeof(ObservableCollection<Discrip>));
using (XmlWriter xmlWriter = XmlWriter.Create(stream, xmlWriterSettings))
{
serializer.Serialize(xmlWriter, lst);
AddDummyItems();
}
}
}
}
void AddDummyItems()
{
try
{
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("People.xml", FileMode.Open))
{
XmlSerializer serializer = new XmlSerializer(typeof(ObservableCollection<Discrip>));
ObservableCollection<Discrip> data = (ObservableCollection<Discrip>)serializer.Deserialize(stream);
// lst = lst;
list = data;
// MainPage page = new MainPage();
// list.Add(lst);
ListBoxDemo.ItemsSource = list;
}
}
}
catch
{
//add some code here
}
}
public readonly DependencyProperty ListVerticalOffsetProperty = DependencyProperty.Register("ListVerticalOffset", typeof(double), typeof(MainPage),
new PropertyMetadata(new PropertyChangedCallback(OnListVerticalOffsetChanged)));
public double ListVerticalOffset
{
get { return (double)this.GetValue(ListVerticalOffsetProperty); }
set { this.SetValue(ListVerticalOffsetProperty, value); }
}
/// <summary>
/// Finding the ScrollViewer
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="root"></param>
/// <returns></returns>
static T FindChildOfType<T>(DependencyObject root) where T : class
{
var queue = new Queue<DependencyObject>();
queue.Enqueue(root);
while (queue.Count > 0)
{
DependencyObject current = queue.Dequeue();
for (int i = VisualTreeHelper.GetChildrenCount(current) - 1; 0 <= i; i--)
{
var child = VisualTreeHelper.GetChild(current, i);
var typedChild = child as T;
if (typedChild != null)
{
return typedChild;
}
queue.Enqueue(child);
}
}
return null;
}
}
}
我的Xaml是
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Black" CornerRadius="8" BorderThickness="1" HorizontalAlignment="Stretch" Name="border">
<Grid HorizontalAlignment="Stretch" Name="grid1" VerticalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="0*" />
<RowDefinition Height="100*" />
</Grid.RowDefinitions>
<Image Height="78" HorizontalAlignment="Left" Margin="13,12,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="92" Grid.Row="1" Source="{Binding ImageSource}" />
<TextBlock Foreground="White" Height="80" HorizontalAlignment="Left" Margin="111,12,0,0" Name="textBlock1" Text="{Binding Title}" VerticalAlignment="Top" Width="280" TextWrapping="Wrap" Grid.Row="1" />
<Image Grid.Row="1" Height="75" HorizontalAlignment="Left" Margin="380,12,0,0" Name="image2" Stretch="Fill" VerticalAlignment="Top" Width="73" Source="/WindowsPhoneApplication7;component/Images/appbar.transport.play.rest.png" />
<TextBlock Foreground="White" Grid.Row="1" Height="20" HorizontalAlignment="Left" Margin="111,88,0,0" Name="textBlock2" Text="{Binding date}" VerticalAlignment="Top" Width="190" FontSize="11" />
</Grid>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>