2012-11-25 29 views
-1


我有一個框架,它有一個函數,當另一個類中的事件被引發時更新框架。 我有班'IRCClient'和'MainFrame'。 IRCClient類有一個事件'OnMessageRecvd',MainFrame有一個'HandleNewMessageReceived'函數。在MainFrame類中,我有變量'CurrentServer'和'CurrentChannel'來指示當前向用戶顯示哪個服務器上的哪個通道。win8框架代理

現在,當我在按鈕回調中設置'CurrentServer'和'CurrentChannel'時,它們有一個值,一切正常。但是,當'HandleNewMessageReceived'函數被IRCClient的'OnMessageRecvd'事件調用時,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; 
} 

回答

0

目前還不清楚是什麼,從你說的情況發生,但如果變量設置爲你當你檢查它們的構造函數中設置的值 - 這意味着,要麼你還沒有被時間改變了他們還沒有你期待他們被改變,或者你設定了一些其他變量的值,而不是你認爲的那些變量的值。

這些只是猜測,你不能期望超過猜測而不顯示你的代碼。

+0

更新後,現在包含代碼。 – Damacustas