|
做项目时遇到一个产品分类展示的页面,于是用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.创建用户控件
用户控件.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(案例分类展示页面)
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;
}
}
最终效果图:
|
|
|