Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
Newbie here so sorry for the dumb question but I'm having a real problem with this simple little issue for some reason. I have a label and button on WPF window and I'm trying to update label from a 2nd static class.cs code file. For exmple:
MainWindow.xaml.cs
internal static namespace.MainWindow myMainWindow;


C#
public delegate void Update_lbl1(string text);
public void refresh(string text)
{
if (lbl_1.Dispatcher.CheckAccess())
{     lbl_1.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new Update_lbl1(refresh), text);
return;
}       
lbl_1.Content = text;
}
private void buttonKonekcija_Click(object sender, RoutedEventArgs e)
{
  class.start();
}


in my class.cs i have:

C#
public static void Start()
{
  try
   {        
MainWindow.myMainWindow.Dispatcher.BeginInvoke(new Action(delegate() { MainWindow.myMainWindow.lbl_1.Content = "Label text is change"; }));  
// have error here   Object reference not set to an instance of an object.    
   }
catch (Exception e)
 {
MainWindow.myMainWindow.Dispatcher.BeginInvoke(new Action(delegate() { MainWindow.myMainWindow.lbl_1.Content = e.message; })); 

}
}


I don't understand why this is happening, can someone please teach me how to do this right.I am searching 5 days to solve this problem and no hope :((
Thank you
Posted
Updated 22-Feb-12 21:37pm
Comments
Sergey Alexandrovich Kryukov 23-Feb-12 3:33am    
What an abuse! OK, could you explain an ultimate goal of all this?
And... happening -- what?
The code makes no sense at all...
--SA
FeniksReborn 23-Feb-12 3:59am    
Sorry for my bad english... The goal is to learn update UI form from static class. How can you show error message from static class on window in label?
Maybe am am wrong, if you can show me better way to program this...
Thank you
FeniksReborn 23-Feb-12 4:05am    
Real class.cs
public static void Start()
{
ConnectionInfo connection = new ConnectionInfo();
Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
SocketInfo info = new SocketInfo();
info.socket = clientSocket;
lock (socketList)
{
socketList.Add(info);
}
try
{
info.socket.Connect(clientEndpoint);
MainWindow.myMainWindow.Dispatcher.BeginInvoke(new Action(delegate() { MainWindow.myMainWindow.lbl_1.Content = "Label text is change"; }))
info.socket.BeginReceive(info.buffer, 0, info.buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), info);
flag_e = true;
flag_b = true;
}
catch (Exception e)
{
flag_e = false;
lock (socketList)
{
socketList.Remove(info);
}
MainWindow.myMainWindow.Dispatcher.BeginInvoke(new Action(delegate() { MainWindow.myMainWindow.lbl_1.Content = e.message; }));

}
}

As you say, this is a fairly simple problem. It's just that you've made it over complicated.

Take a look at http://tap-source.com/?p=160[^] to show you simple MVVM and binding. Get to know your ViewModels and they'll make problems like this as trivial as they should be ;-)
 
Share this answer
 
Comments
FeniksReborn 23-Feb-12 8:34am    
View Models are promis land :)It's working perfectly...
Thank you
As far as I understand you are trying to implement Singleton template for accessing main form. And it looks like error about myMainWindow;

To do it right do the following in your MainWindow class:
C#
private static MainWindow _instance = null;

public static MainWindow Instance
{
 get
 {
  if (_instance == null)
  {
   _instance = new MainWindow(); 
  }
  return _instance;
 }
}


then in your another class you can access instance of MainWindow like this:

C#
MainWindow.Instance.lbl_1.Content = "Label text is change";
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900