[英]how to use pull to refresh facility in listbox for windows phone 7 application
我正在構建一個應用程序,可以從Web服務在列表框中獲取數據。 我需要使用刷新工具,以便當我使用該頁面時出現新數據時,應該自動刷新它,而不是再次調用Web服務。 我的代碼xaml和cs在下面給出。 請幫忙。
Xaml:
<ListBox Name="listBox1" SelectionChanged="listBox1_SelectionChanged" Height="676" VerticalAlignment="Bottom">
<ListBox.ItemTemplate>
<DataTemplate>
<Button IsHitTestVisible="False">
<Button.Content>
<ScrollViewer HorizontalScrollBarVisibility="Auto" Height="80" Width="400">
<StackPanel Orientation="Horizontal" Margin="0,0,0,0">
<Image Source="{Binding ImageBind }" Height="90" Width="95"/>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Path=News_Title}" TextWrapping="Wrap"></TextBlock>
<TextBlock Text="{Binding Path=Date_Start}" TextWrapping="Wrap" ></TextBlock>
</StackPanel>
</StackPanel>
</ScrollViewer>
</Button.Content>
</Button>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
我的cs文件:
public class Newss
{
public string News_Title { get; set; }
public string News_Description { get; set; }
public string Date_Start { get; set; }
public string image_path { get; set; }
public BitmapImage ImageBind{get;set;}
}
public const string NewssXml = "Newss.xml";
public News()
{
InitializeComponent();
LoadData();
}
private void LoadData()
{
bool isSuccess;
//try to load data from iso store
var doc = ReadXml(out isSuccess);
if (isSuccess) PopulateList(doc);
//if failed (data doesn't exists in iso store), download data from web service
else
{
KejriwalService.aapSoapClient client = new KejriwalService.aapSoapClient();
client.getarvindNewsCompleted += new EventHandler<KejriwalService.getarvindNewsCompletedEventArgs>(client_getarvindNewsCompleted);
client.getarvindNewsAsync();
progressName.Visibility = System.Windows.Visibility.Visible;
}
}
//upon download completed, display data then save the xml to iso store
void client_getarvindNewsCompleted(object sender, KejriwalService.getarvindNewsCompletedEventArgs e)
{
var doc = XDocument.Parse(e.Result);
PopulateList(doc);
WriteXml(doc);
}
private void PopulateList(XDocument doc)
{
List<Newss> listData = new List<Newss>();
progressName.Visibility = System.Windows.Visibility.Collapsed;
foreach (var location in doc.Descendants("UserDetails"))
{
Newss data = new Newss();
data.News_Title = location.Element("News_Title").Value;
data.News_Description = location.Element("News_Description").Value;
data.Date_Start = location.Element("Date_Start").Value;
data.image_path = location.Element("image_path").Value;
data.ImageBind = new BitmapImage(new Uri(@"http://political-leader.vzons.com/ArvindKejriwal/images/uploaded/" + data.image_path, UriKind.Absolute));
listData.Add(data);
}
listBox1.ItemsSource = listData;
}
private XDocument ReadXml(out bool isSuccess)
{
isSuccess = false;
var doc = new XDocument();
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
try
{
if (store.FileExists(NewssXml))
{
using (var sr = new StreamReader(new IsolatedStorageFileStream(NewssXml, FileMode.OpenOrCreate, store)))
{
doc = XDocument.Load(sr);
}
isSuccess = true;
}
}
catch (Exception ex) { }
}
return doc;
}
private bool WriteXml(XDocument document)
{
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
try
{
using (var sw = new StreamWriter(new IsolatedStorageFileStream(NewssXml, FileMode.Create, store)))
{
sw.Write(document.ToString());
}
}
catch (Exception ex) { return false; }
}
return true;
}
private void Image_Back(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/AAP.xaml", UriKind.Relative));
}
private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// If selected index is -1 (no selection) do nothing
if (listBox1.SelectedIndex == -1)
return;
Newss news = listBox1.SelectedItem as Newss;
NavigationService.Navigate(new Uri("/NewsDetails.xaml?News_Title=" + news.News_Title + "&News_Description=" + news.News_Description +"&Date_Start=" +news.Date_Start + "&image_path=" + news.image_path, UriKind.Relative));
// Reset selected index to -1 (no selection)
listBox1.SelectedIndex = -1;
}
}
}
請在此處查看我的博客文章: http : //www.sharpgis.net/post/2011/04/03/RefreshBox-for-Windows-Phone-7.aspx注意WP7上的這項工作,但如果編譯后運行得並不好對於WP8(為wp7編譯並在wp8上運行很好)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.