2014-05-07 148 views
1

我有一个Windows手机应用程序,它从SQL数据库获取照片URL列表,具体取决于它上传的内容。 我遇到的问题是用户可以将自己的照片添加到该列表中,但它不刷新页面上的列表,因此我添加了刷新以重新运行代码,但仍然无法运行。 代码运行,但不更新列表框。Windows Phone刷新按钮不起作用

//get/clean these strings 
     int parkID = 0; 
     string parkName = string.Empty; 

     public photos() 
     { 
      InitializeComponent(); 
      BuildLocalizedApplicationBar(); 
     } 


     private void ThemeParkPhotos_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) 
     { 
      if (e.Error == null) 
      { 
       try 
       { 
        //No errors have been passed now need to take this file and parse it 
        //Its in XML format 
        XDocument xdox = XDocument.Parse(e.Result); 
        //need a list for them to be put in to 
        List<Photos> themeparkPhoto = new List<Photos>(); 
        themeparkPhoto.Clear(); 
        XNamespace ns = "http://schemas.datacontract.org/2004/07/WCFServiceWebRole1"; 
        //Now need to get every element and add it to the list 
        foreach (XElement item in xdox.Descendants(ns + "Photos")) 
        { 
         Photos content = new Photos(); 
         content.ID = Convert.ToInt32(item.Element(ns + "ID").Value); 
         content.PhotoURL = Convert.ToString(item.Element(ns + "PhotoURL").Value); 
         //content.ID = Convert.ToInt32(item.Element(ns + "id").Value); 
         //content.ThemeParkName = item.Element(ns + "name").Value.ToString(); 
         themeparkPhoto.Add(content); 
        } 
        ThemeParkPhoto.ItemsSource = null; 
        ThemeParkPhoto.ItemsSource = themeparkPhoto.ToList(); 
        //Delete all the stuff 
       } 
       catch (Exception ex) 
       { 
        MessageBox.Show(ex.Message); 
       } 
      } 
      else 
      { 
       //There an Error 
      } 
     } 

     protected override void OnNavigatedTo(NavigationEventArgs e) 
     { 
      //This is to get the data that was passed from the home screen to which song to use! 
      base.OnNavigatedTo(e); 

      if ((NavigationContext.QueryString["pID"] == string.Empty) || (NavigationContext.QueryString["pName"] == string.Empty)) 
      { 
       //if not show message box. 
       MessageBox.Show("Empty Vaules have been sent, Please got back and try again"); 
      } 
      else 
      { 
       parkID = Convert.ToInt32(NavigationContext.QueryString["pID"]); 
       parkName = NavigationContext.QueryString["pName"].ToString(); 
       PageName.Text = parkName; 
       GetThemeParkPhotos(); 
      } 
     } 


     public void GetThemeParkPhotos() 
     { 

      WebClient ThemeParkPhotos = new WebClient(); 
      ThemeParkPhotos.DownloadStringCompleted += ThemeParkPhotos_DownloadStringCompleted; 
      ThemeParkPhotos.DownloadStringAsync(new Uri("HIDDEDURL/viewphotos?format=xml&themeparkid=" + parkID)); 
      //MessageBox.Show("Test if this works"+parkID); 
     } 

     private void BuildLocalizedApplicationBar() 
     { 
      ApplicationBar = new ApplicationBar(); 
      ApplicationBar.Mode = ApplicationBarMode.Default; 
      ApplicationBar.Opacity = 1.0; 
      ApplicationBar.IsVisible = true; 
      ApplicationBar.IsMenuEnabled = true; 
      ApplicationBarIconButton AddButton = new ApplicationBarIconButton(); 
      AddButton.IconUri = new Uri("/Images/add.png", UriKind.Relative); 
      AddButton.Text = "Add Photo"; 
      ApplicationBar.Buttons.Add(AddButton); 
      AddButton.Click +=AddButton_Click; 
      //Dont add refresh button as it does not work at this time :(
      ApplicationBarIconButton RefreshButton = new ApplicationBarIconButton(); 
      RefreshButton.IconUri = new Uri("/Images/refresh.png", UriKind.Relative); 
      RefreshButton.Text = "Refresh"; 
      ApplicationBar.Buttons.Add(RefreshButton); 
      RefreshButton.Click += RefreshButton_Click; 
     } 

     private void RefreshButton_Click(object sender, EventArgs e) 
     { 
      GetThemeParkPhotos(); 
     } 

     private void AddButton_Click(object sender, EventArgs e) 
     { 
      //need to send them to add a photo page with details. 

      NavigationService.Navigate(new Uri("/TakePhoto.xaml?pID=" + parkID + "&pName=" + parkName, UriKind.Relative)); 


     } 

这里的代码为ListBox

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
      <ListBox Height="559" HorizontalAlignment="Left" Margin="6,20,0,0" x:Name="ThemeParkPhoto" VerticalAlignment="Top" Width="444" FontSize="30" ItemsSource="{Binding}"> 
       <ListBox.ItemTemplate> 
        <DataTemplate> 
         <StackPanel Orientation="Vertical"> 
          <TextBlock x:Name="ID" Text="{Binding ID}"></TextBlock> 
          <Image x:Name="PhotoURL" Source="{Binding PhotoURL}" /> 
         </StackPanel> 
        </DataTemplate> 
       </ListBox.ItemTemplate> 
      </ListBox> 
     </Grid> 

我已删除的URL保存API,该代码会运行并填写,但为什么它不能正确地刷新列表框?

非常感谢

+0

是ThemeParkPhoto是ListBox?你也可以展示XAML吗? –

+0

我现在已经完成:) –

+0

您是否调试过该数据即将到来或'themeparkPhoto'始终为空。尝试从XAML中删除'ItemsSource =“{Binding}”',无需将列表设置为null并将其转换为Tolist'themeparkPhoto.ToList();' –

回答

1

感谢在这里被发送:C# WebClient disable cache

原来的Windows手机Web客户端缓存文件的意思,直到应用程序被刷新它永远不会重新下载。通过使用随机数生成器并将其添加到URL的时间,它将始终下载允许刷新的文件。