简体   繁体   中英

How do I update the GUI label from WCF callback class?

I'm trying to figure out how to access a method in my GUI Class: WinClient, from my WCF callback Class: MyCallBack. The method MyCallBack.JoinServiceCallback(string message) is working, for example if I use MessageBox.Show(message); it works.

I have a method in the GUI class, SetLabelMsg(string message){} . But I just can't figure out how to access it from the callback class. I am relatively new to C# and I have been looking at Delegate and threads and invoke. But I just can't seem to connect the dots. Hopefully someone is able to help me!

//this method should change te text of the GUI label
public class MyCallBack : IServiceCallback
{
    //this method should change the text of the GUI label
    public void JoinServiceCallback(string message)
    {
        MessageBox.Show(message + " joined the service");
    }
}


public partial class WinClient : Form
{
    public void SetLabelMsg(string message)
    {
        lblMsg.Text = message;
    }
}

I got some great help from a fellow student.

public class MyCallBack : IServiceCallback
{
    private static Action<string> _changeText = null;

public static void SetDelegate(Action<string> nAction)
{
    _changeText = nAction;
}

public void JoinServiceCallback(string message)
{
    if(_changeText != null)
    {
        _changeText(message);
    }
}
}


public partial class WinClient : Form
{

public WinClient(Customer customer)
{
    MyCallBack.SetDelegate(SetLabelMsg);

    public void SetLabelMsg(string message)
    {
        lblMsg.Text = message;
    }
}
}

If I understand the question correctly and based on the information provided. I would define a delegate that can recieve the message. Then define an event that uses it in the MyCallBack class.

/// <summary>Represents the method that can recieve a message.</summary>
/// <param name="message">The message.</param>
internal delegate void SendMessage(string message);

/// <summary>Occurrs when the JoinServiceCallback method is invoked.</summary>
public event SendMessage AfterJoinServiceCallback;

Once you have that the form can attach to the AfterJoinServiceCallback event on the and then JoinServiceCallback can raise the event.

if (AfterJoinServiceCallback != null)
{
    AfterJoinServiceCallback(message)
}

### BEGIN EDIT ###

I think the issue may have been that my original example defined 2 parameters on the delegate declaration but the invocation only passed the message .

I am glad you got something to work, however, if you end up with multiple places in the UI that need to be notified when JoinServiceCallback is invoked you should really look at Using Delegates (C# Programming Guide) . Using a delegate, the invocation from within JoinServiceCallback method would be identical but would invoke all attached target methods in the order they were attached.

public partial class WinClient : Form
{
    public WinClient()
    {
        InitializeComponent();

        // Attach the "SetLabelMsg" method to the event
        MyCallBack.AfterJoinServiceCallback += SetLabelMsg;

        // Attach the "ShowMessage" method to the event
        MyCallBack.AfterJoinServiceCallback += ShowMessage;
    }

    private void SetLabelMsg(string message)
    {
        if (!string.IsNullOrEmpty(message))
        {
            // Update the status label
            lblMsg.Text = string.Format("Time: {1}\t\tMessage: {0}", message, DateTime.Now);
        }
    }

    private void ShowMessage(string message)
    {
        if (!string.IsNullOrEmpty(message))
        {
            // Add the message to a listbox of all messages
            listLog.Items.Add(string.Format("Time: {1}\t\tMessage: {0}", message, DateTime.Now));
        }
    }
}

public class MyCallBack : IServiceCallback
{
    public delegate void SendMessage(string message);

    public static event SendMessage AfterJoinServiceCallback;

    public void JoinServiceCallback(string message)
    {
        if (AfterJoinServiceCallback != null)
        {
            AfterJoinServiceCallback(message);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM