Windows phone 7 如何动态加载存储在隔离数据存储器中的数据
当我到达列表框的最后一个索引时,我想添加存储在隔离数据存储器中的数据,但我不知道我是如何做到这一点的,我找到了一个解决方案,即。。我将其转换为我的需求,这是我所做的代码,但问题是当我添加存储在数据存储中的新项时..实际上,在这里我添加了存储在数据存储中的相同项,但未添加项..分别是当我到达最后一个索引时Windows phone 7 如何动态加载存储在隔离数据存储器中的数据,windows-phone-7,Windows Phone 7,当我到达列表框的最后一个索引时,我想添加存储在隔离数据存储器中的数据,但我不知道我是如何做到这一点的,我找到了一个解决方案,即。。我将其转换为我的需求,这是我所做的代码,但问题是当我添加存储在数据存储中的新项时..实际上,在这里我添加了存储在数据存储中的相同项,但未添加项..分别是当我到达最后一个索引时 ObservableCollection<Discrip> list = new ObservableCollection<Discrip>(); st
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;
}
}
}
ObservableCollection列表=新的ObservableCollection();
静态ObservableCollection lst=新ObservableCollection();
滚动查看器滚动查看器;
公共主页()
{
初始化组件();
ListBoxDemo.Loaded+=新路由EventHandler(ListBoxDemo_Loaded);
AddDummyItems();
//save();
}
已加载void ListBoxDemo_(对象发送方,RoutedEventArgs e)
{
FrameworkElement=(FrameworkElement)发送方;
element.Loaded-=ListBoxDemo_Loaded;
scrollViewer=FindChildOfType(元素);
如果(scrollViewer==null)
{
抛出新的InvalidOperationException(“未找到ScrollViewer”);
}
绑定=新绑定();
binding.Source=scrollViewer;
binding.Path=新属性路径(“垂直偏移”);
binding.Mode=BindingMode.OneWay;
此.SetBinding(ListVerticalOffsetProperty,binding);
}
仅专用静态无效ListVerticalOffsetChanged(DependencyObject对象,
DependencyPropertyChangedEventArgs(附件e)
{
主页面=obj作为主页面;
ScrollViewer=page.ScrollViewer;
//根据ScrollableHeight检查滚动是否已到达最后一项
bool atBottom=viewer.VerticalOffset>=viewer.ScrollableHeight;
如果(在底部)
{
//ViewModel会更好。。。。。。。。
//int Count=page.ListBoxDemo.Items.Count;
//int end=计数+10;
//for(int i=Count;i0)
{
DependencyObject当前=队列。退出队列
<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>