views:

385

answers:

1

Hi,

Recently, my question here was answered. Now that I've got my XML all parsed and looking pretty, I've got another question about my application I've been banging my head against a wall over the past few day(s).

The XML is used to automatically add Artist names to a listbox. What I want to do is provide links to Amazon searches from these artists. In the following function, the XML is parsed and the artist name is then added to the list. I need to somehow put a hyperlink on this artist name. Does anybody know how this would be possible?

EDIT: I am missing the connection between steps 2 and 3 in the answer that has been provided. Also, I do not understand how number 3 works at all. I must admit I'm a neophyte at Silverlight programming. From my understanding, you do the binding in the XAML page. How can this be done for listbox items that have not even been created yet?

Additionally, I realized something that the Amazon URLs use + signs where spaces are in artist names. I edited the code to reflect that. Please understand that having the hyperlink as text under each artist name is not what I'm going after. ;)

public void DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error == null || e.Error.Message.IndexOf("NotFound") == -1)
        {
            ArtistsList.Items.Clear();
            uname.Text = "Try Another One!";
            XDocument doc = XDocument.Parse(e.Result);
            var topArtists = from results in doc.Descendants("artist")
            select results.Element("name").Value.ToString();
            foreach (string artist in topArtists)
            {
                ArtistsList.Items.Add(artist);

                string amazonPlus = artist.Replace(" ", "+");

                string amazonURL = "http://www.amazon.ca/s/ref=nb_ss_gw?url=search-alias%3Daps&field-keywords=" + amazonPlus + "&x=0&y=0";
                ArtistsList.Items.Add(amazonURL);
            }
        }
    }

EDIT 2 Is there anybody who can clarify the answer provided?

A: 
  1. Create an Artist object with a Name and Amazon Url Property
  2. When you parse the XML, create a collection of items using LINQ:

    var topArtists = from result in doc.Descendants("artists")

        select new Artist
        {
            Name = result.Element("name").Value,
            Amazon = new Uri(string.format("http://amazon.com/artist={0}", result.Element("name").Value), UriKind.Absolute),
        };
    
    
    ArtistList.ItemsSource = topArtists;
    
  3. I would then use a data template to bind the Name to a TextBlock Text or HyperlinkButton Content and the Amazon property to the HyperlinkButton.NavigateUrl.

Michael S. Scherotter