Windows Phone刷新按钮不工作

本文关键字:工作 按钮 刷新 Phone Windows | 更新日期: 2023-09-27 18:02:22

我有一个Windows phone应用程序,它从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));

        }
下面是列表框的代码
<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,该代码确实运行并填充它,但为什么它不能正确刷新列表框?

多谢

Windows Phone刷新按钮不工作

感谢发送到这里:c# WebClient禁用缓存

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