我正在尝试在this tutorial之后通过WCF填充Longlist。我想按收入(从最小到最大)对用户进行分组,但是随机显示组。列表中也没有项目(见下图)。我错过了什么? 我的XAML代码是:
<phone:PhoneApplicationPage.Resources>
<phone:JumpListItemBackgroundConverter x:Key="BackgroundConverter"/>
<phone:JumpListItemForegroundConverter x:Key="ForegroundConverter"/>
<DataTemplate x:Key="usersHeaderTemplate">
<Border Width="72" Height="72" HorizontalAlignment="Left" Background="{Binding Converter={StaticResource BackgroundConverter}}" Margin="6">
<TextBlock Text="{Binding Income}"
FontSize="20" Padding="6"
VerticalAlignment="Center" HorizontalAlignment="Center"
Foreground="{Binding Converter={StaticResource ForegroundConverter}}" />
</Border>
</DataTemplate>
<DataTemplate x:Key="usersItemTemplate">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Flag}" Height="50" Margin="0,0,10,0"/>
<StackPanel>
<TextBlock Text="{Binding Name}" Style="{StaticResource PhoneTextSubtleStyle}" />
<TextBlock Text="{Binding Income,StringFormat='Income: {0}'}" />
<TextBlock Text="{Binding Job}"/>
</StackPanel>
</StackPanel>
</DataTemplate>
<Style x:Key="LongListSelectorJumpListStyle" TargetType="phone:LongListSelector">
<Setter Property="GridCellSize" Value="113,113"/>
<Setter Property="LayoutMode" Value="Grid" />
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<Border Background="{Binding Converter={StaticResource BackgroundConverter}}"
Width="113" Height="113" Margin="6" >
<TextBlock Text="{Binding Title}"
FontFamily="{StaticResource PhoneFontFamilySemiBold}"
FontSize="48" Padding="6"
Foreground="{Binding Converter={StaticResource ForegroundConverter}}"
VerticalAlignment="Center"/>
</Border>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
<DataTemplate x:Key="usersListHeader">
<Border Background="White" Opacity="0.2" Height="70">
<TextBlock Text="List Header" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="Black" />
</Border>
</DataTemplate>
<DataTemplate x:Key="usersListFooter">
<Border Background="White" Opacity="0.2" Height="70">
<TextBlock Text="List Footer" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="Black"/>
</Border>
</DataTemplate>
</phone:PhoneApplicationPage.Resources>
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<phone:LongListSelector x:Name="longlist2" Height="608" Margin="10,15,24,0" VerticalAlignment="Top" Width="422"
IsGroupingEnabled="True" LayoutMode="List" HideEmptyGroups="False"
ItemTemplate="{StaticResource usersItemTemplate}"
GroupHeaderTemplate="{StaticResource usersHeaderTemplate}"
JumpListStyle="{StaticResource LongListSelectorJumpListStyle}"
ListHeaderTemplate="{StaticResource usersListHeader}"
ListFooterTemplate="{StaticResource usersListFooter}" >
</phone:LongListSelector>
</Grid>
</Grid>
我的服务实施是:
public IEnumerable<mTeachers> GetStuffList(string Job)
{
List<mTeachers> stuffList = new List<mTeachers>();
DataClasses1DataContext data = new DataClasses1DataContext();
List<mTeachers> finalList = new List<mTeachers>();
foreach (var d in data.Stuffs)
{
stuffList.Add(new mTeachers() { Name = d.stuffName, Income = (int)d.stuffIncome, Job = Job, Flag = new Uri(@"/Assets/users.png", UriKind.Relative) });
}
return stuffList;
}
private List<Group<mTeachers>> GetUsersGroups(string Job)
{
IEnumerable<mTeachers> usersList = GetStuffList(Job);
return GetItemGroups(usersList, c => c.Income);
}
private static List<Group<T>> GetItemGroups<T>(IEnumerable<T> itemList, Func<T, int> getKeyFunc)
{
IEnumerable<Group<T>> groupList = from item in itemList
group item by getKeyFunc(item) into g
orderby g.Key
select new Group<T>(g.Key, g);
return groupList.ToList();
}
public class Group<T> : List<T>
{
public Group(int income, IEnumerable<T> items)
: base(items)
{
this.Income = income;
}
public int Income
{
get;
set;
}
}
我的服务界面:
[OperationContract]
IEnumerable<mTeachers> GetStuffList(string Job);
}
[DataContract]
public class mTeachers
{
[DataMember]
public string Name { get; set; }
[DataMember]
public int Income { get; set; }
[DataMember]
public string Job { get; set; }
[DataMember]
public System.Uri Flag { get; set; }
}
编辑: WP页面代码:
public MainPage()
{
InitializeComponent();
ServiceReference1.Service1Client proxy = new ServiceReference1.Service1Client();
proxy.GetStuffListCompleted += showList;
proxy.GetStuffListAsync("teacher");
}
private void showList(object sender, ServiceReference1.GetStuffListCompletedEventArgs e)
{
this.longlist2.ItemsSource = e.Result;
}
答案 0 :(得分:0)
将ItemsSource="{Binding}"
添加到XAML中的LongListSelector声明中。所以看起来应该是这样的。
<phone:LongListSelector x:Name="longlist2" Height="608" Margin="10,15,24,0" VerticalAlignment="Top" Width="422"
IsGroupingEnabled="True" LayoutMode="List" HideEmptyGroups="False"
ItemTemplate="{StaticResource usersItemTemplate}"
GroupHeaderTemplate="{StaticResource usersHeaderTemplate}"
JumpListStyle="{StaticResource LongListSelectorJumpListStyle}"
ListHeaderTemplate="{StaticResource usersListHeader}"
ListFooterTemplate="{StaticResource usersListFooter}"
ItemsSource="{Binding}" >
</phone:LongListSelector>
答案 1 :(得分:0)
我认为这里有两件事情。
首先我认为你的GetStuffList(字符串Job)函数做了一些奇怪的事情,请检查返回值,看看它是否正是你想要的。
其次我认为ItemSource需要像KasunKV那样设置。
以下是我的修补程序以及显示其有效的结果屏幕截图。
// create a static group list to see if it works
private List<Group<mTeachers>> GetUsersGroups(string Job)
{
List<mTeachers> usersList = new List<mTeachers>();
usersList.Add(new mTeachers() { Name = "Bob", Income = 1000, Flag = new Uri(@"/Assets/AlignmentGrid.png", UriKind.Relative), Job = "Guy" });
usersList.Add(new mTeachers() { Name = "Dan", Income = 1000, Flag = new Uri(@"/Assets/AlignmentGrid.png", UriKind.Relative), Job = "Guy" });
usersList.Add(new mTeachers() { Name = "Kate", Income = 2000, Flag = new Uri(@"/Assets/AlignmentGrid.png", UriKind.Relative), Job = "Girl" });
usersList.Add(new mTeachers() { Name = "Charlie", Income = 2000, Flag = new Uri(@"/Assets/AlignmentGrid.png", UriKind.Relative), Job = "Guy" });
usersList.Add(new mTeachers() { Name = "Jeff", Income = 2000, Flag = new Uri(@"/Assets/AlignmentGrid.png", UriKind.Relative), Job = "Guy" });
usersList.Add(new mTeachers() { Name = "Jeff2", Flag = new Uri(@"/Assets/AlignmentGrid.png", UriKind.Relative), Job = "Guy" });
return GetItemGroups(usersList, c => c.Income);
}
// Constructor
public MainPage()
{
InitializeComponent();
// create the user group and bind it to item source
this.longlist2.ItemsSource = GetUsersGroups("whatever");
}
可以在此处查看更改结果: