开发者

Return values from XDocument with events

I wrote a class ReasXML the basically read only XML files. You see I worked withe events.. Now the problem is I want to use the result of lsTags or I want the values of my XML file. The basic thought to do that: make the function a return type List for the method LoadXMLFile and XMLFileLoaded. But I receive an error that, has the wrong return type.

Can someone help me with this of give me an example with events how to ret开发者_C百科urn variables?

Thanks in advance!

public void LoadXMLFile() {
        WebClient xmlClient = new WebClient();
        xmlClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(XMLFileLoaded);
        xmlClient.DownloadStringAsync(new Uri("codeFragments.xml", UriKind.RelativeOrAbsolute));
    }
    protected void XMLFileLoaded(object sender, DownloadStringCompletedEventArgs e) {
        if (e.Error == null) {
            string xmlData = e.Result;
            XDocument xDoc = XDocument.Parse(xmlData);
            var tagsXml = from c in xDoc.Descendants("Tag") select c.Attribute("name");
            List<Tag> lsTags = new List<Tag>();

            foreach (string tagName in tagsXml) {
                Tag oTag = new Tag();
                oTag.name = tagName;
                var tags = from d in xDoc.Descendants("Tag")
                           where d.Attribute("name").Value == tagName
                           select d.Elements("oFragments");
                var tagXml = tags.ToArray()[0];

                foreach (var tag in tagXml) {
                    CodeFragments oFragments = new CodeFragments();
                    oFragments.tagURL = tag.Attribute("tagURL").Value;
                    oTag.lsTags.Add(oFragments);
                }
                lsTags.Add(oTag);
            }
            //List<string> test = new List<string> { "a","b","c" };
            lsBox.ItemsSource = lsTags;
        }
    }


Event handlers can be any delegate type, not just EventHandler.

If you want to return a result, simply change the event to use

Func<CustomEventArgs, YourReturnType>

Here is some sample code:

using System;

class Program
{
    public class Ev
    {
        public int? RaiseSomeEvent()
        {
            if (SomeEvent != null)
            {
                return SomeEvent();
            }

            return null;
        }

        public event Func<int> SomeEvent;
    }

    static void Main(string[] args)
    {
        var ev = new Ev();
        ev.SomeEvent += ev_someEvent1;
        ev.SomeEvent += ev_someEvent2;
        int? value = ev.RaiseSomeEvent();
        Console.WriteLine(value.HasValue ? value.Value.ToString() : "(null)");
    }

    static int ev_someEvent1()
    {
        return 5;
    }

    static int ev_someEvent2()
    {
        return 6;
    }
}

The output of this code:

6

Multiple event handlers

Notice that you only get the last value returned by all of the event handlers.

If you want to handle multiple return values in a somewhat event-like fashion, you may want to check out the Visitor Design Pattern instead. If you use this pattern, you would have to create a visitor adapter (or visitor adapters) that has the Accept methods on it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜