2014-05-07 79 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的時間,它將始終下載允許刷新的文件。