Social Icons

Pages

Access main window label from a separate project avoiding circular references C#-WPF


I had 2 projects in one solution. Project 1 needs a class from project 2 and project 2 needs a class of project 1.

The problem is: If I add a reference of project 1 in project 2, I cannot add a reference of project 2 in project 1 .

But I needed to access main window's label from different project class. This is how I achieved it
public class ClassfromProject2
{
public static event Action<string> MessageReceived;
// calling the message displayer
public ClassfromProject2()
{
Broadcast("this is the sample message");
}
internal static void Broadcast(string message)
{
if (MessageReceived != null)
{
MessageReceived(message);
}
}
}
view raw Project 2.cs hosted with ❤ by GitHub
Presentation project:
Using Project2;
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ClassfromProject2.MessageReceived += messagetext =>
{
status_lable.Content = messagetext;
};
}
}
view raw presentation.cs hosted with ❤ by GitHub
Enjoy !!!



1 comment:

  1. Excellent!!!!.... Thank you very much.. it works for me. Keep it up

    ReplyDelete