0

I honestly don't know how my components won't show.

basically this is my structure:

and right now i am building a 'thread' which is one of the 'name here' this will be a message preview .....' all i am asking is this: can someone lead me in the right direction. I am a java developer and getting used to c#

MessageThread m = new MessageThread("Example Name", "Hey man, How are you?", "12:34", "000000000325");
m.buildThread();

and here is this class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace FacebookMessenger
{
    class MessageThread
    {
        private String title;
        private String preview;
        private String time;
        private String userId;
        public Label lblTitle = new Label();

        public MessageThread(String title, String preview, String time, String userId)
        {
            this.title = title;
            this.preview = preview;
            this.time = time;
            this.userId = userId;
        }

        public void buildThread()
        {
            lblTitle.Content = "Name Here";
            lblTitle.FontFamily = new FontFamily("Segoe UI Light");
            lblTitle.FontSize = 28;
            lblTitle.Width = 400;
            lblTitle.Height = 50;
            lblTitle.Foreground = Brushes.Wheat;
        }
    }
}

it would be really helpful if someone helped me out thanks.

Grant Winney
  • 65,241
  • 13
  • 115
  • 165
  • I assume you are aware that manipulating the UI outside the UI thread is prohibited in both Java and C#. – Tarik Feb 19 '15 at 21:54
  • You can do it in Java by making the ui public and static. http://i.imgur.com/cTt1jbh.png http://i.imgur.com/bFMqEtX.png – user3740781 Feb 19 '15 at 21:56
  • For Java, can you refer me to an article that indicates that manipulating public static UI elements outside the UI thread is safe? – Tarik Feb 19 '15 at 21:59
  • The fact that it works in a particular case does not mean it always will. – Tarik Feb 19 '15 at 22:00
  • Well how do people build messenger apps? Do they just appear without changing ui? – user3740781 Feb 19 '15 at 22:03
  • In Java you call SwingUtilities.invokeLater from any thread that is not the EDT. – Tarik Feb 19 '15 at 22:10
  • See http://stackoverflow.com/questions/661561/how-to-update-the-gui-from-another-thread-in-c – Tarik Feb 19 '15 at 22:12

1 Answers1

0

If you're using Win Forms you need to update the labels on the UI thread, i.e,

        private void UpdateTitle(string newTitle)
        {         
            if (this.lblTitle.InvokeRequired)
            {
                this.lblTitle.BeginInvoke((MethodInvoker) delegate() { this.lblTitle.Text = newTitle; });    
            }
            else
            {
                this.lblTitle.Text = newTitle;
            }
        }

If you're using WPF you should bind the labels to a view model and use INotifyPropertyChanged when the UI needs to be refreshed.

RagtimeWilly
  • 5,265
  • 3
  • 25
  • 41