-1


我有一个框架,它具有在引发另一个类中的事件时更新框架的功能。我有“IRCClient”和“MainFrame”类。IRCClient 类有一个事件'OnMessageRecvd',MainFrame 有一个函数'HandleNewMessageReceived'。在 MainFrame 类中,我有变量“CurrentServer”和“CurrentChannel”来指示当前向用户显示的服务器上的哪个频道。

现在,当我在按钮的回调中设置“CurrentServer”和“CurrentChannel”时,它们有一个值,一切都很好。但是,当 IRCClient 的 'OnMessageRecvd' 事件调用 'HandleNewMessageReceived' 函数时,CurrentServer 和 CurrentChannel 都等于 MainFrame 的构造函数中声明的任何值(null)。

有谁知道这种行为的根源是什么?提前非常感谢。

编辑:下面是代码,我只发布了有问题的代码(任何使用 CurrentChannel 和 CurrentServer 属性的函数)并剪掉了不相关的代码。

// Main page, shows chat history.
public sealed partial class MainPage : LIRC.Common.LayoutAwarePage
{
    private uint maxMessages;
    IRCClient ircc;
    IRCHistory irch;
    string CurrentServer, CurrentChannel;

    // Does all the setup for this class.
    public MainPage()
    {
        this.InitializeComponent();

        ircc = App.ircc; // This is a global variable in the 'App' class.
        ircc.OnMessage += NewMessageReceived;

        irch = App.irch; // This is also a global variable in the 'App' class.
        currentChannel = currentServer = null;
    }

    // Restores the previous state.
    protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
    {
        if (pageState != null)
        {
            if(pageState.ContainsKey("viewedChannel"))
            {
                // Retrieve required info.
                string[] viewedChannelTokens = (pageState["viewedChannel"] as string).Split('.');
                CurrentChannel = viewedChannelTokens[0];
                CurrentServer = viewedChannelTokens[1];

                // If the saved channel or server got corrupt
                if (string.IsNullOrEmpty(CurrentChannel) || string.IsNullOrEmpty(CurrentServer))
                {
                    // Check if a channel is open, if so, select it.
                    *snip* // Non-relevant code.
                }

                // Clear and load required history.
                ClearHistory();
                if(CurrentServer != null && CurrentChannel != null)
                    LoadHistory(CurrentServer, CurrentChannel);
            }
        }

        // Create buttons that switch to a channel
        *Snip* // Calls AddChannelButton
    }

    // Creates a button that, when clicked, causes the ChatHistoryView to display the ChannelHistory.
    void AddChannelButton(string Server, string Channel)
    {
        Button btn = new Button();
        btn.Content = Channel + "\n" + Server;
        btn.Width = 150;

        // A function to switch to another channel.
        btn.Click += (e, s) =>
        {
            ClearHistory(); // Clears the ChatHistoryVi.ew field.
            LoadHistory(Server, Channel); // Does the actual loading of the channel history

            CurrentChannel = Channel;
            CurrentServer = Server;
        };

        ChannelBar.Children.Add(btn);
    }

    // The function that is called by the IRCClient.OnMessageRecv event.
    public void NewMessageReceived(ref DataWriter dw, IRCServerInfo ircsi, IRCClient.RecvMessage recvmsg)
    {
        if (ircsi.Name == CurrentServer && CurrentChannel == recvmsg.recipient)
        {
            AddMessage(DateTimeToTime(DateTime.UtcNow), recvmsg.author, recvmsg.message);
        }
    }
}

// Responsible for creating, managing and closing connections.
public class IRCClient
{
    // A structure that describes a message.
    public struct RecvMessage
    {
        public string author;   // Nickname
        public string realName;
        public string ipAddress;
        public string recipient; // Indicates in what channel or private converstion.
        public string message;   // The actual message
    };

    // Describes how a function that handles a message should be declared.
    public delegate void MessageHandler(ref DataWriter dw, IRCServerInfo ircsi, RecvMessage msg);

    // Gets raised/called whenever a message was received.
    public event MessageHandler OnMessage;
}
4

1 回答 1

0

目前尚不清楚您所说的内容发生了什么,但是如果变量设置为您在检查它们时在构造函数中设置的值 - 这意味着您在期望它们被更改时尚未更改它们或者你设置一些其他变量的值,而不是你认为的那些。

不过,这些只是猜测,如果不显示代码,您不能期待更多的猜测。

于 2012-11-25T22:52:41.447 回答