Frage

Ich habe auf dieses ganze Wochenende fest gewesen und kläglich gescheitert!
Bitte helfen Sie mir meinen Verstand zurück zu kratzen !!

Ihre Herausforderung

Für meine erste Silverlight-Anwendung dachte ich, es würde Spaß machen, die World of Warcraft zu verwenden armoury die Zeichen in meiner Gilde aufzulisten. Dies beinhaltet eine asynchrones von Silverlight (duh!) Machen auf die WoW Rüstkammer, die XML basiert. SIMPLE EH?

Werfen Sie einen Blick auf diesen Link und die Quelle öffnen. Sie werden sehen, was ich meine: http://eu.wowarmory.com/guild-info.xml? r = Eonar & n = Gifted und Talentierte

Im Folgenden finden Sie Code für das Erhalten der XML (um den Anruf zu ShowGuildies wird mit dem zurückgegebenen XML zu bewältigen - ich dies lokal getestet haben, und ich weiß, es funktioniert).

Ich habe nicht bekommen die erwartete zurück XML überhaupt verwaltet.

Weitere Informationen:

  • Wenn der Browser der Transformation des XML-fähig ist, es so tun, sonst wird HTML zur Verfügung gestellt werden. Ich denke, es untersucht die Useragent
  • Ich bin ein erfahrener asp.net Web-Entwickler C # einfach so gehen, wenn Sie reden über native Windows starten Forms / WPF
  • Ich kann nicht scheinen, um die Useragent-Einstellung in .net 4.0 zu setzen - scheint nicht eine Eigenschaft von der HttpWebRequest Objekt aus irgendeinem Grund zu sein -. Ich denke, es verwendet, verfügbar sein
  • Silverlight 4.0 (erstellt als 3,0 ursprünglich, bevor ich auf 4.0 meine Installation von Silverlight aktualisiert)
  • Erstellt C # 4.0
  • mit
  • Bitte beschreiben Sie, als ob Sie zu einem Web-Entwickler sprechen und keine richtige Programmierung lol!

Im Folgenden finden Sie den Code - es sollte die XML aus der wow Rüstkammer zurückkehren

.
private void button7_Click(object sender, RoutedEventArgs e)
{
   // URL for armoury lookup
                string url = @"http://eu.wowarmory.com/guild-info.xml?r=Eonar&n=Gifted and Talented";

                // Create the web request
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);

                // Set the user agent so we are returned XML and not HTML
                //httpWebRequest.Headers["User-Agent"] = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)";

                // Not sure about this dispatcher thing - it's late so i have started to guess.
                Dispatcher.BeginInvoke(delegate()
                {
                    // Call asyncronously
                    IAsyncResult asyncResult = httpWebRequest.BeginGetResponse(ReqCallback, httpWebRequest);

                    // End the response and use the result
                    using (HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.EndGetResponse(asyncResult))
                    {
                        // Load an XML document from a stream
                        XDocument x = XDocument.Load(httpWebResponse.GetResponseStream());

                        // Basic function that will use LINQ to XML to get the list of characters.
                        ShowGuildies(x);
                    }
                });
            }

            private void ReqCallback(IAsyncResult asynchronousResult)
            {
                // Not sure what to do here - maybe update the interface?
            }

hoffe wirklich, dass jemand da draußen kann mir helfen!

Danke mucho! Dan.

PS Ja, ich habe die Ironie im Namen der Zunft bemerkt:)

War es hilfreich?

Lösung

First, Dispatcher.BeginInvoke is only needed when you're on another thread than the UI thread (where everything silverlight/WPF related happens). On a click event, you're already in the UI thread so there's no need to call it.

Second, BeginGetResponse is an asynchronous operation so when it has finished, it will call a callback function on another thread, here ReqCallback. It's in this method that you can call EndGetResponse. This pattern applies to every BeginX/EndX you'll find in the framework.

However, since you're in another thread, you'll need to use BeginInvoke to dispatch a method back to the UI thread.

The code will look like this:

private void button7_Click(object sender, RoutedEventArgs e) {
    string url = @"http://eu.wowarmory.com/guild-info.xml?r=Eonar&n=Gifted and Talented";
    HttpWebRequest httpWebRequest = (HttpWebRequest) WebRequest.Create(url);
    httpWebRequest.BeginGetResponse(ReqCallback, httpWebRequest);
}

private void ReqCallback(IAsyncResult asyncResult)
{
    HttpWebRequest httpWebRequest = (HttpWebRequest) asyncResult.AsyncState;
    using (HttpWebResponse httpWebResponse = (HttpWebResponse) httpWebRequest.EndGetResponse(asyncResult))
    {
        XDocument x = XDocument.Load(httpWebResponse.GetResponseStream());
        Dispatcher.BeginInvoke((Action) (() => ShowGuildies(x)));
    }
}

Note that you can also process the XML in the thread and use the dispatcher only to send back guildies to the UI, to avoid freezing the UI if the XML is very long to parse (shouldn't be the case).

Edit: Corrected the code. You only have to implement ShowGuildies. Regarding the internet connectivity and delays, since the operation occurs in another thread the UI won't freeze. You might consider showing a loading animation or something though.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top