LINQ to XML 将多个 Uri 解析为单个超链接按钮 [英] LINQ to XML to parse multiple Uri's to a single hyperlinkbutton

查看:18
本文介绍了LINQ to XML 将多个 Uri 解析为单个超链接按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将解析的字符串传递给按钮单击事件时遇到问题.它在这个线程中解决了这里,但现在还有另一个小问题,但它与 LINQ TO XML 相关.我正在制作一个页面,该页面将显示我制作的所有应用程序,并将使用我将在我的服务器上托管的 XML 进行更新.在该页面上,我正在解析应用程序的图标、标题、价格和市场 Uri.我将应用程序的 Uri 和标题绑定到单个超链接按钮,但问题是列表中的每个超链接按钮都将我带到同一个市场页面.如何修复它以便每个超链接按钮都导航到不同的页面?

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?

代码如下:

  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();
    }

}

和 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>

推荐答案

就像我在上一篇文章中提到的,您可以使用 tag 参数.更新您的数据模板

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>

然后在您的活动中

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

        ShowMarket(selectedApp.AppUri);
    }

这篇关于LINQ to XML 将多个 Uri 解析为单个超链接按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆