我将解析后的字符串传递给按钮点击事件时出现问题。它在这个线程here中得到了解决,但是现在又出现了一个小问题,但它与LINQ TO XML有关。我正在制作一个页面,它将显示我制作的所有应用程序,并将使用XML进行更新,我将在我的服务器上托管。在那个页面上,我正在解析应用程序的图标,标题,价格和市场Uri。我将Uri和应用程序的标题绑定到单个超链接按钮,但问题是列表上的每个超链接按钮都会将我带到同一个市场页面。如何修复它,以便每个超链接按钮导航到不同的页面?
以下是代码:
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>
答案 0 :(得分:1)
就像我在上一篇文章中提到的那样,您可以使用tag参数。更新您的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);
}