简体   繁体   English

c# DocumentLoadedEvent 未触发

[英]c# DocumentLoadedEvent not firing

I want my program to get the latitude and longitude of the ISS from a website, so I have the following code:我希望我的程序从网站获取国际空间站的纬度和经度,所以我有以下代码:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Diagnostics;
 using System.Threading;
 using System.Speech.Synthesis;
 using System.Windows.Forms;
 using System.Net;

 namespace myprogram
 {
        class Program
        {
                static void Main(string[] args)
                {
                       try
                       {
                              WebBrowser browser1 = new WebBrowser();
                              browser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);

                              browser1.Url = new Uri("https://isstracker.spaceflight.esa.int/");
                              browser1.Visible = true;
                              browser1.ScrollBarsEnabled = false;
                              browser1.ScriptErrorsSuppressed = true;
                              browser1.AllowNavigation = false;
                       }
                       catch (Exception e) 
                       {
                            Console.WriteLine(e.ToString());
                       }

                       Main(null); //don't know if this is the best way to keep the program from closing
                }
                static void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
                {
                     Console.WriteLine("iss page loaded");
                     if (e.Url.AbsolutePath != (sender as WebBrowser).Url.AbsolutePath)
                         return;
                     string lat = (sender as WebBrowser).Document.GetElementById("isst_lat").InnerText;
                     string lon = (sender as WebBrowser).Document.GetElementById("isst_lon").InnerText;
                     Console.WriteLine("latitude is" + lat + ",longitude is" + lon);
        
                }
        }
 }

When I run the program, the WebBrowserDocumentCompletedEvent doesn't fire.当我运行程序时, WebBrowserDocumentCompletedEvent不会触发。 I have looked for solutions, but have not found one.我一直在寻找解决方案,但还没有找到。 If anyone has a soultion, thank you in advance.如果有人有灵魂,请提前感谢您。

Dude, your program exits before it does, actually, fires "document loaded" event, or even, it throws a Stack Overflow exception (not rly... it's maximum call stack exceeded, i guess... can't remember it).伙计,您的程序实际上在它退出之前就退出了,它会触发“文档加载”事件,甚至会引发堆栈溢出异常(不是 rly ......它超出了最大调用堆栈,我猜......不记得了) .

Now, answering your questions:现在,回答你的问题:

1st.第一个。 See, your program is a Console App, instead of a Windows Forms App.看,您的程序是控制台应用程序,而不是 Windows Forms 应用程序。 check out each the difference between them ( https://docs.microsoft.com/pt-br/dotnet/csharp/tutorials/console-teleprompter and https://docs.microsoft.com/en-us/windows/uwp/launch-resume/app-lifecycle ) and notice they are way way different.检查它们之间的区别( https://docs.microsoft.com/pt-br/dotnet/csharp/tutorials/console-teleprompterhttps://docs.microsoft.com/en-us/windows/uwp/ launch-resume/app-lifecycle )并注意它们的方式不同。 So If you want to see the web browser it self, you should be using windows forms application.因此,如果您想自己查看 web 浏览器,您应该使用 windows forms 应用程序。 Instead, if you are just willing to parse and transform the Response of a Web Request (I guess thats the case here), google "WebClient.DownloadString Tutorial" and enjoy.相反,如果您只是愿意解析和转换 Web 请求的响应(我猜这里就是这种情况),请搜索“WebClient.DownloadString Tutorial”并享受。 ;) ;)

2nd.第二。 This is not the best way to keep your program from closing... Since you've commented it ("//don't know if this is the best way to keep the program from closing") I'll point you to some clarifying directions...这不是阻止程序关闭的最佳方法...既然您已经评论过它(“//不知道这是否是阻止程序关闭的最佳方法”)我会为您指出一些明确方向...

Back in the day when we programmers used to drink soda in cork closed bottles, ride horses bareback, hunt our food and stuff, programs used to be called "command prompt"... is like git bash where you need to know the commands to use them in a "no user interface" environment...早在我们程序员过去常常在软木密封的瓶子里喝苏打水,骑马,打猎我们的食物和东西的日子里,程序曾经被称为“命令提示符”......就像 git bash 你需要知道命令在“无用户界面”环境中使用它们......

yep!是的! there was NO USER INTERFACE , you had to really type each command (like clicking a button), and pass the parameters (the stuff you put inside a textbox, or a checkbox checked state. for instance) in a single line to do anything.., It has been like forever, but the scars (lessons learned) are still with us!没有USER INTERFACE ,您必须真正键入每个命令(例如单击按钮),并在一行中传递参数(例如,您放入文本框中的内容,或选中 state 的复选框)以执行任何操作。 ., 就像永远一样,但伤疤(经验教训)仍然存在!

One way to keep your console from closing (can't tell you the "best". sure can tell you the one I like best.) is to listen to commands.,, While the user inputs text lines.防止控制台关闭的一种方法(不能告诉你“最好的”。当然可以告诉你我最喜欢的那个。)是听命令。,当用户输入文本行时。 you check if this line is a valid command, and tells him if it's not.你检查这行是否是一个有效的命令,如果不是,告诉他。 If the user inputs your valid "Exit" command.如果用户输入您的有效“退出”命令。 it shuts.:.它关闭了。:. (hint: the words "While" and "If" are actual programming patterns to be implemented) (提示:单词“While”和“If”是要实现的实际编程模式)

So, this is a "guidelines answer", not the "programming done it self"... if wou need it done, just comment here and I can give it a try for you... Just an advice: It is better learning than being prisioner to other's help.所以,这是一个“指南答案”,而不是“编程自己完成”......如果你需要它,只需在这里评论,我可以为你试一试......只是一个建议:这是更好的学习而不是被别人的帮助所束缚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM