How to add tweets to a listbox wpf C#

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 1044 Views - Last Post: 02 May 2019 - 09:22 AM Rate Topic: -----

#1 Stacker25   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 09-December 18

How to add tweets to a listbox wpf C#

Posted 17 April 2019 - 10:27 AM

I've been given twitter files to use to make a twitter program in C# - This includes a twitter account and the tweets. I'm trying to display all the tweets in a listbox but the code I'm using doesn't display the tweets in the listbox at run time, the program compiles but no tweets are displayed and I can't work out why.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Windows;
using System.Windows.Threading;


namespace Twitter
{

    public partial class MainWindow : Window
    {
        public MainWindow()

        {
            InitializeComponent();
            
        }


        static void main (string[] args)
        {
            using (StreamReader r = new StreamReader("@1234_AllTweets.json"))
            {

       
                string json = r.ReadToEnd();
                
                List<Tweet> tweets = JsonConvert.DeserializeObject<List<Tweet>>(json, new IsoDateTimeConverter { DateTimeFormat = "ddd MMM dd HH:mm:ss zz00 yyyy" });

                

                foreach (Tweet tweet in tweets)
                {
                    ((MainWindow)Application.Current.MainWindow).twittermessage.Items.Add(tweet.text);

                }
         
        }

            using (StreamReader r = new StreamReader("@1234_Followers.json"))
            {
                string json = r.ReadToEnd();
                List<User> users = JsonConvert.DeserializeObject<List<User>>(json, new IsoDateTimeConverter { DateTimeFormat = "ddd MMM dd HH:mm:ss zz00 yyyy" });

             
            }

            using (StreamReader r = new StreamReader("@1234_User.json"))
            {
                string json = r.ReadToEnd();
                User user = JsonConvert.DeserializeObject<User>(json, new IsoDateTimeConverter { DateTimeFormat = "ddd MMM dd HH:mm:ss zz00 yyyy" });

    
            }

        }
    }
}






This post has been edited by Stacker25: 17 April 2019 - 10:28 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How to add tweets to a listbox wpf C#

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15058
  • View blog
  • Posts: 60,125
  • Joined: 12-June 08

Re: How to add tweets to a listbox wpf C#

Posted 17 April 2019 - 10:52 AM

Where are you binding your collection to your UI object?
Was This Post Helpful? 0
  • +
  • -

#3 Stacker25   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 09-December 18

Re: How to add tweets to a listbox wpf C#

Posted 17 April 2019 - 11:17 AM

View Postmodi123_1, on 17 April 2019 - 10:52 AM, said:

Where are you binding your collection to your UI object?


I haven't done that as I'm not sure how to do it - I'm still new to c#
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6913
  • View blog
  • Posts: 23,493
  • Joined: 05-May 12

Re: How to add tweets to a listbox wpf C#

Posted 17 April 2019 - 11:17 AM

He is not using binding. On line 40, he is trying to directly add items as if he was using WinForms instead of WPF.
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15058
  • View blog
  • Posts: 60,125
  • Joined: 12-June 08

Re: How to add tweets to a listbox wpf C#

Posted 17 April 2019 - 11:30 AM

.ItemsSource is pretty handy. :^:

https://docs.microso...framework-4.7.2
Was This Post Helpful? 0
  • +
  • -

#6 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 223
  • View blog
  • Posts: 1,260
  • Joined: 05-December 13

Re: How to add tweets to a listbox wpf C#

Posted 17 April 2019 - 12:48 PM

using (StreamReader r = new StreamReader("@1234_Followers.json"))

What is this for and how will it be used? I'll take a wild guess and assume your application will allow your users to track their followers and who unfollows them? It might be worth noting, that this type of application is a direct violation of Twitter Services. And since I see no reference to Twitter's API, It seems evident so I will assume you're scraping tags to collect the info by webrequests or alike. Not a good idea...

As for your code, look into MVVM as a way of moving forward. And this link will give you a run down on WPF, but if you want to skip right to Binding in WPF, click here. Trust me, you don't want to do things the hard way. WPF is not like Winforms, and if you insist on the simplicity of the tradiditional Winforms way, perhaps change your application type to that instead, as WPF has a learning curve, and learning it isn't for everyone it seems. As a side note, once Twitter get wind of your App, it will be banned, and if you are using their API, you will be restricted permanently. It's not a good idea to offer services which are against their tos/rules.
Was This Post Helpful? 0
  • +
  • -

#7 Stacker25   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 09-December 18

Re: How to add tweets to a listbox wpf C#

Posted 17 April 2019 - 01:35 PM

I'm finding all the tutorials and things I read online difficult to understand, I have tried this in the xaml code, but I'm obviously still missing something or not doing it correctly..

 <ListBox x:Name="twittermessage" 
                 ItemsSource="{Binding Path=tweets}"
                 HorizontalAlignment="Left" Height="208" Margin="89,201,0,0" VerticalAlignment="Top" Width="336"/>


Was This Post Helpful? 0
  • +
  • -

#8 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 223
  • View blog
  • Posts: 1,260
  • Joined: 05-December 13

Re: How to add tweets to a listbox wpf C#

Posted 17 April 2019 - 02:29 PM

Too note; If tweets is not executing, then the bind is not going to do anything as no property has changed. And have you read what I linked in reference to binding? How did you go about implementing the code behind for the INotifyPropertyChanged event? You can setup an event to fire when the property value changes... read the related docs on the first link I gave you, and you will have learned some of this. If you need to ask questions, you didn't read enough of it to understand what it's explaining, and I doubt you read it all and learned it in 40 minutes....

at a glance of your code on SO, it doesn't look like you know what you're doing https://stackoverflo...at-run-time-wpf

On line 28, what happens then the reader finishes reading? Put a break point on your code and start debugging to find out.
Was This Post Helpful? 0
  • +
  • -

#9 Stacker25   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 09-December 18

Re: How to add tweets to a listbox wpf C#

Posted 01 May 2019 - 04:51 AM

I'm trying to get the content of what is stored in the Tweet JSON file to display in a label but when I run the program I'm getting a error:
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Twitter.Tweet' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path '', line 1, position 1.'


I've tried to use the same concept as I did to display the user name in a label but it doesn't seem to be working and I can't figure out why. I'm also using a WPF.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Windows;
using System.Windows.Threading;
using System.Collections.ObjectModel;
using System.Windows.Controls;


namespace Twitter
{

    public partial class MainWindow : Window
    {
        

        public MainWindow()

        {
            InitializeComponent();

            using (StreamReader r = new StreamReader("@1234_User.json"))
            {
                string json = r.ReadToEnd();
                User user = JsonConvert.DeserializeObject<User>(json, new IsoDateTimeConverter { DateTimeFormat = "ddd MMM dd HH:mm:ss zz00 yyyy" });

                


                label.Content = $"User name: " + user.screen_name.ToString();

            }


                using (StreamReader r = new StreamReader("@1234_AllTweets.json"))
                {


                    string json = r.ReadToEnd();


                    Tweet tweet = JsonConvert.DeserializeObject<Tweet>(json, new IsoDateTimeConverter { DateTimeFormat = "ddd MMM dd HH:mm:ss zz00 yyyy" });

                // List<Tweet> tweets = JsonConvert.DeserializeObject<List<Tweet>>(json, new IsoDateTimeConverter { DateTimeFormat = "ddd MMM dd HH:mm:ss zz00 yyyy" });
                box.Content = tweet.text;



                    

                }
            }



This post has been edited by Stacker25: 01 May 2019 - 04:52 AM

Was This Post Helpful? 0
  • +
  • -

#10 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6913
  • View blog
  • Posts: 23,493
  • Joined: 05-May 12

Re: How to add tweets to a listbox wpf C#

Posted 01 May 2019 - 05:10 AM

Please show us what your Tweet class looks like.
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6913
  • View blog
  • Posts: 23,493
  • Joined: 05-May 12

Re: How to add tweets to a listbox wpf C#

Posted 01 May 2019 - 05:17 AM

Also show us the first few lines of the data file you are feeding in.
Was This Post Helpful? 0
  • +
  • -

#12 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6795
  • View blog
  • Posts: 28,082
  • Joined: 12-December 12

Re: How to add tweets to a listbox wpf C#

Posted 01 May 2019 - 05:38 AM

That is a very clear and descriptive error message. It is most likely that you have an array of objects rather than a single object (Tweet). You either want to reference an individual object within the array, or deserialize to a collection of Tweets. But posting the requested details will also clarify this.
Was This Post Helpful? 0
  • +
  • -

#13 Stacker25   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 09-December 18

Re: How to add tweets to a listbox wpf C#

Posted 01 May 2019 - 07:35 AM

Tweet class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Twitter
{
    class Tweet
    {
        public DateTime created_at;
        public string text;
        public string name;
        public string statuses_count;

    }
}




Was This Post Helpful? 0
  • +
  • -

#14 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6913
  • View blog
  • Posts: 23,493
  • Joined: 05-May 12

Re: How to add tweets to a listbox wpf C#

Posted 01 May 2019 - 07:38 AM

On line 49, it looks like our OP tried to deserialize a List<Tweet> just like adviced by the error message. I assume that also failed.
Was This Post Helpful? 1
  • +
  • -

#15 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6795
  • View blog
  • Posts: 28,082
  • Joined: 12-December 12

Re: How to add tweets to a listbox wpf C#

Posted 01 May 2019 - 07:43 AM

Maybe it failed with a different error ;)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2