范型类提供数据源
using System;
using System.Collections.Generic;
using System.Linq;
namespace CustomerUriApp
{
public class Customers
{
public List<Customer> GetAllCustomers()
{
List<Customer> c = new List<Customer>();
c.Add(new Customer()
{ CustomerId = 1,
CompanyName = "Microsoft" });
c.Add(new Customer()
{ CustomerId = 2,
CompanyName = "Google" });
c.Add(new Customer()
{ CustomerId = 3,
CompanyName = "Apple" });
return c;
}
public Customer GetCustomer(int customerId)
{
var customer =
from c in GetAllCustomers()
where c.CustomerId == customerId
select c;
return customer.First();
}
}
public class Customer
{
public int CustomerId { get; set; }
public string CompanyName { get; set; }
}
}
邦定数据控件
<uriMapper:UriMapping Uri="Customer/{customerId}"
MappedUri="/Views/CustomerDetails.xaml?customerId={customerId}" />
<ItemsControl x:Name="CustomersList">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<HyperlinkButton FontSize="24"
Content="{Binding CompanyName}"
Tag="{Binding CustomerId}"
Click="HyperlinkButton_Click" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
页面加载时邦定数据
public Home()
{
InitializeComponent();
Loaded += new RoutedEventHandler(Home_Loaded);
}
void Home_Loaded(object sender, RoutedEventArgs e)
{
Customers c = new Customers();
CustomersList.ItemsSource = c.GetAllCustomers();
}
hyperlinkbutton click 事件
private void HyperlinkButton_Click
(object sender, RoutedEventArgs e)
{
HyperlinkButton hyperlink = sender as HyperlinkButton;
string customerId = hyperlink.Tag.ToString();
this.NavigationService.Navigate
(new Uri
(string.Format("Customer/{0}", customerId), UriKind.Relative)); //传递customerid
}
CustomerDetails.xaml Page 接收传递参数
<StackPanel>
<TextBlock x:Name="CustomerId" FontSize="24"></TextBlock>
</StackPanel>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
CustomerId.Text = this.NavigationContext.QueryString["customerId"];
}