0

私のwinフォームアプリには、リストボックスとテキストボックスがあり、アプリはサーバーからメールを取得し、リストボックスに件名などを表示します。リストボックスをクリックすると、本文がテキストボックスに表示されます。問題は、選択したインデックス変更イベントで以下のコード全体を繰り返して動作させる必要があることです。そうしないと、「現在のコンテキストに存在しません」というエラーが発生し、アプリの速度が低下します。

// 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;
        }

このコードを使用すると、「現在のコンテキストにバンドルが存在しません」というエラーが表示されます。このエラーを修正するにはどうすればよいですか?

4

2 に答える 2

1

関心のあるオブジェクトが必要なコンテキストで利用できるように、コードを再設計する必要があるようです。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;
  }
 }

 ...
}
于 2010-10-16T14:24:50.543 に答える
0

プロジェクトのプロパティを確認し、両方のプロジェクトが同じフレームワークをターゲットにしていることを確認してください。通常、これは、1 つが .Net Framework 4 を指し、もう 1 つが .Net Framework 4 Client Profile を指している場合に発生します。

ありがとう、セバスチャン

于 2012-06-23T21:04:31.067 に答える