LINQ to XML to parse multiple Uri's to a single hyperlinkbutton

こ雲淡風輕ζ 提交于 2019-12-25 08:05:09

问题


I had a problem with passing a parsed string to a button click event. It was solved in this thread here, but now there's another small problem but it's with LINQ TO XML. I'm making a page which will show all of the apps that I make and will be updated with XML which I'll host on my server. On that page I'm parsing the icon, the title, the price and the marketplace Uri of the apps. I'm binding the Uri and the title of the apps to a single hyperlinkbutton but the problem is that every hyperlinkbutton on the list takes me to the same marketplace page. How can I fix it so every hyperlinkbutton navigates to a different page?

Here's the code:

  public partial class MorePage : PhoneApplicationPage
{
    private string appLink;

    public MorePage()
    {
        InitializeComponent();
    }

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        WebClient client = new WebClient();
        Uri uritoXML = new Uri("http://www.allanhaapalainen.com/AppsXML/MorePageXML.xml", UriKind.RelativeOrAbsolute);
        client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
        client.DownloadStringAsync(uritoXML);

        base.OnNavigatedTo(e);
    }

 public void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) 
    {
        XDocument moreApps = XDocument.Parse(e.Result);

        morePageAppList.ItemsSource = from Apps in moreApps.Descendants("App")
                                       select new MoreApps
                                       {
                                           MoreImage = Apps.Element("link").Value,
                                           Price = Apps.Element("price").Value,
                                           Title = Apps.Element("title").Value
                                       };

        var attribute = (from Apps in moreApps.Descendants("App")
                         select new MoreApps
                         {
                             AppAttribute = (string)Apps.Attribute("id").Value
                         }).FirstOrDefault();


        string appAttr = attribute.AppAttribute;

        var link = (from Apps in moreApps.Descendants("App")
                    where Apps.Attribute("id").Value == appAttr
                    select new MoreApps
                    {
                        AppUri = (string)Apps.Element("marketplace").Value
                    }).FirstOrDefault();

        appLink = link.AppUri;            
    }

    private void App_Name_Click(object sender, RoutedEventArgs e)
    {
        ShowMarket(appLink);
    }

    private void ShowMarket(string id)
    {
        MarketplaceDetailTask marketplaceDetailTask = new MarketplaceDetailTask();
        marketplaceDetailTask.ContentIdentifier = id;
        marketplaceDetailTask.Show();
    }

}

AND THE XAML:

<ListBox Height="500" Width="Auto" Name="morePageAppList" Margin="0,0,0,0" ItemsSource="{Binding}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                        <StackPanel Orientation="Horizontal" Height="173">
                            <Image Source="{Binding MoreImage}" Height="Auto" Width="Auto" />
                                <StackPanel Orientation="Vertical">
                                     <HyperlinkButton Name="appName" Content="{Binding Title}" Margin="15,60,0,0"  Click="App_Name_Click" />
                                     <TextBlock Name="price" Text="{Binding Price}" Margin="28,0,0,0" Foreground="#FFBD0000" />
                                </StackPanel>
                        </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

回答1:


Like I mentioned in your previous post, you can use the tag parameter. Update your DataTemplate

<DataTemplate>
                        <StackPanel Orientation="Horizontal" Height="173">
                            <Image Source="{Binding MoreImage}" Height="Auto" Width="Auto" />
                                <StackPanel Orientation="Vertical">
                                     <HyperlinkButton Name="appName" Content="{Binding Title}" Tag="{Binding}" Margin="15,60,0,0"  Click="App_Name_Click" />
                                     <TextBlock Name="price" Text="{Binding Price}" Margin="28,0,0,0" Foreground="#FFBD0000" />
                                </StackPanel>
                        </StackPanel>
                </DataTemplate>

then in your event

private void App_Name_Click(object sender, RoutedEventArgs e)
    {
        var button = (HyperLinkButton)sender;
        var selectedApp = (MoreApps)button.Tag;

        ShowMarket(selectedApp.AppUri);
    }


来源:https://stackoverflow.com/questions/8751845/linq-to-xml-to-parse-multiple-uris-to-a-single-hyperlinkbutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!