繁体   English   中英

如何在Windows Phone 7应用程序的列表框中使用拉动刷新功能

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM