通过访问Windows Phone 7中的休息服务按字母顺序搜索记录

时间:2011-12-01 11:27:32

标签: c# windows-phone-7 rest

我试图通过访问Windows Phone 7中的休息服务按字母顺序搜索记录。

设计页面代码..               

            <controls:PivotItem Header="buddies">
             <toolkit:LongListSelector x:Name="BookList" Background="Transparent" IsFlatList="true"  

              GroupViewOpened="LongListSelector_GroupViewOpened"
              GroupViewClosing="LongListSelector_GroupViewClosing">
                <toolkit:LongListSelector.GroupItemsPanel>
                    <ItemsPanelTemplate>
                        <toolkit:WrapPanel Orientation="Horizontal"/>
                    </ItemsPanelTemplate>
                </toolkit:LongListSelector.GroupItemsPanel>
                <toolkit:LongListSelector.GroupItemTemplate>
                    <DataTemplate>
                        <Border Background="{Binding Converter={StaticResource GroupBackground}}" 
                                Width="99" Height="99" Margin="6" IsHitTestVisible="{Binding HasItems}">
                            <TextBlock Text="{Binding Title}" 
                                       FontFamily="{StaticResource PhoneFontFamilySemiBold}"
                                       FontSize="48"
                                       Margin="8,0,0,0"
                                       Foreground="{Binding Converter={StaticResource GroupForeground}}"                                        
                                       VerticalAlignment="Bottom"/>
                            <Border.Projection>
                                <PlaneProjection RotationX="-60"/>
                            </Border.Projection>
                        </Border>
                    </DataTemplate>
                </toolkit:LongListSelector.GroupItemTemplate>
                <toolkit:LongListSelector.GroupHeaderTemplate>
                    <DataTemplate>
                        <Border Background="Transparent" Margin="12,8,0,8">
                            <Border Background="{StaticResource PhoneAccentBrush}"  
                                    Padding="8,0,0,0" Width="62" Height="62"                 
                                    HorizontalAlignment="Left">
                                <TextBlock Text="{Binding Title}" 
                                           Foreground="#FFFFFF" 
                                           FontSize="48"
                                           FontFamily="{StaticResource PhoneFontFamilySemiLight}"
                                           HorizontalAlignment="Left"
                                           VerticalAlignment="Bottom"/>
                            </Border>
                        </Border>
                    </DataTemplate>
                </toolkit:LongListSelector.GroupHeaderTemplate>
                <toolkit:LongListSelector.ItemTemplate>
                    <DataTemplate>
                        <Grid Margin="12,8,0,8">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <Image Width="110" Height="150" Source="{Binding ImageUrl}" VerticalAlignment="Top"/>
                            <StackPanel Grid.Column="1" VerticalAlignment="Top">
                                <TextBlock Text="{Binding AutherName}" Style="{StaticResource PhoneTextLargeStyle}" FontFamily="{StaticResource PhoneFontFamilySemiBold}" Margin="12,-12,12,6"/>
                                <TextBlock Text="{Binding Email}" Style="{StaticResource PhoneTextNormalStyle}" TextWrapping="Wrap" FontFamily="{StaticResource PhoneFontFamilySemiBold}"/>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="Title:" Style="{StaticResource PhoneTextSmallStyle}"/>
                                    <TextBlock Text="{Binding Title}" Style="{StaticResource PhoneTextSmallStyle}" FontFamily="{StaticResource PhoneFontFamilySemiBold}"/>
                                </StackPanel>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="Price:" Style="{StaticResource PhoneTextSmallStyle}"/>
                                    <TextBlock Text="{Binding Price}" Style="{StaticResource PhoneTextSmallStyle}" FontFamily="{StaticResource PhoneFontFamilySemiBold}"/>
                                </StackPanel>
                            </StackPanel>
                        </Grid>
                    </DataTemplate>
                </toolkit:LongListSelector.ItemTemplate>

            </toolkit:LongListSelector>
            </controls:PivotItem>

    </controls:Pivot>
</Grid>

这是我的MainPage.xaml.cs页面代码

  private LongListSelector currentSelector;
    List<Person> objperson = null; 
    // Constructor
    public MainPage()
    {
        InitializeComponent();
        string Categoryid = "2";
        WebClient proxy = new WebClient();
        proxy.DownloadStringAsync(new Uri("http://localhost:3160/Service1.svc/GetListItemDetail/" + Categoryid));
        proxy.DownloadStringCompleted += new DownloadStringCompletedEventHandler(proxy_DownloadStringCompleted);
    }


    void proxy_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error == null)
        {
            XDocument doc = XDocument.Load(new StringReader(e.Result));

            var CatList = (from item in doc.Descendants("ItemDetail")
                           select new Person
                           {
                               GenreName = item.Element("GenreName").Value.ToString(),
                               ItemID = Convert.ToInt32(item.Element("ItemID").Value),
                               CatID = Convert.ToInt32(item.Element("CatID").Value),
                               GenreID = Convert.ToInt32(item.Element("GenreID").Value),
                               AutherName = item.Element("AutherName").Value.ToString(),
                               Title = item.Element("Title").Value.ToString(),
                               Email = item.Element("Email").Value.ToString(),
                               Price = item.Element("Price").Value.ToString(),
                               Description = item.Element("Description").Value.ToString(),
                               ImageUrl = item.Element("ImageUrl").Value.ToString()
                           }).ToList();


            objperson = new List<Person>();  
            objperson = CatList;

            BookList.ItemsSource = CatList;
        }
    }





    public List<Person> GetPersonListInfo()
    {
        List<Person> objp = new List<Person>();

        objp = objperson; 


        return objp;


    }

    private void LongListSelector_GroupViewOpened(object sender, GroupViewOpenedEventArgs e)
    {
        //Hold a reference to the active long list selector.
        currentSelector = sender as LongListSelector;

        //Construct and begin a swivel animation to pop in the group view.
        IEasingFunction quadraticEase = new QuadraticEase { EasingMode = EasingMode.EaseOut };
        Storyboard _swivelShow = new Storyboard();
        ItemsControl groupItems = e.ItemsControl;

        foreach (var item in groupItems.Items)
        {
            UIElement container = groupItems.ItemContainerGenerator.ContainerFromItem(item) as UIElement;
            if (container != null)
            {
                Border content = VisualTreeHelper.GetChild(container, 0) as Border;
                if (content != null)
                {
                    DoubleAnimationUsingKeyFrames showAnimation = new DoubleAnimationUsingKeyFrames();

                    EasingDoubleKeyFrame showKeyFrame1 = new EasingDoubleKeyFrame();
                    showKeyFrame1.KeyTime = TimeSpan.FromMilliseconds(0);
                    showKeyFrame1.Value = -60;
                    showKeyFrame1.EasingFunction = quadraticEase;

                    EasingDoubleKeyFrame showKeyFrame2 = new EasingDoubleKeyFrame();
                    showKeyFrame2.KeyTime = TimeSpan.FromMilliseconds(85);
                    showKeyFrame2.Value = 0;
                    showKeyFrame2.EasingFunction = quadraticEase;

                    showAnimation.KeyFrames.Add(showKeyFrame1);
                    showAnimation.KeyFrames.Add(showKeyFrame2);

                    Storyboard.SetTargetProperty(showAnimation, new PropertyPath(PlaneProjection.RotationXProperty));
                    Storyboard.SetTarget(showAnimation, content.Projection);

                    _swivelShow.Children.Add(showAnimation);
                }
            }
        }

        _swivelShow.Begin();
    }
    private void LongListSelector_GroupViewClosing(object sender, GroupViewClosingEventArgs e)
    {
        //Cancelling automatic closing and scrolling to do it manually.
        e.Cancel = true;
        if (e.SelectedGroup != null)
        {
            currentSelector.ScrollToGroup(e.SelectedGroup);
        }

        //Dispatch the swivel animation for performance on the UI thread.
        Dispatcher.BeginInvoke(() =>
        {
            //Construct and begin a swivel animation to pop out the group view.
            IEasingFunction quadraticEase = new QuadraticEase { EasingMode = EasingMode.EaseOut };
            Storyboard _swivelHide = new Storyboard();
            ItemsControl groupItems = e.ItemsControl;

            foreach (var item in groupItems.Items)
            {
                UIElement container = groupItems.ItemContainerGenerator.ContainerFromItem(item) as UIElement;
                if (container != null)
                {
                    Border content = VisualTreeHelper.GetChild(container, 0) as Border;
                    if (content != null)
                    {
                        DoubleAnimationUsingKeyFrames showAnimation = new DoubleAnimationUsingKeyFrames();

                        EasingDoubleKeyFrame showKeyFrame1 = new EasingDoubleKeyFrame();
                        showKeyFrame1.KeyTime = TimeSpan.FromMilliseconds(0);
                        showKeyFrame1.Value = 0;
                        showKeyFrame1.EasingFunction = quadraticEase;

                        EasingDoubleKeyFrame showKeyFrame2 = new EasingDoubleKeyFrame();
                        showKeyFrame2.KeyTime = TimeSpan.FromMilliseconds(125);
                        showKeyFrame2.Value = 90;
                        showKeyFrame2.EasingFunction = quadraticEase;

                        showAnimation.KeyFrames.Add(showKeyFrame1);
                        showAnimation.KeyFrames.Add(showKeyFrame2);

                        Storyboard.SetTargetProperty(showAnimation, new PropertyPath(PlaneProjection.RotationXProperty));
                        Storyboard.SetTarget(showAnimation, content.Projection);

                        _swivelHide.Children.Add(showAnimation);
                    }
                }
            }

            _swivelHide.Completed += _swivelHide_Completed;
            _swivelHide.Begin();

        });
    }
    private void _swivelHide_Completed(object sender, EventArgs e)
    {
        //Close group view.
        if (currentSelector != null)
        {
            currentSelector.CloseGroupView();
            currentSelector = null;
        }
    }

我是Windows Phone 7应用程序开发的新手,不知道在Longlistselector中对字母表进行分组。请帮助我。提前谢谢。

2 个答案:

答案 0 :(得分:0)

一种非常简单的方法是使用LongListSelector的专用集合。 I just so happen to have written one

基本上您会将代码更改为以下内容:

BookList.ItemsSource = new LongListCollection<Person, char>(CatList, x => x.Title[0]));

您将获得Title属性的第一个字符的字母分组。

您需要注意的唯一细节是,Person类需要实现IComparable<Person>Title属性进行排序(因为您做< / em>想要排序,对吗?)

简单地完成:

public int Compare(Person other)
{
    if (other == null)
        return 1;

    return this.Title.CompareTo(other.Title);
}

答案 1 :(得分:0)

我曾经使用过一次代码进行分组。如你所见,它与克劳斯的相似:

public class YourList : ObservableCollection<ItemsInGroup>
{
    private static readonly string Groups = "#abcdefghijklmnopqrstuvwxyz";

    Dictionary<string, ItemsInGroup> groups = new Dictionary<string, ItemsInGroup>();

    public YourList()
    {
        foreach (char c in Groups)
        {
            ItemsInGroup group = new ItemsInGroup(c.ToString());
            this.Add(group);
            groups[c.ToString()] = group;
        }
    }

    public void AddItem(Item item)
    {
        string GroupKey = Item.GetSomeFieldKey(item);// a, b, etc.

        for (int i = 0; i < groups[GroupKey].Count; i++)
        {
            if (Item.CompareBySomeField(item, groups[GroupKey][i]) < 0)
            {
                groups[Item.GetSomeFilesKey(item)].Insert(i, item);
                return;
            }
        }
        groups[GroupKey].Add(item);
    }
}

public class ItemsInGroup : ObservableCollection<Item>, INotifyPropertyChanged
{
    public ItemsInGroup(string category)
    {
        Key = category;
    }

    public string Key { get; set; }

    public bool HasItems { get { return Count > 0; } }

//INotifyPropertyChanged implementation

}

项目必须实施:

 public static string GetSomeFieldKey(Item item)

 public static int CompareBySomeFields(object obj1, object obj2)

用法:

 YourList list = new YourList();
 foreach (var item in resultListFromService)
 {
      list.AddItem(item); // fill list with items
 }
 myList.ItemsSource = list; // bind to UI

希望这有助于更好地理解它是如何工作的