Windows Phone刷新按钮不起作用

时间:2014-05-07 13:00:04

标签: c# windows-phone-8

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

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

我删除了用于保存API的URL,该代码确实运行并填充它,但为什么它不能正确刷新列表框?

非常感谢

1 个答案:

答案 0 :(得分:1)

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

原来Windows手机网络客户端缓存该文件意味着它永远不会再次下载它,直到刷新应用程序。通过使用随机数生成器并将其添加到URL的后续,它将始终下载文件以允许刷新。