<phone:PhoneApplicationPage.Resources>
<!--定义组头绑定模板-->
<DataTemplate x:Key="GroupHeader">
<Border Background="{StaticResource PhoneAccentBrush}" Margin="{StaticResource PhoneTouchTargetOverhang}" Padding="{StaticResource PhoneTouchTargetOverhang}">
<StackPanel>
<TextBlock Text="{Binding Key}"/>
</StackPanel>
</Border>
</DataTemplate>
<!--定义组选项绑定模板-->
<DataTemplate x:Key="GroupItem">
<Border Background="{StaticResource PhoneAccentBrush}" Margin="{StaticResource PhoneTouchTargetOverhang}" Padding="{StaticResource PhoneTouchTargetOverhang}">
<TextBlock Text="{Binding Key}" Style="{StaticResource PhoneTextLargeStyle}"/>
</Border>
</DataTemplate>
<!--定义列头绑定模板-->
<DataTemplate x:Key="ListHeader">
<TextBlock Text="城市列表" Style="{StaticResource PhoneTextTitle1Style}"/>
</DataTemplate>
<!--定义列表选项绑定模板-->
<DataTemplate x:Key="ItemTmpl">
<Grid Margin="10,0,0,0">
<TextBlock Text="{Binding Cname}" Tap="TextBlock_Tap"></TextBlock>
</Grid>
</DataTemplate>
</phone:PhoneApplicationPage.Resources>
<!--LayoutRoot 是包含所有页面内容的根网格-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<!--TitlePanel 包含应用程序的名称和页标题-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"></StackPanel>
<!--ContentPanel - 在此处放置其他内容-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<toolkit:LongListSelector x:Name="LongList" Background="Transparent"
ItemTemplate="{StaticResource ItemTmpl}"
ListHeaderTemplate="{StaticResource ListHeader}"
GroupHeaderTemplate="{StaticResource GroupHeader}"
GroupItemTemplate="{StaticResource GroupItem}" Tap="LongList_Tap">
</toolkit:LongListSelector>
</Grid>
</Grid>
public partial class SelectCity : PhoneApplicationPage
{
private string cityname;
public SelectCity()
{
InitializeComponent();
CityList();
}
//继承Linq的IGrouping接口 来存储分组的数据
public class GroupingLayer : IGrouping
{
//分组数据
private readonly IGrouping grouping;
//初始化
public GroupingLayer(IGrouping unit)
{
grouping = unit;
}
//唯一的键值
public TKey Key
{
get { return grouping.Key; }
}
//重载判断相等方法
public override bool Equals(object obj)
{
GroupingLayer that = obj as GroupingLayer;
return (that != null) && (this.Key.Equals(that.Key));
}
public IEnumerator GetEnumerator()
{
return grouping.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return grouping.GetEnumerator();
}
}
//List选项的类 Content表示类别 Title表示选项的标题
public class Item
{
public string Title { get; set; }
public string Content { get; set; }
}
void CityList()
{
Uri url = new Uri("http://");
string cont = "http:///\" xmlns:tem="http://\">"
+""
+""
+""
+" 111"
+""
+" 111"
+""
+""
+""
+""
+""
+""
+""
+""
+""
+"";
WebClient webClient = new WebClient();
webClient.Encoding = System.Text.UTF8Encoding.UTF8;
webClient.Headers[HttpRequestHeader.ContentType] = "text/xml; charset=utf-8";
webClient.UploadStringAsync(url, "POST", cont);
webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(wc_UploadStringCompleted);
}
public class DS
{
public string Cname { get; set; }
//public string Pinyin { get; set; }
// public string Py { get; set; }
public string Item { get; set; }
}
private IEnumerable citylist;
private void wc_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
{
StringReader stream = new StringReader(e.Result);
XDocument xdoc = XDocument.Load(stream);
citylist = from query in xdoc.Descendants("ds")
select new DS
{
Cname = (string)query.Element("cname"),
//Pinyin = (string)query.Element("pinyin"),
//Py = (string)query.Element("py"),
Item = (string)query.Element("item"),
};
foreach (var item in citylist)
{
var d = item.Cname;
//MessageBox.Show(d);
}
//this.listBox1.ItemsSource = citylist;
//使用Linq来查询List数据 按照Content来进行分组
var selected = from c in citylist group c by c.Item into n select new GroupingLayer(n);
this.LongList.ItemsSource = selected;
}
//在所要获取item那项 添加点击事件
private void TextBlock_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
cityname = (e.OriginalSource as TextBlock).Text;
NavigationService.GoBack();
}
}