好的,这就是我所拥有的。我有我的主要表单frmMain.cs
,我有一个class.cs
。我正在为我的电子邮件做RSSFeed,我收到错误:
由于其防护等级而无法进入。
在我的class.cs
上,我有以下代码:
public class RSSFeed
{
public void CheckForEmails()
{
string GmailAtomUrl = "https://mail.google.com/mail/feed/atom";
XmlUrlResolver xmlResolver = new XmlUrlResolver();
xmlResolver.Credentials = new NetworkCredential(Settings.Default.GmailUser, Settings.Default.GmailPassword);
XmlTextReader xmlReader = new XmlTextReader(GmailAtomUrl);
xmlReader.XmlResolver = xmlResolver;
try
{
XNamespace ns = XNamespace.Get("http://purl.org/atom/ns#");
XDocument xmlFeed = XDocument.Load(xmlReader);
var emailItems = from item in xmlFeed.Descendants(ns + "entry")
select new
{
Author = item.Element(ns + "author").Element(ns + "name").Value,
Title = item.Element(ns + "title").Value,
Link = item.Element(ns + "link").Attribute("href").Value,
Summary = item.Element(ns + "summary").Value
};
frmMain.MsgList.Clear();
frmMain.MsgLink.Clear();
foreach (var item in emailItems)
{
if (item.Title == String.Empty)
{
frmMain.MsgList.Add("Message from " + item.Author + ", There is no subject and the summary reads, " + item.Summary);
frmMain.MsgLink.Add(item.Link);
}
else
{
frmMain.MsgList.Add("Message from " + item.Author + ", The subject is " + item.Title + " and the summary reads, " + item.Summary);
frmMain.MsgLink.Add(item.Link);
}
}
if (emailItems.Count() > 0)
{
if (emailItems.Count() == 1)
{
frmMain.lblEmail.Text = ("You have one new email, would you like me to read it to you");
}
else
{
frmMain.lblEmail.Text("You have " + emailItems.Count() + "new emails");
}
}
else if (frmMain.QEvent == "CheckForNewEmails" && emailItems.Count() == 0)
{
frmMain.lblEmail.Text("You have no new emails"); frmMain.QEvent = String.Empty;
}
}
catch
{
frmMain.lblEmail.Text("You have submitted invalid log in information");
}
}
}
然后我在我的主表单上有一个计时器刻度事件:
public void tmrEmail_Tick(object sender, EventArgs e)
{
lblEmail.Text = ("New Emails " + RSSFeed.CheckForEmails);
}
我不理解的是当我在RSSFeed中或在我的主表单计时器上有标签时。我收到了错误。我已将所有内容更改为public
,但仍然会抛出错误。
我错过了什么,或者我没有我应该拥有的一切?
此外,我将有另一个专门用于电子邮件的表单。最好不要使用RSSFeed.cs并只编写winform代码?我唯一要做的就是在收到新电子邮件时创建标签。
有什么想法吗?
答案 0 :(得分:2)
您缺少类和方法中的static
关键字。应为public static class RSSFeed
和public static void CheckForEmails()
您还需要将frmMain的实例传递给方法。 E.g:
public static void CheckForEmails(frmMain frmMainInstance)
全部放在一起:
public static class RSSFeed
{
public static void CheckForEmails(frmMain frmMainInstance)
{
string GmailAtomUrl = "https://mail.google.com/mail/feed/atom";
XmlUrlResolver xmlResolver = new XmlUrlResolver();
// ... rest of your code ...
}
}
对它的调用就像是:
public void tmrEmail_Tick(object sender, EventArgs e)
{
// The following line will produce a compile error because
// CheckForEmails doesn't return a value
// lblEmail.Text = ("New Emails " + RSSFeed.CheckForEmails(this);
// Try this instead:
RSSFeed.CheckForEmails(this);
}
请注意,我假设tmrEmail_Tick
是frmMain
中的方法,因此我将this
作为参数传递给CheckForEmails
。< / p>
您可以实例化RSSFeed
的实例,而不是使CheckForEmails
和RSSFeed
为静态:
public void tmrEmail_Tick(object sender, EventArgs e)
{
RSSFeed feed = new RSSFeed();
feed.CheckForEmails(this);
}
请注意,您仍需要将frmMain实例作为参数传递给CheckForEmails
。