C#,传递事件不起作用

时间:2011-03-12 08:51:32

标签: c#

我有这个代码可以工作。

主要形式:

FileTransferManager fm = new FileTransferManager();

...
public FrmMain()
{
InitializeComponent();
...
fm.OnFile += fm_OnFile;
}

...
    void fm_OnFile(object sender, FileTransferEventArgs e)
    {
        var recvFile = new FrmReceiveFile(fm, e);
        recvFile.Show();
        e.Accept = true;
    }

和FrmReceiveFile:

public partial class FrmReceiveFile : Form
{
    private FileTransferManager fm;
    private FileTransferEventArgs ftea;
    public FrmReceiveFile(FileTransferManager ftm, FileTransferEventArgs fea)
    {
        InitializeComponent();

        fm = ftm;
        ftea = fea;

        Text = "File transfer: " + ftea.Jid;

        lblSize.Text = Util.HumanReadableFileSize(ftea.FileSize);
        lblFileName.Text = ftea.Filename;
        lblDescription.Text = ftea.Description;

        fm.OnError += fm_OnError;
        fm.OnEnd += fm_OnEnd;
        fm.OnStart += fm_OnStart;
        fm.OnProgress += fm_OnProgress;
    }


    void fm_OnStart(object sender, FileTransferEventArgs e)
    {
        MessageBox.Show("file transfer started"); ///// THIS APPEARS & EVERYTHING WORKS!
        if (e.Sid != ftea.Sid)
            return;
    }
...

以下是我的代码,所有代码都采用一种形式,但不知何故它不起作用。

public partial class Form1 : Form
    {

        private string sid = "";

        FileTransferManager fmout = new FileTransferManager(); //// this FileTransferManager is for outgoing files
        FileTransferManager fmin = new FileTransferManager();  //// this FileTransferManager is for incomeing files
        FileTransferEventArgs fta = new FileTransferEventArgs();
        Jid _jid = new Jid();


        public Form1()
        {
            InitializeComponent();

            fmout.OnError += fmout_OnError;
            fmout.OnEnd += fmout_OnEnd;
            fmout.OnStart += fmout_OnStart;
            fmout.OnProgress += fmout_OnProgress;

            fmout.XmppClient = xmppClient;
            fmin.XmppClient = xmppClient;

            fmin.OnFile += fmin_OnFile;
            fmin.OnEnd += fmin_OnEnd;
            fmin.OnStart += fmin_OnStart;
            fmin.OnProgress += fmin_OnProgress;
        }

        ////////////////////////////////////////////////////////////////////////////////////////////

        void fmin_OnFile(object sender, FileTransferEventArgs e)
        {
            DisplayEvent("INCOMING FILE: " + e.Filename + " - " + e.FileSize); ///// THIS APPEARS CORRECTLY
            e.Accept = true;
            fta = e;
        }


        void fmin_OnStart(object sender, FileTransferEventArgs e) /// THIS WON'T START! :(
        {
            MessageBox.Show("Incoming file!"); /// THIS WON'T START! :( 
            if (e.Sid != fta.Sid)
                return;
        }

看起来 e.Accept = true; 没有启动 fmin_OnStart ...任何想法可能会出现什么问题?

谢谢!

1 个答案:

答案 0 :(得分:1)

在两段代码中的差异(可以从您共享的代码中得出)是在第一个代码中,当您调用“OnFileHandler”时,您正在注册“fm.OnStart + = fm_OnStart”事件,而在另一个(不工作)中,即使未调用OnFileHandler,也会提前执行此操作。

尽管如此,作为FileTransferManager的用户,我认为这不重要。

不过,您可以在第二个代码中尝试相同的操作..所以请按照以下方式进行操作。

  void fmin_OnFile(object sender, FileTransferEventArgs e)        
 {     fmin.OnStart += fmin_OnStart; 
DisplayEvent("INCOMING FILE: " + e.Filename + " - " + e.FileSize); 
e.Accept = true;             fta = e;  }

如果有效,我宁愿质问FileTransferManager的程序员。