Vivek
Vivek

Reputation: 663

ObservableCollection is not refreshing

I am creating an application by using Windows Phone Databound app in Visual Studio 2013. I am using a ObservableCollection like below:

public ObservableCollection<ItemViewModel> Items { get; private set; }

and this Items is the ItemSource for LongListSelector.

Once app starts everything working good. In my app, there is a refresh button in app bar. I want, when I click on refresh button then ObservableCollection will display the updated values but the ObservableCollection is not refreshing.

On refresh button click, I am calling the App.ViewModel.LoadData(); function, which downloads some information from web in json format.

Code which I am using is below:

public void LoadData()
        {
            if( IsDataLoaded )
                this.Items = new ObservableCollection<ItemViewModel>();

            WebClient webClient = new WebClient();
            webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
            webClient.DownloadStringAsync(new System.Uri("http://questoons.com/"));                     
        }
private void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    if (e.Error != null)
    {
        System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
        {
            // Showing the exact error message is useful for debugging. In a finalized application, 
            // output a friendly and applicable string to the user instead. 
            System.Windows.MessageBox.Show(e.Error.Message);
        });
    }
    else
    {
       //System.Windows.Data.CollectionViewSource.GetDefaultView(this.Items).Refresh();

        int i = 0;
        dynamic dynJson = JsonConvert.DeserializeObject(e.Result);
        foreach (var item in dynJson)
        {
            String bodyText = "";
            bodyText += "Description:\n";
            bodyText += (String)item.description;

            this.Items.Add(new ItemViewModel()
            {
                ID = i.ToString(),
                AppID = "lc-details-001",
                Title = (String)item.title,
                SenderID = "",
                SenderName = "",
                Time = "",
                Type = "information",
                Body = bodyText,
                ValidTill = "",
                Subtitle = (String)item.subtitle,
                Icon = "",
                Image = "",
                ResponseURL = "",
                ResponseOptions = ""
            });

            i++;
        }
        this.IsDataLoaded = true;
    }
}

So how can I update ObservableCollection?

Upvotes: 0

Views: 684

Answers (2)

KirtiSagar
KirtiSagar

Reputation: 584

  • you need to DeserializeObject to ItemViewModel
  • loop thru the object
  • add it to the Items. Ex: Items.Add(obj);
  • you need to create the ItemViewModel based on web service response, you can try this http://json2csharp.com/ this will give you the C# object

like this:

else
{

    int i = 0;

    var dynJson = JsonConvert.DeserializeObject<ItemViewModel>(e.Result);
    foreach (var item in dynJson)
    {
        String bodyText = "";
        bodyText += "Description:\n";
        bodyText += (String)item.description;

        this.Items.Add(item);


        i++;
    }
    this.IsDataLoaded = true;
}

Upvotes: 1

Mark
Mark

Reputation: 8281

Okay the reason why the data doesn't update is that you set your observable collection to a new instance. Update your LoadData Function to this:

    public void LoadData()
    {
        if( IsDataLoaded )
            this.Items.Clear();

        WebClient webClient = new WebClient();
        webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
        webClient.DownloadStringAsync(new System.Uri("http://questoons.com/"));                     
    }

If you update the instance of your collection you will have to notify the UI with a RaisePropertyChanged.

Upvotes: 2

Related Questions