原文地址:
http://www.cnblogs.com/suojh/archive/2012/02/23/2364094.html
做项目时遇到一个产品分类展示的页面,于是用Pivot来实现,因为每个PivotItem现实的列表模板是一样的,所以就创建了一个用户控件封装一个案例列表,,Pivot页面引用用户控件时传输对应数值得到不同类别的数据.做到这里都没问题,但做案例详细页面时问题出现了.用户控件页面不支持NavigationService.Navigate(new Uri("/Views/CaseInfo.xaml", UriKind.Relative)); 哎,这怎么办呢.网上搜索资料发现原理是这样来解决的 (App.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/Views/CaseInfo.xaml?id=" + curCity.id, UriKind.Relative)); 呵呵,搞定.下面是页面和代码
1.创建用户控件
<UserControl.Resources>
<DataTemplate x:Key="caseTemplate">
<StackPanel Width="436" Orientation="Horizontal" Height="124">
<StackPanel Width="160">
<Image Source="{Binding ImageUri}" Width="150" Height="94" HorizontalAlignment="Left" VerticalAlignment="Top" />
</StackPanel>
<StackPanel Width="285">
<TextBlock Text="{Binding title}" FontWeight="Bold" FontSize="24"/>
<TextBlock Text="{Binding Type}" FontWeight="Bold" Height="69" TextWrapping="Wrap"/>
</StackPanel>
<TextBlock Text="{Binding id}" Foreground="#FF140303" Visibility="Collapsed"/>
</StackPanel>
</DataTemplate>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" >
<ListBox x:Name="caseListBox" ItemTemplate="{StaticResource caseTemplate}" Width="440" Foreground="White" SelectionChanged="caseInfo_SelectionChanged" />
<!--<helpers:PopupSplash x:Name="pop" />-->
</Grid>
用户控件.cs页面代码
public int Kind { get; set; }
public CaseListControl()
{
InitializeComponent();
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
string url = "http://localhost/caseList.aspx?kind=" + Kind;
client.DownloadStringAsync(new Uri(url));
}
public void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
var xml = XElement.Parse(e.Result);
var videosTemp = (
from p in xml.Descendants("CaseList")
select new caseList()
{
title = p.Element("title").Value,
ImageUri = p.Element("ImageUri").Value,
LogoUri = p.Element("LogoUri").Value,
Type = p.Element("Type").Value,
id = p.Element("id").Value
}).ToList();
caseListBox.Items.Clear();
videosTemp.ForEach(p => caseListBox.Items.Add(p));
//pop.Visibility = Visibility.Collapsed;
}
}
private void caseInfo_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (caseListBox.SelectedIndex != -1)
{
caseList curCity = (caseList)caseListBox.SelectedItem;
(App.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/Views/CaseInfo.xaml?id=" + curCity.id, UriKind.Relative));
}
}
2.Pivot(案例分类展示页面)
<Grid x:Name="LayoutRoot" Background="Transparent">
<!--枢轴控件-->
<controls:Pivot Title="案例共享" Name="CasePivot" LoadingPivotItem="Pivot_LoadingPivotItem" SelectionChanged="CasePivot_SelectionChanged">
<!--枢轴项一-->
<controls:PivotItem Header="文化传播">
<Grid>
</Grid>
</controls:PivotItem>
<!--枢轴项二-->
<controls:PivotItem Header="医疗">
<Grid>
</Grid>
</controls:PivotItem>
<!--枢轴项三-->
<controls:PivotItem Header="教育">
<Grid>
</Grid>
</controls:PivotItem>
<!--枢轴项四-->
<controls:PivotItem Header="政府机构">
<Grid/>
</controls:PivotItem>
<!--枢轴项五-->
<controls:PivotItem Header="建筑规划">
<Grid/>
</controls:PivotItem>
<!--枢轴项六-->
<controls:PivotItem Header="广告">
<Grid/>
</controls:PivotItem>
<!--枢轴项七-->
<controls:PivotItem Header="能源">
<Grid/>
</controls:PivotItem>
<!--枢轴项八-->
<controls:PivotItem Header="IT">
<Grid/>
</controls:PivotItem>
</controls:Pivot>
</Grid>
Pivot(案例分类展示页面).cs页面代码
private void Pivot_LoadingPivotItem(object sender, PivotItemEventArgs e)
{
int Kind = Convert.ToInt32(NavigationContext.QueryString["Kind"]);
switch (Kind)
{
case 13:
(CasePivot.Items[0] as PivotItem).Content = new CaseListControl() { Kind = 13 };
break;
case 12:
(CasePivot.Items[1] as PivotItem).Content = new CaseListControl() { Kind = 12 };
break;
case 11:
(CasePivot.Items[2] as PivotItem).Content = new CaseListControl() { Kind = 11 };
break;
case 10:
(CasePivot.Items[3] as PivotItem).Content = new CaseListControl() { Kind = 10 };
break;
case 9:
(CasePivot.Items[4] as PivotItem).Content = new CaseListControl() { Kind = 9 };
break;
case 8:
(CasePivot.Items[5] as PivotItem).Content = new CaseListControl() { Kind = 8 };
break;
case 7:
(CasePivot.Items[6] as PivotItem).Content = new CaseListControl() { Kind = 7 };
break;
case 4:
(CasePivot.Items[7] as PivotItem).Content = new CaseListControl() { Kind = 4 };
break;
default:
break;
}
}
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
int Kind = Convert.ToInt32(NavigationContext.QueryString["Kind"]);
switch (Kind)
{
case 13:
CasePivot.SelectedIndex = 0;
break;
case 12:
CasePivot.SelectedIndex = 1;
break;
case 11:
CasePivot.SelectedIndex = 2;
break;
case 10:
CasePivot.SelectedIndex = 3;
break;
case 9:
CasePivot.SelectedIndex = 4;
break;
case 8:
CasePivot.SelectedIndex = 5;
break;
case 7:
CasePivot.SelectedIndex = 6;
break;
case 4:
CasePivot.SelectedIndex = 7;
break;
default:
break;
}
}
private void CasePivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int item = CasePivot.SelectedIndex;
switch (item)
{
case 0:
(CasePivot.Items[0] as PivotItem).Content = new CaseListControl() { Kind = 13 };
break;
case 1:
(CasePivot.Items[1] as PivotItem).Content = new CaseListControl() { Kind = 12 };
break;
case 2:
(CasePivot.Items[2] as PivotItem).Content = new CaseListControl() { Kind = 11 };
break;
case 3:
(CasePivot.Items[3] as PivotItem).Content = new CaseListControl() { Kind = 10 };
break;
case 4:
(CasePivot.Items[4] as PivotItem).Content = new CaseListControl() { Kind = 9 };
break;
case 5:
(CasePivot.Items[5] as PivotItem).Content = new CaseListControl() { Kind = 8 };
break;
case 6:
(CasePivot.Items[6] as PivotItem).Content = new CaseListControl() { Kind = 7 };
break;
case 7:
(CasePivot.Items[7] as PivotItem).Content = new CaseListControl() { Kind = 4 };
break;
default:
break;
}
}
最终效果图: