1

I have a Windows phone application which gets a list of photos URLs from a SQL database depending what it uploaded. The issue i have is users can add their own photos to that list but it does not refresh the list on the page so i added a refresh to re run the code but it still does not run. Well the code runs but does not update the list box.

//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));


        }

Here the Code for the 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>

I have removed the URL to save the API, That code does run and fill it but why does it not refresh the List Box correctly?

Many Thanks

Laurent Etiemble
  • 27,111
  • 5
  • 56
  • 81
Ryan Bowden
  • 171
  • 1
  • 12
  • Is ThemeParkPhoto is ListBox? and can you show the XAML for that as well? – Haseeb Asif May 07 '14 at 13:06
  • Have you debugged that data is coming or 'themeparkPhoto' is always empty. Try to remove `ItemsSource="{Binding}"` from XAML, no need to set the list to null and convert it to Tolist `themeparkPhoto.ToList();` – Haseeb Asif May 07 '14 at 13:30
  • Right just had a Look When Debugging, and noticed the File its downloading is the same old one. like its cache it somewhere how can i make sure it downloads a fresh one? With the new Pictures on it. The code is working just the file it getting is old and when i hit that REST url it showing the right one straight away. – Ryan Bowden May 07 '14 at 14:03
  • 1
    Well issue could be something else you can have a look on this http://stackoverflow.com/questions/3812089/c-sharp-webclient-disable-cache – Haseeb Asif May 07 '14 at 15:03
  • Thank you, That was the solution! – Ryan Bowden May 07 '14 at 18:58

1 Answers1

1

Thank to being sent here : C# WebClient disable cache

Turns out that Windows phone web client caches the file meaning it never download it again until the app is refreshed. By using a random number generator and adding it to the then of the URL it will always download the file allowing for a refresh.

Community
  • 1
  • 1
Ryan Bowden
  • 171
  • 1
  • 12