【问题标题】:Does not exist in current context c#在当前上下文中不存在 c#
【发布时间】:2010-10-16 13:50:59
【问题描述】:

在我的 win forms 应用程序中,我有一个列表框和一个文本框,该应用程序从服务器获取电子邮件并在列表框中显示主题等,当我单击列表框时,正文显示在文本框中。问题是我必须在选定的索引更改事件中重复下面的整个代码才能使其正常工作,否则我会收到“在当前上下文中不存在”错误,这会减慢应用程序的速度。

// Create an object, connect to the IMAP server, login,
        // and select a mailbox.
        Chilkat.Imap imap = new Chilkat.Imap();
        imap.UnlockComponent("");
        imap.Port = 993;
        imap.Ssl = true;
        imap.Connect("imap.gmail.com");
        imap.Login("user@email.com", "pass");
        imap.SelectMailbox("Inbox");

        // Get a message set containing all the message IDs
        // in the selected mailbox.
        Chilkat.MessageSet msgSet;
        msgSet = imap.Search("ALL", true);

        // Fetch all the mail into a bundle object.
        Chilkat.EmailBundle bundle = new Chilkat.EmailBundle();
        bundle = imap.FetchBundle(msgSet);

        // Loop over the bundle and display the From and Subject.
        Chilkat.Email email;
        int i;
        for (i = 0; i < bundle.MessageCount - 1; i++)
        {

            email = bundle.GetEmail(i);
            listView1.Items.Add(email.From + ": " + email.Subject).Tag = i;


            richTextBox1.Text = email.Body;

        }

        // Save the email to an XML file
        bundle.SaveXml("bundle.xml");

这是我想在选定的索引更改事件中使用的代码:

 if (listView1.SelectedItems.Count > 0)
        {
            richTextBox1.Text = bundle.GetEmail((int)listView1.SelectedItems[0].Tag).Body;
        }

当我使用此代码时,我收到错误“当前上下文中不存在捆绑包”;如何解决此错误?

【问题讨论】:

    标签: c# winforms email scope selectedindexchanged


    【解决方案1】:

    看来您必须重新设计您的代码,以便您感兴趣的对象在需要它的上下文中可用。一种解决方案可能是:

    class Form1
    {
     ...
    
     // You need to have the bundle available in your event handler, so it should be 
     // a field (or property) on the form:
     Chilkat.EmailBundle bundle;
    
     // Call this e.g. on start up and possibly when a
     // refresh button is clicked:
     protected void RefreshMailBox()
     {
      Chilkat.Imap imap = new Chilkat.Imap();
      imap.UnlockComponent("");
      imap.Port = 993;
      imap.Ssl = true;
      imap.Connect("imap.gmail.com");
      imap.Login("user@email.com", "pass");
      imap.SelectMailbox("Inbox");
    
      Chilkat.MessageSet msgSet = imap.Search("ALL", true); 
      bundle = imap.FetchBundle(msgSet);
     }
    
     protected void YourEventHandler()
     {
      if (listView1.SelectedItems.Count > 0)
      {
       // bundle is now accessible in your event handler:
       richTextBox1.Text = bundle.GetEmail((int)listView1.SelectedItems[0].Tag).Body;
      }
     }
    
     ...
    }
    

    【讨论】:

      【解决方案2】:

      检查项目属性并确保两个项目都针对相同的框架。通常这种情况发生在一个指向 .Net Framework 4 而另一个指向 .Net Framework 4 Client Profile

      谢谢, 塞巴斯蒂安

      【讨论】:

        猜你喜欢
        • 2018-03-21
        • 1970-01-01
        • 2019-02-20
        • 1970-01-01
        • 2014-12-06
        • 1970-01-01
        • 1970-01-01
        • 2023-03-30
        相关资源
        最近更新 更多